About if statements in java

Asked 2 years ago, Updated 2 years ago, 111 views

I am currently studying java at NetBeans.

Scanner sc=new Scanner (System.in);

System.out.print("Determination");
int x = sc.nextInt();

if(x>=0.4)
            System.out.println("Determination A";
else if (x>=0.3&x<0.4)
            System.out.println("Determination B";
else
            System.out.println("Determination C";


when you type
If it's 1 or 0, it will run well.
If you type 0.5 or 0.2, you will get an error.
Please find out the cause.

java netbeans

2022-09-30 21:28

2 Answers

The int type can be an integer, such as 1 or 0.
You must use double type (or float type) to represent decimal places such as 0.5 and 0.2.

For the code in question, doublex=sc.nextDouble(); instead of intx=sc.nextInt(); works.


2022-09-30 21:28

Scanner sc=new Scanner (System.in);

System.out.print("Determination");
double x = sc.nextDouble();

if(x>=0.4)
            System.out.println("Determination A";
else if (x>=0.3&x<0.4)
            System.out.println("Determination B";
else
            System.out.println("Determination C";


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.