在一個無限循環中,我將如何打印一個隨機數,每增加一次(例如10次)?這是我目前的代碼。如何在java中每增加一次隨機數就增加一次?
`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();
}
//--------------------------------------------
}
}
}
}'
有人能告訴我我做錯了什麼,我該如何修復此代碼才能使其工作?
這是不是很清楚你要求 – Cruncher
增加隨機數量? – redFIVE
此外,我確定宣佈16個整數不是必需的 – Cruncher