我是Java的新手,对Java来说是个菜鸟,而且我正在尝试制作一个计算器,并未测试所有工具是否正常工作,但是我遇到了问题。我似乎无法弄清楚执行一次计算后如何返回主菜单。我在最后添加了该问题,以提示用户退出或继续返回主菜单。我只是不知道要在if(whatnow == Y){中放什么,我应该怎么做才能返回主菜单?? }。很抱歉,如果它有点长,但实际上它们都是一样的,所以就跳过计算吧。任何帮助表示赞赏。我对Java真的很陌生,可能必须重新编写这段代码。
package practice;
import java.util.Scanner;
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int choice;
int firstnumber;
int secondnumber;
int result;
char whatnow;
System.out.println("Welcome to StemCalc Z Edition(Integers only)!");
System.out.println("Made with love and basic Java");
System.out.println("Which math operation would you like to perform?");
System.out.println("");
System.out.println("WARNING: Enter the integer x, press ENTER, then enter y");
System.out.println("");
System.out.println("[1]-Addition (+)");
System.out.println("[2]-Subtraction (-)");
System.out.println("[3]-Multiplication (x)");
System.out.println("[4]-Division (/)");
System.out.println("");
System.out.println("Enter your choice[1-4 or 99]:"); choice = scan.nextInt();
while ((choice < 1 || choice > 4) && choice != 99) {
System.out.println("Please enter 1, 2, 3, 4, or 99: ");
choice = scan.nextInt();
}
if (choice == 1){
System.out.println("Enter two integer to add(x + y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
result = firstnumber + secondnumber;
System.out.println(firstnumber + " + " + secondnumber + " = " + result);
}
else if (choice == 2) {
System.out.println("Enter two integers to subtract(x - y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
result = firstnumber - secondnumber;
System.out.println(firstnumber + " - " + secondnumber + " = " + result);
}
else if (choice == 3) {
System.out.println("Enter two integers to multiply(x * y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
result = firstnumber * secondnumber;
System.out.println(firstnumber + " * " + secondnumber + " = " + result);
}
else if (choice == 4) {
System.out.println("Enter to integers to divide(x / y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
while (secondnumber == 0) {
System.out.println("ERROR-CANNOT DIVIDE TO ZERO! Type another integer:");
secondnumber = scan.nextInt();
}
result = firstnumber / secondnumber;
System.out.println(firstnumber + " / " + secondnumber + " = " + result);
}
else if (choice == 99) {
System.exit(0);
}
while (choice !=99) {
System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);
if (whatnow == 'Y' || whatnow == 'y') {
}
if (whatnow == 'N' || whatnow == 'n') {
System.exit(0);
}
}
}
}
附注:我编辑了结尾,使开头看起来像while(true):
`while (choice !=99) {
System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);
while(whatnow != 'Y' || whatnow != 'y' || whatnow !='N' || whatnow !='n') {
System.out.println("Enter [Y/N] only:"); whatnow = scan.next().charAt(0);
}
if (whatnow == 'N' || whatnow == 'n') {
System.exit(0);`
最佳答案
您只需要重复编写的所有内容,直到用户插入N
。因此,您要做的就是将所有内容放入while(true)
循环中,其最后一条指令将是:
if (whatnow == 'N' || whatnow = 'n') {
System.exit(0);
}
这样,如果用户在
N
或n
之外插入任何内容,循环将把他带回到主菜单打印部分,因此也许您需要以与您相同的方式对whatnow
的值进行测试为choice
做过。结果将是这样的:
public static void main(String[] args){
...
while(true){
System.out.println("Welcome to StemCalc Z Edition(Integers only)!");
...
while (choice !=99) {
System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);
//insert some loop to ensure that the value of whatnow will be either Y or N
if (whatnow == 'N' || whatnow == 'n') {
System.exit(0);
}
}
}
}
编辑
这是我对上一条评论的预期示例代码:
public static void main(String[] args){
char whatnow = 'Y';
Scanner scan = new Scanner(System.in);
while (whatnow != 'N' && whatnow != 'n') {
int choice = printMenuAndAsk(scan);
if (choice == 99)
break;
else performOperation(choice, scan);
System.out.println("Do you want to continue calculating? [Y/N]:");
whatnow = scan.next().charAt(0);
while(whatnow != 'N' && whatnow != 'Y' && whatnow != 'n' && whatnow != 'y') {
System.out.println("Incorrect answer");
whatnow = scan.next().charAt(0);
}
}
scan.close();
}
public static int printMenuAndAsk(Scanner scan) {
int choice;
System.out.println("Welcome to StemCalc Z Edition(Integers only)!");
...
System.out.println("Enter your choice[1-4 or 99]:");
choice = scan.nextInt();
while ((choice < 1 || choice > 4) && choice != 99) {
System.out.println("Please enter 1, 2, 3, 4, or 99: ");
choice = scan.nextInt();
}
return choice;
}
public static void performOperation(int operation, Scanner scan) {
System.out.println("Enter first:");
int firstnumber = scan.nextInt();
System.out.println("Enter second:");
int secondnumber = scan.nextInt();
if (choice == 1)
System.out.println(firstnumber + " + " + secondnumber + " = " + (firstnumber+secondnumber));
else if (choice == 2)
System.out.println(firstnumber + " - " + secondnumber + " = " + (firstnumber-secondnumber));
else if (choice == 3)
System.out.println(firstnumber + " * " + secondnumber + " = " + (firstnumber*secondnumber));
else if (choice == 4) {
while (secondnumber == 0) {
System.out.println("ERROR-CANNOT DIVIDE TO ZERO! Type another integer:");
secondnumber = scan.nextInt();
}
System.out.println(firstnumber + " / " + secondnumber + " = " + (firstnumber/secondnumber));
}
}