提问人:Dimitar Todorov 提问时间:6/27/2023 更新时间:6/27/2023 访问量:36
在执行代码块后循环回到 Java 程序的开头
Looping back to the start of a java program after executing a block of code
问:
我写了一个Java程序,更具体地说是一个基本的计算器。我想回到“主菜单”,要求用户在解决数学问题后输入“0”。我想出的解决方案似乎没有按照我想要的方式工作。返回的概念是有一个变量“pick”,它位于程序的开头,并且尚未分配任何内容。程序询问用户他要执行什么操作,每个操作都被分配一个数字,整个事情是在 while/do 循环的帮助下执行的。循环询问用户在“pick”为 0 时要执行什么操作。例如,如果用户选择正在求解二次方程的操作编号 2,则变量“pick”的值将变为 2,并且如果变量 pick 等于 2,则激活一个 if,并且求解二次方程的代码块开始执行。求解二次方程后,程序会询问用户是否要按 0 返回“主菜单”,我以为如果变量“pick”的值再次为 0,程序将重新开始,但似乎不是,这就是我写这篇文章的原因。
这是代码:
import java.util.Objects;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String pick_String;
int pick;
do {
pick_String = JOptionPane.showInputDialog("Which tool would you like to use? (Type just the number) " +
"\n1.Basic Calculator" +
"\n2.Solve a quadratic equation" +
"\n3.Find a side in a right triangle" +
"\n4.Find the distance between two points in a coordinate system" +
"\n5.Exit"
);
pick = Integer.parseInt(pick_String);
} while (pick == 0);
if (pick == 1) {
//code for the basic calculator
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 2) {
//code for solving a the quadratic equation
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 3) {
//code for finding a side in a right triangle
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 4) {
//code for finding the distance between two points in a coordinate system
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 5) {
System.exit(0);
}
}
}
我认为它可能不起作用,因为程序是从顶部执行的,然后一直到底部,并且在执行“主菜单”后无法再次执行,但我不确定。
答:
你有一个循环,只要输入是 0,它就会不断要求输入,但你没有任何 if 条件会在你稍后键入 0 时再次运行循环。像这样,你不能通过键入 0 再次运行循环。在执行其中一项计算后键入 0 的效果将与键入大于 5 或小于 1 的任何其他数字的效果相同。 如果要在键入 0 时运行主菜单循环,则应为此添加 if 条件,例如:
if (pick == 0) {
//the main menu loop
}
此外,由于 if-else-block 之后没有任何内容,因此程序将在您执行下一个输入后结束。如果你想让它运行多次,你必须循环程序。我建议将 do-while-loop 变成 while-loop,然后在 while (true) - 循环中运行整个代码:
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String pick_String;
int pick = 0;
while (true) {
if (pick == 0) {
while (pick==0) {
pick_String = JOptionPane.showInputDialog("Which tool would you like to use? (Type just the number) " +
"\n1.Basic Calculator" +
"\n2.Solve a quadratic equation" +
"\n3.Find a side in a right triangle" +
"\n4.Find the distance between two points in a coordinate system" +
"\n5.Exit");
pick = Integer.parseInt(pick_String);
}
}
if (pick == 1) {
//code for the basic calculator
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 2) {
//code for solving a the quadratic equation
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 3) {
//code for finding a side in a right triangle
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 4) {
//code for finding the distance between two points in a coordinate system
System.out.println("If you want to get back to the menu type 0");
pick = scanner.nextInt();
} else if (pick == 5) {
System.exit(0); // this will exit while(true) along with the program
}
}
}
评论