Home > OS >  how to write range for a variable in if statement?
how to write range for a variable in if statement?

Time:04-26

i want to make a project that will use if statement for my problem please see the picture:: If the user enter a height of the window between 20_21.99 and the width of between 14_15.99 it will show the number of balance you should use is number 3.

so in the if statement can i write if(hight is betweent 20 and 21.99) (width is between 14 and 15.99)[ balance= 3; system.out.println(balance);

Scanner scan = new Scanner(System.in);
System.out.println("please enter hight ");
double hight= scan.nextDouble();
System.out.println("please enter width");
double width=scan.nextDouble();
double balance=0;    

if(hight==20 && width==14){
balance=3;
System.out.println("please use balance number "   balance);}
  
  


}

OUTPUT// please enter hight 20 please enter width 14 please use balance number 3.0

the picture: https://i.stack.imgur.com/tJa7b.pn

CodePudding user response:

Question is not clear though what I have understood that balance should be 3, if height and width in range. Have a look look at this.

Condition inside if statement should be something like this: if( (height>=20 && height<=21.99) && (width>=14 && width<=15.99) ){ \\do something }

CodePudding user response:

You can create a range using brackets to specify the upper and lower limits.

Scanner scan = new Scanner(System.in);

double height = 0, width = 0, balance = 0; // Declare variables

System.out.println("Please enter height");
height = scan.nextDouble();

System.out.println("please enter width");
width = scan.nextDouble();

if ((height >= 20.0d && height <= 21.99d) && (width >= 14.0d && width <= 15.99d)) { 
    balance = 4.0d; 
}

System.out.println("Please use balance number "   balance);
  • Related