我试图使用循环开关制作计算器程序。
double firstNum, secondNum;
int operation;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the first number: ");
firstNum = keyboard.nextDouble();
System.out.print("\n");
System.out.print("Please enter the second number: ");
secondNum = keyboard.nextDouble();
System.out.print("\n");
System.out.println("Press 1 for addition");
System.out.println("Press 2 for subtraction");
System.out.println("Press 3 for multiplication");
System.out.println("Press 4 for division");
System.out.println("Press 0 to end the program");
System.out.println("\n");
System.out.print("Which operation would you like to perform? ");
operation = keyboard.nextInt();
System.out.print("\n");
switch (operation)
{
case 1:
System.out.println(firstNum + " + " + secondNum + " = " + (firstNum + secondNum));
break;
case 2:
System.out.println(firstNum + " - " + secondNum + " = " + (firstNum - secondNum));
break;
case 3:
System.out.println(firstNum + " * " + secondNum + " = " + (firstNum * secondNum));
break;
case 4:
if (secondNum ==0 )
{
System.out.println("You can't divide by zero.");
}
else
{
System.out.println(firstNum + " / " + secondNum + " = " + (firstNum / secondNum));
}
break;
default:
System.out.println("You have pressed 0. The program is now over.");
}
所以,我有这个。如果用户只想计算一件事,它工作正常。但是,我想这样做,以便在得到第一个答案后,再次提示他们进入操作(因此程序循环从&#34开始;您想要执行哪个操作?")。我希望循环继续,直到他们按0。
我该怎么做呢?我是新手。
感谢您的帮助。
答案 0 :(得分:0)
你需要使用while循环,当你准备好退出时,你会跳出while循环。请记住,switch语句中的任何中断都将应用于switch而不是while循环,因此您可能需要维护一个单独的变量,以确定是否不会中断循环。
例如:
boolean continue = true;
while(continue) {
System.out.print("Which operation would you like to perform? ");
operation = keyboard.nextInt();
System.out.print("\n");
switch (operation)
{
case 1:
System.out.println(firstNum + " + " + secondNum + " = " + (firstNum + secondNum));
break;
case 2:
System.out.println(firstNum + " - " + secondNum + " = " + (firstNum - secondNum));
break;
case 3:
System.out.println(firstNum + " * " + secondNum + " = " + (firstNum * secondNum));
break;
case 4:
if (secondNum ==0 )
{
System.out.println("You can't divide by zero.");
}
else
{
System.out.println(firstNum + " / " + secondNum + " = " + (firstNum / secondNum));
}
break;
default:
System.out.println("You have pressed 0. The program is now over.");
continue = false;
break;
}
}
这听起来像是一个家庭作业问题,所以花一些时间来理解vs for for循环等等,不要只接受这个答案并运行它。
答案 1 :(得分:0)
最佳解决方案是do-while
循环,因为您希望的操作至少发生一次
do
{
System.out.println("What operation would you like to preform?");
//do math calculating stuff
System.out.println("Would you like to do another calculation?");
}
while(scannerScan.nextInt != 0);
当您编写do
部分时,将要重复执行的所有内容放在do
括号内。在这种情况下,我将SOP和注释放在括号中,因此在nextInt() != 0
时SOP将继续打印。这样更容易阅读,并且可以消除程序中任何可能导致其他类型的循环失败的问题,从而保证程序至少运行一次。在那之后,如果它失败了,你就知道你到底在哪里,就在while
条件所在的一端。
修改
我在默认情况下检查了一个继续,它应该适合你。这样做的是,如果用户输入一个不可接受的值,则执行默认值并执行continue语句,该语句将直接进入while
条件。因此,我建议您重新构建默认情况:
do
{
int x = 0;
switch(x)
{
case 1:
System.out.println("Math ops.");
break;
case 2:
//wonderful math
break;
case 3:
//wonders of code
break;
default:
System.out.println("You entered invalid input. Try again. Enter a non-zero number to continue.");
continue;
}
}
while(scannerScan.nextInt() != 0);