import java.util.Scanner; import java.io.PrintStream; import java.util.Random; public class Main { public static PrintStream out = System.out; public static Scanner in = new Scanner(System.in); public static void main(String[] args) { float a = 0, b = 0; boolean d, l; if (in.hasNextInt()) b = in.nextInt(); d = (-3.0 <= b) && (b <= 5.0); l = (9.0 <= b) && (b <= 15.0); out.print(d || l); } } 

the result for 6.2 is d = true (??), l = false

why?(

  • first of all it is necessary to check whether you enter the if condition (in.hasNextInt ()) - Grundy

1 answer 1

hasNextInt - return true if this is a valid int value

At input 6.2, it is obviously not an integer, so this method returns false , and the value of b remains at 0

Further in the conditions:

 -3.0 <= 0 && 0 <= 5 // очевидно true 9.0 <= 0 && 0 <= 15.0 // false 
  • That is, you need to use a similar method for float? Thank you) - Vyacheslav Potseluyko
  • one
    @VyacheslavPotseluyko, it’s generally worth using the method for the type you need. In this case, yes you can use hasNextFloat - Grundy
  • holy man you are :). Thank you) - Vyacheslav Potseluyko