返回Java代码的开始(计算器的主菜单)
我是新来的,对Java有点不熟悉,我正在尝试制作一个计算器,没有测试所有的东西看它是否工作,但我遇到了一个问题。我似乎不知道在执行一次计算后如何返回主菜单。我在最后添加了这个问题,以提示用户退出或继续返回主菜单。我只是不知道在if(whatnow==Y){wtf我应该怎么做才能回到主菜单??}中放什么。抱歉,如果有点长或者什么的话,但是它们实际上是一样的,所以跳过计算。谢谢你的帮助。我对java真的很陌生,我可能不得不重新编写这段代码返回Java代码的开始(计算器的主菜单),java,return,calculator,Java,Return,Calculator,我是新来的,对Java有点不熟悉,我正在尝试制作一个计算器,没有测试所有的东西看它是否工作,但我遇到了一个问题。我似乎不知道在执行一次计算后如何返回主菜单。我在最后添加了这个问题,以提示用户退出或继续返回主菜单。我只是不知道在if(whatnow==Y){wtf我应该怎么做才能回到主菜单??}中放什么。抱歉,如果有点长或者什么的话,但是它们实际上是一样的,所以跳过计算。谢谢你的帮助。我对java真的很陌生,我可能不得不重新编写这段代码 package practice; import 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循环,而是尝试将整个代码放在
while(true)
块中。最后使用if语句,您可以询问用户是否希望继续我们的操作。使用break
关键字退出循环。
尝试这样退出:
if(input == 'n'){break;}
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);
}
}
}
}
它的作用是退出while循环。您可以在
while
块之外执行更多指令。您只需重复所写的所有内容,直到用户插入N
。因此,您只需将所有内容放入while(true)
循环中,该循环的最后一条指令是:
if (whatnow == 'N' || whatnow = 'n') {
System.exit(0);
}
这样,如果用户插入除N
或N
之外的任何内容,循环将使他返回主菜单打印部分,因此您可能需要添加一个关于whatnow
值的测试,方法与对选项的测试相同
结果如下:
if(input == 'n'){break;}
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));
}
}
publicstaticvoidmain(字符串[]args){
char whatnow='Y';
扫描仪扫描=新扫描仪(System.in);
whatnow!='N'&&whatnow!='N'){
int choice=printmmenuandask(扫描);
如果(选项==99)
打破
其他性能操作(选择、扫描);
System.out.println(“是否继续计算?[Y/N]:”;
whatnow=scan.next().charAt(0);
而(whatnow!='N'&&whatnow!='Y'&&whatnow!='N'&&whatnow!='Y'){
System.out.println(“回答不正确”);
whatnow=scan.next().charAt(0);
}
}
scan.close();
}
公共静态int printMenuAndAsk(扫描仪扫描){
智力选择;
System.out.println(“欢迎使用StemCalc Z版(仅限整数)!”;
...
System.out.println(“输入您的选择[1-4或99]:”;
choice=scan.nextInt();
while((选项<1 | |选项>4)和&choice!=99){
System.out.println(“请输入1、2、3、4或99:”;
choice=scan.nextInt();
}
回报选择;
}
公共静态无效性能操作(int操作、扫描仪扫描){
System.out.println(“先输入:”;
int firstnumber=scan.nextInt();
System.out.println(“输入秒:”);
int secondnumber=scan.nextInt();
如果(选项==1)
System.out.println(firstnumber+“+”+secondnumber+“=”+(firstnumber+secondnumber));
else if(选项==2)
System.out.println(firstnumber+“-”+secondnumber+“=”+(firstnumber-secondnumber));
else if(选项==3)
System.out.println(firstnumber+“*”+secondnumber+“=”+(firstnumber*secondnumber));
else if(选项==4){
while(secondnumber==0){
System.out.println(“ERROR-CANNOT DIVIDE TO ZERO!键入另一个整数:”);
secondnumber=scan.nextInt();
}
System.out.println(firstnumber+“/”+secondnumber+“=”+(firstnumber/secondnumber));
}
}
您可以按如下方式更改它:
您的主要方法如下:
public static void main(String[] args) {
calc();
}
然后创建一个名为calc()
的方法:
将所有代码移到另一个方法中,并在需要返回开始时调用该方法。见鬼,我甚至不知道这是不是一种方法:PYes,这是一种方法。你应该再做一个像它一样的,给它起个像样的名字,然后在你需要回去的时候从内部调用它。周围应该有足够的信息,这样你可以从我们的对话中搜索关键词,了解你需要做什么。嗨,Ubuntu4EVA,请不要在问题中添加答案和[已解决]。您可以在回答部分回答您的问题并接受答案(如果您想更改已接受的答案),我不建议使用函数,因为您已经说过您是Java新手。您能在最后举一个if语句的例子吗?我见过其他人把while(真的)东西放在后面,然后把它弄错或诸如此类的东西。但这能像我想的那样工作吗?询问用户在计算后是要退出还是继续?我添加了while(true),但不知道如何处理末尾的while循环,所以我只将Y留空,N留空;就像你说的。然后,当我在Eclipse的控制台上运行它时,当我选择Y时,它会重复这个问题(Y不是?),但当我选择N时,它实际上会因为一些愚蠢的原因返回菜单。。。。我是一个noob,我很困惑你是否试图在代码中的第一个while循环中输入99以退出程序?我输入了99,它正常终止做什么!='你的工作?用于循环以确保值为Y或N?如果是的话,我们是否需要Y的if语句?您能否查看我的更改?刚刚编辑了原始问题,我想说whatnow!=“N”&&whatnow!=“Y”){System.out.println(“错误答案”);whatnow=scan.next().charAt(0);}
。完成后,whatnow
将是'N'(在本例中我们退出)或'Y'(在本例中,我们不必做任何事情,因为我们处于循环的末尾,我们将被带到循环的开始),因此我们解决了您所写内容中的问题