我想知道如何从 switch 语句初始化 int c 。我不知道如何将随机开关参数“提取”到最终答案中。我是初学者,所以也许我在这里遗漏了一些明显的东西。这是我到目前为止的代码:
import java.util.*;
public class whileTest{
public static void main (String args[]){
Scanner sc = new Scanner(System.in);
String a;
do{
String operatorSwitch;
int b;
int c;
Random number = new Random();
int d = number.nextInt(100) +1;
int e = number.nextInt(100) +1;
Random operatorChoice = new Random();
int operator = operatorChoice.nextInt(4);
switch (operator){
case 0: operatorSwitch= "+";
c = d+e;
break;
case 1: operatorSwitch= "-";
c = d-e;
break;
case 2: operatorSwitch= "*";
c = d*e;
break;
case 3: operatorSwitch= "/";
c = d/e;
break;
default: operatorSwitch= "";
}
System.out.println("What is: "+d+operatorSwitch+e+"?");
b = sc.nextInt();
if(b!=c)
System.out.println("Wrong answer! Right answer is: "+c);
else{if(b==c)
System.out.println("Right answer!"+c);
}
System.out.println("Continue? y/n");
a = sc.next();
}while(a.equals("y"));
}
}