在无限循环中,我如何打印一个随机数会增加每一组次数(例如10次)?这是我目前的代码。
`import hsa.*;
public class MidTermProject{
public static void main(String[] args){
Console con = new Console();
int intCount;
int intRand;
int intAnswer;
int intRand2;
int intTotal;
int intSubtractTotal;
int intAnswer2;
int intRand3;
int intRand4;
int intQuestionsAsked;
int intTotalQuestions;
int intTRand;
int intTRand1;
int intTRand2;
int intTRand3;
int intTRand4;
double dblTotalScore;
double dblScore;
dblScore = 0;
intQuestionsAsked = 0;
//5 - Math Training Game
con.println("This is the Math Training Game. The Questions will increase difficulty.");
con.println("There will be 30 Questions in Total");
con.println("");
con.println("One Digits:");
con.println("");
//Loop ---------------------------------------------------------------------
for(;;){
for(intCount=0; intCount<5;intCount++){
//----------------------------------------------------------------------------
//1 DIGITS
intRand=(int)(Math.random()*9+1);
intRand2=(int)(Math.random()*9+1);
intTotal = intRand + intRand2;
intRand3 =(int)(Math.random()*9+1);
intRand4 =(int)(Math.random()*9+1);
intSubtractTotal = intRand3 - intRand4;
con.println("What is " + intRand + " + " + intRand2);
intAnswer = con.readInt();
if(intAnswer == intTotal){
con.println("Correct");
con.println("");
//Add score
dblScore = dblScore + 1;
intQuestionsAsked = intQuestionsAsked + 1;
}else{
con.println("Wrong");
con.println("");
intQuestionsAsked = intQuestionsAsked + 1;
}
// SUBTRACTION ---------------------------------------------------------
con.println("What is " + intRand3 + " - " + intRand4);
intAnswer2 = con.readInt();
if(intAnswer2 == intSubtractTotal){
con.println("Correct");
con.println("");
//Add Score -------------------------------------------------------------
intQuestionsAsked = intQuestionsAsked + 1;
dblScore = dblScore + 1;
}else{
con.println("Wrong");
con.println("");
intQuestionsAsked = intQuestionsAsked + 1;
}
intTotalQuestions = intQuestionsAsked;
//----------------------------------------------------------
while(intTotalQuestions == 10){
intQuestionsAsked = 0;
intTRand = intRand * 10;
intTRand2 = intRand2 * 10;
intTRand3 = intRand3 * 10;
intTRand4 = intRand4 * 10;
con.println("What is " + intRand * intTRand + "+" + intTRand2 * intRand2);
intAnswer = con.readInt();
con.println("What is " + intRand2 * intTRand2 + "-" + intRand3 * intTRand3);
intAnswer2 = con.readInt();
}
//--------------------------------------------
}
}
}
}'
有人能告诉我我做错了什么以及如何修复此代码以使其正常工作?
答案 0 :(得分:0)
我的理解是你想要生成越来越高的随机数。存储先前随机生成的值。从您想要的任何范围生成新的随机数,并添加以前的数字以确保它增加。此外,由于看起来您的代码比以前更笨拙,我建议using arrays减少您拥有的所有过多实例变量。
答案 1 :(得分:0)
让程序永远运行是糟糕的设计。
话虽如此,如果您希望每次设置范围时随机数都更大,
(min + (int)(Math.random() * ((max - min) + 1)));
并在每次迭代时增加min
和max
。