所以我做了一个程序,根据用户的需求做不同的数学题,我不希望程序在用户完成后结束。现在,我的循环只是一遍又一遍地重复同样的选择。理想的效果是能够不断选择你想做的问题。我是相当新的编码,所以我道歉,如果这是一个简单的问题。
public static void main(String[] args)
{
System.out.println("What problem do you want to do?");
System.out.println("1:Celcius to Farenheit");
System.out.println("2:Slope Formula");
System.out.println("3:Averages");
System.out.println("4:Perimeter");
System.out.println("5:Exit\n\n");
Scanner keyboard = new Scanner(System.in);
int response = keyboard.nextInt();
do
{
if (response == 1)
{
System.out.println("Degrees Celcius:");
int degrees = keyboard.nextInt();
System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
}
else if (response == 2)
{
System.out.println("x1:");
int x1 = keyboard.nextInt();
System.out.println("y1:");
int y1 = keyboard.nextInt();
System.out.println("x2:");
int x2 = keyboard.nextInt();
System.out.println("y2:");
int y2 = keyboard.nextInt();
System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
}
else if (response == 3)
{
System.out.println("First number:");
int num1 = keyboard.nextInt();
System.out.println("Second number:");
int num2 = keyboard.nextInt();
System.out.println("The average is " + average(num1,num2));
}
else if (response == 4)
{
System.out.println("Length:");
int length = keyboard.nextInt();
System.out.println("Width:");
int width = keyboard.nextInt();
System.out.println("The perimeter is " + perimeter(length,width));
}
}
while (response != 5);
2条答案
按热度按时间axzmvihb1#
声明
int response
在圈外。圈内的地方response = keyboard.nextInt();
与int response = keyboard.nextInt()
在循环之外,你再也无法到达它。另外,将选项放置在循环中,以便每次都显示它们。r6hnlfcb2#
把这个改成这样:
如果你移动
keyboard.nextInt()
呼叫内部do
循环它现在将反复请求用户输入。您在循环之前有这行代码,所以它只处理一次。