228k views
4 votes
Write an expression that attempts to read a double from standard input and store it in an double variable, x, that has already been declared. This expression's value should be true if the attempt was successful but false if no data was read because there was no more data available.

User Kobik
by
4.5k points

1 Answer

4 votes

Answer:

x = scan.nextDouble();

if(x == Math.floor(x)){

System.out.println("False. The number is not a double");

} else {

System.out.println("True. The number is a double");

}

Step-by-step explanation:

First,

x = scan.nextDouble();

allow the receive input to be assigned to the already declared x.

The next block of if-else statement evaluate to true or false depending on if the number entered is a double or not.

The if statement check if the flooring of x is the same as x, if it is, then the number is not a double else if they are not the same the it is a double.

The flooring of a number remove the decimal part of the number and the decimal part of the number is what define a number as double.

User Chebus
by
4.6k points