Ichise Ichise - 1 month ago 6
Java Question

Java error with a while and if statement

Hi I'm new to Java and for my intro class I have to program something that does the following: Define a problem with user input, user output, While Statement and some mathematical computation.

What I want to do is have it prompt the user for feet and inches for height, if it's over 5'8 they can't go on the roller coaster; if it's 5'8 or less they can. I realize this would be much easier with like an if else kind of thing but I'm required to use while; also we haven't covered do while yet so I can't use that either. I'm probably messing up horribly and there's some better way to do this, but this is what I have so far.

import java.util.Scanner;

public class coasterHeight
{
public static void main(String[] args)
{


Scanner keyboard = new Scanner(System.in);
int feet;
int inches;

System.out.println ("Your must be at least 5'8 to ride this ride.");
System.out.println ("Please enter your height in feet:");

feet = keyboard.nextInt();
System.out.println ("Please enter your height in inches:");

inches = keyboard.nextInt ();



while (feet <= 5 && inches <= 8)
{
System.out.println("You can go on this ride.");
break;
}


{
if (feet >= 6 && inches >=9)

{
System.out.println ("You cannot go on this ride.");
}
}
}


}


So here's the problem. When the input meets the while requirements it works fine (it used to go on an infinite loop with "You can go on this ride" but I discovered break;), but for the if statement, nothing appears in the output. The "You cannot go on this ride", nothing appears, there's no error or anything it just ends the output after I enter a height that is over 5'8. Like I said this is probably awful but any help would be appreciated, thanks.

Answer

Try using float or double instead, where float height = 0.00 or double height = 0.0. Then you can use it in the while statement such as

    while (height >= 5.8){
        System.out.println("You cannot enter the ride");
        break;
    }
    while (height < 5.8){
        System.out.println("You can enter the ride");
        break;
    }
Comments