-4

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?

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();

        }
    }
}
Willi Mentzel
  • 21,499
  • 16
  • 88
  • 101

3 Answers3

0
  1. Seems they only do some work inside the loop.
  2. Depends on what is entered.
  3. A boolean cannot hold that double value.
  4. They seem to indicate if the user provided input for the first and the second double. Depending on the option chosen by the input (switch statement) it might do a calculation.
Floris Velleman
  • 4,658
  • 3
  • 27
  • 45
0

1 - They variables check if the user inserted some values(first and second). 2 - Before the while-loop the variables have the declared default values (false and false). After the loop, the variables hold the last assigned values. But your program will exit after the while-loop. This means, you will have to start it again and the variables will get the default values. 3 - You can not store a double-value in a boolean-variable. Java has strict data-types, it's not like javascript or php. 4 - The booleans are just flags. They check if the user insert some data. After the first insert the variable firstNumberEntered will be set to true. After the second insert the variable secondNumberEntered will be set to true.

miago
  • 101
  • 1
  • 7
0

A boolean is a Java primitive type that can store only two values: true or false

You need to use a boolean value or an expression that resolves to a boolean value in a while loop

1: I got confused by how both of these variables:

boolean firstNumberEntered=false;
boolean secondNumberEntered=false;

These are two variables that store a value, as their type are boolean they can only store true or false

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

It works exactly the same as other primitives like int or double. As the variables are declared outside the loop, they still exists after the 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?!!

No. boolean only can have two values: true or false

This is illegal:

firstNumberEntered = firstNumber; // The types are incompatible.

This is legal:

boolean areEquals = ( 4 == 2 + 2); // `true` is stored in the boolean.

4: What is the relation between

boolean firstNumberEntered=false; boolean secondNumberEntered=false;

and if-statements inside the loop!!?

You can use booleans or expression that resolve to a boolean as the argument of a if or while statement. To make an example:

int a = 3;
int b = 3;
boolean sameValue = a == b;
if (a == b) // Equivalent to  if(sameValue)
David SN
  • 3,026
  • 1
  • 13
  • 20