vendredi 27 mars 2015

boolean in Java program

Hi there i'm just beginner to JAVA so 1: I got confused by how both of these variables:



boolean firstNumberEntered=false;
boolean secondNumberEntered=false;


control the conditions inside and outside the loop?!!.


2: what will the values before and after it exits While loop?!!


3: can the values of these variables



double firstNumber=0.0,secondNumber=0.0;


be stored inside the boolean variables and when if it's so?!!


4: What is the relation between



boolean firstNumberEntered=false;
boolean secondNumberEntered=false;


and if-statements inside the loop!!?


Thank you:)



public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Welcome to calculations in order");

int itemMenu=0;

boolean userWantsToContinue=true;

boolean firstNumberEntered=false;
boolean secondNumberEntered=false;

double firstNumber=0.0,secondNumber=0.0;

while (userWantsToContinue){


if (firstNumberEntered) {
System.out.println("1.First number "+"(Currently): "+'('+firstNumber+')');//
} else {
System.out.println("1.First number "); //it will be displayed at first because its value true
}

if (secondNumberEntered){
System.out.println("2.Second number "+"(Currently): "+'('+secondNumber+')');
}else{
System.out.println("2.Second number ");

System.out.println("3.Takes Square");
System.out.println("4.Takes Cube");
System.out.println("5.Multiplication");
System.out.println("0.Exit");

System.out.println("\nPlease choose an item");
switch (itemMenu=in.nextInt()){

case 1:
System.out.println("Please Enter First Number: ");
firstNumber=in.nextDouble();

firstNumberEntered=true;
break;

case 2:
System.out.println("Please Enter second Number");
secondNumber=in.nextDouble();
secondNumberEntered=true;
break;
case 3:
if(firstNumberEntered&&secondNumberEntered){
double square=firstNumber*secondNumber;
System.out.println("Square results = "+square);
}else{
System.out.println("Please go back to item 1 & 2");
}
break;
case 4:

if(firstNumberEntered){
double cube=firstNumber*firstNumber*firstNumber;

System.out.println("Cube results= "+ cube);

}else{
System.out.println("Please go back to 1&2");

}


break;
case 5:
if(firstNumberEntered&&secondNumberEntered){
double multiplication= firstNumber*secondNumber;
System.out.println("Multiplication of one and second number = "+multiplication);
}else{
System.out.println("Please go back to item 1 & 2");
}
break;
case 0:
System.out.println("thank you visit us again لاتنسى!!");
userWantsToContinue=false;
break;
default:
System.out.println("Invalid option!!@_@");
System.out.println("Please enter the number of the menu item you want and don't do this mistake again!!.");
itemMenu=in.nextInt();

}
}
}

Aucun commentaire:

Enregistrer un commentaire