プログラムを実行すると、 else部分が実行されて実行されますが、if部分ではループし続け、強制的に停止するまで停止しません。1 から 4 までの選択肢のパラメーターと、1 を下回らないメーターのパラメーターを指定しようとしています。パラメーターを指定する代わりの方法はありますか?
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();
}
}
}