我如何让我的程序在提出指定数量的问题后停止提问? import java.util.Scanner; 公共课数学实践{ 公共静态void main(字符串[]args){ //卢克·米哈洛维奇 扫描仪键盘=新扫描仪(System.in); int questions=getNumberOfQuestions(); int难度=GetQuestionDemobility(); while(问题>0){ intrandom1=(int)(9*Math.random())+1; intrandom2=(int)(9*Math.random())+1; int randomSign=(int)(4*Math.random())+1; int answerAdd; int rightad; 国际答复小组; int rightSub; 国际答复; int rightMul; 国际答复司; int rightDiv; 如果(随机符号==1){ 系统输出打印(“问题#”+问题+“什么是“+random1+”+“+random2+”?”); answerAdd=keyboard.nextInt(); rightAdd=random1+random2; 如果(answerAdd==rightAdd){ System.out.println(“正确!”); }else if(answerAdd!=rightad){ System.out.print(“错误…答案是“+rightAdd”); } } 如果(随机符号==2){ 系统输出打印(“问题#”+问题+“什么是”+random1+“-“+random2+”?”); answerSub=keyboard.nextInt(); rightSub=random1-random2; if(answerSub==rightSub){ 系统输出打印(“正确!”); }else if(answerSub!=rightSub){ System.out.print(“错误……答案是“+rightSub”); } } 如果(随机符号==3){ 系统输出打印(“问题#“+问题+”什么是“+random1+”*“+random2+”?”; answerMul=keyboard.nextInt(); rightMul=random1*random2; if(answerMul==rightMul){ 系统输出打印(“正确!”); }else if(answerMul!=rightMul){ System.out.print(“错误……答案是“+rightMul”); } } 如果(随机符号==4){ 系统输出打印(“问题#”+问题+“什么是“+random1+”/“+random2+”?”); answerDiv=keyboard.nextInt(); rightDiv=random1/random2; if(answerDiv==rightDiv){ 系统输出打印(“正确!”); }else if(answerDiv!=rightDiv){ System.out.print(“错误……答案是“+rightDiv”); } } 问题++; } } 公共静态int getNumberOfQuestions(){ 扫描仪键盘=新扫描仪(System.in); 智力问题=0; System.out.print(“您想要多少问题?”); 问题=键盘.nextInt(); while(问题1 |难度>2){ System.out.println(“有效级别为1或2。请重新输入”); System.out.print(“您想要什么难度级别(1=低或2=高)?”; 难度=键盘.nextInt(); } 返回困难; } }
我的程序应该向用户询问他们想问的问题的数量,然后程序应该询问许多随机生成的数学问题。我需要在我的程序中修正什么,才能在达到这么多问题后停止提问?还有,我如何让我的问题从#1开始?谢谢您可以为循环使用我如何让我的程序在提出指定数量的问题后停止提问? import java.util.Scanner; 公共课数学实践{ 公共静态void main(字符串[]args){ //卢克·米哈洛维奇 扫描仪键盘=新扫描仪(System.in); int questions=getNumberOfQuestions(); int难度=GetQuestionDemobility(); while(问题>0){ intrandom1=(int)(9*Math.random())+1; intrandom2=(int)(9*Math.random())+1; int randomSign=(int)(4*Math.random())+1; int answerAdd; int rightad; 国际答复小组; int rightSub; 国际答复; int rightMul; 国际答复司; int rightDiv; 如果(随机符号==1){ 系统输出打印(“问题#”+问题+“什么是“+random1+”+“+random2+”?”); answerAdd=keyboard.nextInt(); rightAdd=random1+random2; 如果(answerAdd==rightAdd){ System.out.println(“正确!”); }else if(answerAdd!=rightad){ System.out.print(“错误…答案是“+rightAdd”); } } 如果(随机符号==2){ 系统输出打印(“问题#”+问题+“什么是”+random1+“-“+random2+”?”); answerSub=keyboard.nextInt(); rightSub=random1-random2; if(answerSub==rightSub){ 系统输出打印(“正确!”); }else if(answerSub!=rightSub){ System.out.print(“错误……答案是“+rightSub”); } } 如果(随机符号==3){ 系统输出打印(“问题#“+问题+”什么是“+random1+”*“+random2+”?”; answerMul=keyboard.nextInt(); rightMul=random1*random2; if(answerMul==rightMul){ 系统输出打印(“正确!”); }else if(answerMul!=rightMul){ System.out.print(“错误……答案是“+rightMul”); } } 如果(随机符号==4){ 系统输出打印(“问题#”+问题+“什么是“+random1+”/“+random2+”?”); answerDiv=keyboard.nextInt(); rightDiv=random1/random2; if(answerDiv==rightDiv){ 系统输出打印(“正确!”); }else if(answerDiv!=rightDiv){ System.out.print(“错误……答案是“+rightDiv”); } } 问题++; } } 公共静态int getNumberOfQuestions(){ 扫描仪键盘=新扫描仪(System.in); 智力问题=0; System.out.print(“您想要多少问题?”); 问题=键盘.nextInt(); while(问题1 |难度>2){ System.out.println(“有效级别为1或2。请重新输入”); System.out.print(“您想要什么难度级别(1=低或2=高)?”; 难度=键盘.nextInt(); } 返回困难; } },java,Java,我的程序应该向用户询问他们想问的问题的数量,然后程序应该询问许多随机生成的数学问题。我需要在我的程序中修正什么,才能在达到这么多问题后停止提问?还有,我如何让我的问题从#1开始?谢谢您可以为循环使用 替换 import java.util.Scanner; public class MathPractice { public static void main(String[] args) { // Luke Mihalovich Scanner k
替换
import java.util.Scanner;
public class MathPractice {
public static void main(String[] args) {
// Luke Mihalovich
Scanner keyboard = new Scanner(System.in);
int questions = getNumberOfQuestions();
int difficulty = getQuestionDifficulty();
while (questions > 0) {
int random1 = (int)(9 * Math.random()) + 1;
int random2 = (int)(9 * Math.random()) + 1;
int randomSign = (int)(4 * Math.random()) + 1;
int answerAdd;
int rightAdd;
int answerSub;
int rightSub;
int answerMul;
int rightMul;
int answerDiv;
int rightDiv;
if(randomSign == 1) {
System.out.print("Question #" + questions + " What is " + random1 + " + " + random2 + "? ");
answerAdd = keyboard.nextInt();
rightAdd = random1 + random2;
if (answerAdd == rightAdd) {
System.out.println("Correct!");
} else if (answerAdd != rightAdd) {
System.out.print("Wrong... The answer is " + rightAdd);
}
}
if(randomSign == 2) {
System.out.print("Question #" + questions + " What is " + random1 + " - " + random2 + "? ");
answerSub = keyboard.nextInt();
rightSub = random1 - random2;
if (answerSub == rightSub) {
System.out.print("Correct!");
} else if (answerSub != rightSub) {
System.out.print("Wrong... The answer is " + rightSub);
}
}
if(randomSign == 3) {
System.out.print("Question #" + questions + " What is " + random1 + " * " + random2 + "? ");
answerMul = keyboard.nextInt();
rightMul = random1 * random2;
if (answerMul == rightMul) {
System.out.print("Correct!");
} else if (answerMul != rightMul) {
System.out.print("Wrong... The answer is " + rightMul);
}
}
if(randomSign == 4) {
System.out.print("Question #" + questions + " What is " + random1 + " / " + random2 + "? ");
answerDiv = keyboard.nextInt();
rightDiv = random1 / random2;
if (answerDiv == rightDiv) {
System.out.print("Correct!");
} else if (answerDiv != rightDiv) {
System.out.print("Wrong... The answer is " + rightDiv);
}
}
questions++;
}
}
public static int getNumberOfQuestions() {
Scanner keyboard = new Scanner(System.in);
int questions = 0;
System.out.print("How many questions do you want? ");
questions = keyboard.nextInt();
while(questions < 1) {
System.out.println("The number of questions must be 1 or more.");
System.out.print("How many questions do you want? ");
questions = keyboard.nextInt();
}
return questions;
}
public static int getQuestionDifficulty() {
Scanner keyboard = new Scanner(System.in);
int difficulty = 0;
System.out.print("What difficulty level do you want (1=low or 2=high)? ");
difficulty = keyboard.nextInt();
while (difficulty > 1 | difficulty > 2) {
System.out.println("Valid levels are 1 or 2. Please reenter. ");
System.out.print("What difficulty level do you want (1=low or 2=high)? ");
difficulty = keyboard.nextInt();
}
return difficulty;
}
}
与
与
另外,根据您的代码注释
System.out.print("Question #" + loop + " What is " + random1 + " + " + random2 + "? ");
这是错误的,应该是错误的
while (difficulty > 1 | difficulty > 2) {
编辑
我注意到您有问题++代码>在代码中-删除它Afor
循环重复一定次数的迭代(通常)。问题——在while循环中,在while循环中使用整数计数器变量:int counter=0;while(true){counter++;System.out.println(“要问的问题…”);…}
@DevilsHnd-为什么while(true)
-它是如何中断的?@thrable Wombat-抱歉,这只是一个简单的条件示例,而不是写出问题>0
。但是…要打破这种情况,您可以使用:if(counter==questions){break;}
。for循环在达到所需问题的设置数量后仍不会停止程序,但问题的编号是固定的,谢谢。请注意,您有questions++代码>在你的代码中-删除它,其他明智的循环
将永远不会抓住卢克-发生了什么事?@ScaryWombat:恐怕帮助吸血鬼,一个继续在这里丢作业的人。1+用于尝试
System.out.print("Question #" + questions + " What is " + random1 + " + " + random2 + "? ");
System.out.print("Question #" + loop + " What is " + random1 + " + " + random2 + "? ");
while (difficulty > 1 | difficulty > 2) {
while (difficulty != 1 && difficulty != 2) {