0

When I run the program it runs the else part and executes but on the if part it just keeps looping and doesn't stop until I have to forcibly stop it. I'm trying to give it parameters on the choice from 1 to 4 and parameters to meters that doesn't go under 1. is there an alternative to having the parameters?

import java.util.*;

public class Project4

{
    public static void showKilometers(double meters) //this is a parameterized function

    {
        double kilometers = meters * 0.001;
        System.out.println(meters +" meters is " + kilometers + " kilometers.");
    }

    public static void showInches(double meters)
    {
        double inches = meters * 39.37;
        System.out.println(meters +" meters is " + inches + " inches.");
    }

    public static void showFeet(double meters)
    {
        double feet = meters * 3.281;
        System.out.println(meters +" meters is " + feet + " feet.");
    }

    public static void quitProgram()
    {
        System.out.println("Goodbye!");
        System.out.println(0);
    }
    public static void showMenu()
    {
        System.out.println(" 1. Convert to kilometers ");
        System.out.println(" 2. Convert to inches ");
        System.out.println(" 3. Convert to feet ");
        System.out.println(" 4. Quit the program ");
        System.out.println(" ");
    }

    public static void main (String [] args)
    {

        double meters;
        int choice;



        Scanner keyboard = new Scanner (System.in);

        System.out.println("Enter a distance in meters: ");
        meters = keyboard.nextDouble();
        while (meters <=0 || meters > 0)
        {
            if (meters > 0)
            {
                showMenu();
                meters = keyboard.nextDouble();
            }
            else
            {
                System.out.println("Please enter a number greater than 1");
                meters = keyboard.nextDouble();
                showMenu();
            }
        }

        choice = keyboard.nextInt();
        switch(choice) //note the use of switch case
            {
            case 1: showKilometers(meters);
            break;
            case 2:showInches(meters);
            break;
            case 3:showFeet(meters);
            break;
            case 4:
            quitProgram();
            }
    }
}

3 Answers 3

3

It's an infinite loop. You might as well write while (true)

Try either while (meters <= 0) or while (meters >0)

Or:

Scanner keyboard = new Scanner (System.in);

System.out.println("Enter a distance in meters: ");
meters = keyboard.nextDouble();
while (meters < 1 || meters.isNaN())
{
    System.out.println("Please enter a number greater than 1");
    meters = keyboard.nextDouble();
    showMenu();
}

Your output also specifies "Greater than 1", so your condition needs to agree.

Sign up to request clarification or add additional context in comments.

6 Comments

Unless the user is mean enough to enter a NaN ;)
He might aswell remove the if too.
When the program runs and the while loop is false it doesn't run the rest of the program.
@IvanNewYork it doesn't run the rest of the program isn't that the point of using the while loop?
@John To add an error message, do you think I should do an IF statement since the while loop will not run when false?
|
1
 while (meters <=0 || meters > 0) is an infinite loop

Comments

0

The condition specified in the loop is contradictory. The condition must be used like this :

while (meters >= 0)
{
    // logical statements here if the condition is satisfied
}
else
{
    // logical statements here if the condition is not satisfied
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.