我需要根据用户输入重复这个java代码,但我无法使用目前为止的代码重复它
我需要根据用户输入重复这个java代码,但我无法使用到目前为止的代码重复它。我不应该使用扫描器和主类以外的任何其他导入。这是因为我们使用作为编译器,因为我们是一个基本类。当我运行代码时,它应该询问10个随机加减,并且应该询问用户是否想继续。当输入1继续时,它应该再问10个问题。但是,在运行此代码时,它会在第一个问题之后停止我需要根据用户输入重复这个java代码,但我无法使用目前为止的代码重复它,java,Java,我需要根据用户输入重复这个java代码,但我无法使用到目前为止的代码重复它。我不应该使用扫描器和主类以外的任何其他导入。这是因为我们使用作为编译器,因为我们是一个基本类。当我运行代码时,它应该询问10个随机加减,并且应该询问用户是否想继续。当输入1继续时,它应该再问10个问题。但是,在运行此代码时,它会在第一个问题之后停止 import java.util.Scanner; class Main { public static void main(String[] args) { S
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Answer the following questions.");
Scanner in = new Scanner(System.in);
int A = 0;
int N = 10;
int n = 0;
int H = 0;
boolean p = true;
while (p){
int R1 = (int)(Math.random() * 50 + 1);
int R2 = (int)(Math.random() * 999 + 1);
int R3 = (int)(Math.random() * 999 + 1);
if(R1>25){
System.out.println( "" + R2 + " + " + R3);
A = in.nextInt();
if (A == (R2 + R3))
System.out.println("Correct");
else
System.out.println("Incorrect");
}
if(R1<25){
System.out.println( "" + R2 + " - " + R3);
A = in.nextInt();
if (A == (R2 - R3))
System.out.println("Correct");
else
System.out.println("Incorrect");}
N--;
if (N==0)
p = false;
continue;
}System.out.println("Do you want ot continue? Put 1 for yes, 2 for no.");
H = in.nextInt();
if (H==1)
p=true;
else
p=false;
while (N>0);
}
}
import java.util.Scanner;
班长{
公共静态void main(字符串[]args){
System.out.println(“回答以下问题”);
扫描仪输入=新扫描仪(系统输入);
int A=0;
int N=10;
int n=0;
int H=0;
布尔p=真;
while(p){
int R1=(int)(Math.random()*50+1);
int R2=(int)(Math.random()*999+1);
int R3=(int)(Math.random()*999+1);
如果(R1>25){
System.out.println(“+R2+”+“+R3”);
A=in.nextInt();
如果(A==(R2+R3))
系统输出打印项次(“正确”);
其他的
系统输出打印项次(“不正确”);
}
if(R10);
}
}
这就是为什么你在系统输出.println(“你想继续吗?输入1表示是,2表示否”);
退出而。
我建议使用do while
而不是while
。因此,您只需将问题放入循环do while
中即可
System.out.println("Answer the following questions.");
Scanner in = new Scanner(System.in);
int A = 0;
int N = 10;
int H = 0;
boolean p = true;
do{
int R1 = (int) (Math.random() * 50 + 1);
int R2 = (int) (Math.random() * 999 + 1);
int R3 = (int) (Math.random() * 999 + 1);
if (R1 > 25) {
System.out.println("" + R2 + " + " + R3);
A = in.nextInt();
if (A == (R2 + R3)) {
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
if (R1 < 25) {
System.out.println("" + R2 + " - " + R3);
A = in.nextInt();
if (A == (R2 - R3)) {
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
N--;
System.out.println("Do you want ot continue? Put 1 for yes, 2 for no.");
H = in.nextInt();
if (H == 1) {
p = true;
} else {
p = false;
}
if (N == 0) {
p = false;
System.out.println("You have reached your max attempts.")
}
}while (N > 0 && p);
System.out.println(“回答以下问题”);
扫描仪输入=新扫描仪(系统输入);
int A=0;
int N=10;
int H=0;
布尔p=真;
做{
int R1=(int)(Math.random()*50+1);
int R2=(int)(Math.random()*999+1);
int R3=(int)(Math.random()*999+1);
如果(R1>25){
System.out.println(“+R2+”+“+R3”);
A=in.nextInt();
如果(A==(R2+R3)){
系统输出打印项次(“正确”);
}否则{
系统输出打印项次(“不正确”);
}
}
如果(R1<25){
系统输出打印项次(“+R2+”-“+R3”);
A=in.nextInt();
如果(A==(R2-R3)){
系统输出打印项次(“正确”);
}否则{
系统输出打印项次(“不正确”);
}
}
N--;
System.out.println(“是否继续?输入1表示是,输入2表示否”);
H=in.nextInt();
如果(H==1){
p=真;
}否则{
p=假;
}
如果(N==0){
p=假;
System.out.println(“您已达到最大尝试次数。”)
}
}而(N>0&p);
while(N>0)时,你用这个做什么
语句?请修正代码的缩进,这样代码中的错误就更容易被发现。请给变量取正确的名称。用编写代码的方式阅读和理解代码是不必要的困难。如果变量有特定的用途,那么就称它为specificPurposeFullfiller
而不是a
。你真的应该be给你的变量起描述性的名字。我建议用伪代码写你的问题,然后在此基础上编程。你能用你写的伪代码发回吗?如果你能,我愿意提供更多帮助。像上面的人一样,你需要给你的变量起一个清晰的描述性的名字。这是强烈建议的,因为在做了大量的c在ode中,许多变量的名称毫无意义会让人感到困惑。