我是新手,有点像Java的菜鸟,我试图制作一个计算器,没有测试一切,看它是否有效,但我遇到了问题。我似乎无法弄清楚如何在执行一次计算后返回主菜单。我最后添加了这个问题,以提示用户退出或继续返回主菜单。我只是不知道在if(whatnow == Y)中放入什么{wtf我应该回到主菜单? }。对不起,如果它有点长或什么的,但他们真的都是一样的,所以只是跳过计算的东西。任何帮助赞赏。我是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);
}
}
}
}
P.S:我在开头编辑了结尾看起来像这样的东西(真实):
`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);`
答案 0 :(得分:1)
在用户插入N
之前,您只需要重复所写的所有内容。所以你要做的就是将所有内容放在while(true)
循环中,其最后一条指令是:
if (whatnow == 'N' || whatnow = 'n') {
System.exit(0);
}
这样,如果用户在N
或n
之外插入任何内容,循环会将他带回主菜单打印部分,因此您可能需要在{的值上添加测试{1}}与您对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);
}
}
}
}
答案 1 :(得分:0)
尝试将整个代码放在while(true)
块中,而不是尝试在最后放置while循环。最后使用if语句,您可以询问用户是否想要继续使用。使用break
关键字退出循环。
尝试像这样退出:
if(input == 'n'){break;}
这样做是因为它退出了while循环。您可以在while
块之外执行更多指令。
答案 2 :(得分:0)
您可以按如下方式更改:
您的主要方法如下:
public static void main(String[] args) {
calc();
}
然后创建一个名为calc()
的方法:
public static void calc() {
Scanner scan = new Scanner(System.in);
//the rest of your code
} else if (choice == 99) {
System.exit(0);
}
calc(); //call the method again at the end of your code
//remove your while loop
}