Java System.out.print(“否!请重试”); } }*/ } 而(选择6); { System.out.println(“1.加法问题”); System.out.println(“2.减法问题”); System.out.println(“3.乘法问题”); System.out.println(“4.划分问题”); System.out.println(“5.退出程序”); System.out.print(“欢迎!请选择:”); //choice=input.nextInt(); } } }
开关中的Case语句将继续打开,除非您放入Java System.out.print(“否!请重试”); } }*/ } 而(选择6); { System.out.println(“1.加法问题”); System.out.println(“2.减法问题”); System.out.println(“3.乘法问题”); System.out.println(“4.划分问题”); System.out.println(“5.退出程序”); System.out.print(“欢迎!请选择:”); //choice=input.nextInt(); } } },java,if-statement,while-loop,switch-statement,do-while,Java,If Statement,While Loop,Switch Statement,Do While,开关中的Case语句将继续打开,除非您放入break/return/etc.语句以通知程序转到其他位置 例如: switch(number) { case 0: { // do stuff } case 1: { // If number was 0 this case would still be entered! // The program "fell through" the end of case 0 and go
break
/return
/etc.语句以通知程序转到其他位置
例如:
switch(number) {
case 0: {
// do stuff
}
case 1: {
// If number was 0 this case would still be entered!
// The program "fell through" the end of case 0 and got here
}
...
}
这称为开关故障排除。有时看起来很烦人,但它确实有它的用途:
switch(number) {
case 0:
case 1:
case 2: {
// Do something if number is 0, 1, or 2
}
...
}
为避免出错,请在案例末尾添加break
语句。这将导致程序跳转到开关
语句的末尾:
switch(number) {
case 0: {
// do stuff
break; // <-- Now if number is 0 case 1 won't be entered!
}
case 1: {
// Only entered if number == 1 now
}
...
}
另外,当验证用户的输入是否是选项之一时,应该使用do while循环。按照现在的方式,如果我输入两次无效值,代码仍然会继续运行。还有
菜单()
方法;为什么不使用它来代替所有的System.out.println()
调用?当使用Scanner检索int时,请记住执行nextLine()以前进到下一行。您的case语句将相互冲突,因为您没有break语句。一般来说,想想循环是如何执行语句并简化事情的。例如(我只实现了add):
不知道为什么投票要关闭或关闭。当然,调试器会有所帮助。
while(选项!=5)代码>?谢谢你,先生,这个解释对我帮助很大。我感谢你的帮助!非常感谢。这也帮助了我。
switch(number) {
case 0: {
// do stuff
break; // <-- Now if number is 0 case 1 won't be entered!
}
case 1: {
// Only entered if number == 1 now
}
...
}
int[] arr; // conventional way of declaring arrays in Java
int arr[]; // works, but doesn't follow convention. Usually not a big deal, but still...
import java.util.Scanner;
import java.util.Random;
public class MathTutor {
private static Random obj = new Random();
public static int[] randomNumbers(){
int arr[] = new int[2]; //init array to hold 2 numbers
int x = obj.nextInt(99);
int y = obj.nextInt(99);
arr[0] = Math.max(x,y);
arr[1] = Math.min(x, y);
return arr;
}
public static void menu(){
System.out.println("1. Addition Problem");
System.out.println("2. Subtraction Problem");
System.out.println("3. Multiplication Problem");
System.out.println("4. Division Problem");
System.out.println("5. Exit Program");
System.out.print("Welcome! Please make a selection: ");
}
public static void main(String[] args) {
int choice, answer;
int[] number;
Scanner input = new Scanner(System.in);
do {
menu();
//get user's choice
choice = input.nextInt();
input.nextLine(); //ignore the rest of the line
switch (choice)
{
case 1:
number = randomNumbers();
do {
System.out.print("What's " +number[0]+ " plus " +number[1]+ "?\n");
answer = input.nextInt();
input.nextLine();
if (answer != (number[0]+number[1]))
System.out.println("No! Please try again.");
} while (answer != (number[0]+number[1]));
System.out.println("\nCorrect!");
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
System.err.println("*** invalid selection ***");
break;
}
} while (choice != 5);
input.close();
System.out.println("bye.");
}
}