我试图写一个循环,我让用户输入一个米的值,然后根据菜单给出他们会选择一个打印转换的选项,它应该运行直到用户输入4退出程序。但是我的程序无法无序地运行每个循环或不止一次,我不确定为什么。我知道转换和休息,但是我的老师不鼓励我们做任何我们尚未做的任何课程。
import javax.swing.JOptionPane;
public class Conversion_LAbA {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Junior Nieves Alvarez, Conversion LabA
String inputString, inputOption;
double meters, kilometers, inches, feet;
int option;
inputString = JOptionPane.showInputDialog("Enter the Meters value");
meters = Double.parseDouble(inputString);
if (meters < 1)
{
inputString =JOptionPane.showInputDialog("Enter the Meters value");
}
System.out.println("Choose one of the Following"
+ " 1. Convert to Kilometers"
+ " 2. Convert to Inches"
+ " 3. Convert to Feet"
+ " 4. Exit");
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
while (option == 1)
{
kilometers = meters*0.001;
System.out.println(kilometers);
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
}
while (option == 2)
{
inches = meters*39.37;
System.out.println(inches);
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
}
while (option == 3)
{
feet= meters*3.281;
System.out.println(feet);
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
}
if (option == 4)
{
System.out.println("The program will end");
System.exit(0);
}
}
}
答案 0 :(得分:1)
您的逻辑存在一些问题。
您希望在while循环中包含米的输入,这样您就可以确保用户输入的仪表对程序执行的其余部分有效。
剩下要做的就是在while循环中使用if语句而不是while循环包装你的选项。这样您就可以选择一个选项,然后提示您在程序运行中尝试不同的转换,并能够以您想要的任何顺序输入您的选项。
最后,将测量符号放在输出旁边是有帮助的,这样当用户选择一个选项时,他们可以看到他们正在使用的转换,km为千米,“英寸和英尺”。
String inputString, inputOption;
double meters = 0, kilometers, inches, feet;
int option = 0;
while(meters < 1)
{
inputString = JOptionPane.showInputDialog("Enter the Meters value");
meters = Double.parseDouble(inputString);
if (meters < 1)
{
System.out.println("Invalid Meter value, please enter a value >= 1");
option = 0;
}
}
while(option != 4){
System.out.println("Choose one of the Following"
+ "\n 1. Convert to Kilometers"
+ "\n 2. Convert to Inches"
+ "\n 3. Convert to Feet"
+ "\n 4. Exit");
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
if(option == 1)
{
kilometers = meters*0.001;
System.out.println(kilometers + "km");
}
else if(option == 2)
{
inches = meters*39.37;
System.out.println(inches + "\"");
}
else if(option == 3)
{
feet= meters*3.281;
System.out.println(feet + "'");
}
else if (option == 4)
{
System.out.println("The program will end");
}
}
答案 1 :(得分:0)
如果在每个选项上都有while循环,编译器将继续运行该循环,直到数字发生变化。这个数字不会改变,所以国家将保持不变。
您需要一个switch语句或一系列IF-ELSE语句。之前我有一位老师让我们做了IF-ELSE语句,向我们展示了很多选项带来的麻烦。然后他们给了我们转换声明。
答案 2 :(得分:0)
没有休息,没有开关,有一些验证
package com.qvs;
import javax.swing.JOptionPane;
public class Conversion_LAbA {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Junior Nieves Alvarez, Conversion LabA
String inputString = "", inputOption;
double meters;
int option;
for (;;) {
inputString = JOptionPane.showInputDialog("Enter the Meters value");
meters = Double.parseDouble(inputString);
if (meters >= 1) {
System.out.println("Choose one of the Following"
+ " 1. Convert to Kilometers"
+ " 2. Convert to Inches"
+ " 3. Convert to Feet"
+ " 4. Exit");
option = 0;
while ((option < 1) || (option > 4)) {
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
if (option == 1) {
System.out.println(meters * 0.001);
} else if (option == 2) {
System.out.println(meters * 39.37);
} else if (option == 3) {
System.out.println(meters * 3.281);
} else if (option == 4) {
System.out.println("The program will end");
System.exit(0);
} else {
System.out.println("Invalid option");
}
}
} else {
System.out.println("Meters entered must be >= 1");
}
}
}
}