【发布时间】:2016-10-07 14:07:36
【问题描述】:
我的程序要求用户输入一个选项(1、2、3 或 0)。选项 1 要求用户输入英尺和英寸。选项 2 要求用户输入厘米。 每次我这样做时,程序都会打印出我需要的两倍(请参见下文):
Enter choice: 1
Enter feet: 12
Enter inches: 2
Enter feet: 12 //This second part should not happen
Enter inches: 2
我已尝试注释掉 addConversion(...) 行,但这不允许我正确打印选项 3。当我注释掉 System.out.println(feetInchesToCm()) 时,我对该案例的转换没有打印出来。
我的代码主要功能如下:
public static void main(String[] args) throws NumberFormatException, IOException
{
int choice;
do
{
displayMenu();
choice = getInteger("\nEnter choice: ", 0, Integer.MAX_VALUE);
if(choice == 1)
{
addConversion(feetInchesToCm());
System.out.println("");
System.out.println(feetInchesToCm());
}
else if(choice == 2)
{
addConversion(cmTofeetInches());
System.out.println("");
System.out.println(cmTofeetInches());
}
else if(choice == 3)
{
if(_prevConversions == null)
{
break;
}
else if(_prevConversions != null)
{
for(int i = 1; i <= _numConversions; i++)
{
System.out.print("Conversion # " + i + ": ");
System.out.println(_prevConversions[i]);
}
}
}
}while(choice != 0);
if(choice == 0)
{
System.out.println("\nGoodbye!");
System.exit(0); //Ends program
}
}
提前感谢您的帮助!
【问题讨论】:
-
feetInchesToCm是什么? -
投反对票的人,如果您要投反对票,请提供反馈以帮助 OP 了解他做错了什么。没有理由的投票只会使人们远离该网站。我们希望人们回来,但提出有用且有意义的问题。
-
@Dale 没有人有义务在投票后发表评论,但我之所以这样做是因为问题基本上是要求我们进行调试,并且缺少许多重要细节。
-
我想知道为什么这个问题得到了 -4 票 O.o
-
@Dale 同意。我的回答也解决了这个问题,即使没有提供足够的信息