2013-11-22 129 views
-2

在一個無限循環中,我將如何打印一個隨機數,每增加一次(例如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(); 

    }   
    //--------------------------------------------   
    } 
} 
}  
}' 

有人能告訴我我做錯了什麼,我該如何修復此代碼才能使其工作?

+3

這是不是很清楚你要求 – Cruncher

+0

增加隨機數量? – redFIVE

+1

此外,我確定宣佈16個整數不是必需的 – Cruncher

回答

0

我的理解是,你想生成隨機數越來越高。存儲以前的隨機生成值。從您想要的任何範圍生成新的隨機數,並添加以前的數字以確保增加。另外,由於看起來你的代碼比它的笨拙,所以我建議using arrays減少你擁有的所有過量實例變量。

0

使程序永遠運行是糟糕的設計。

話雖這麼說,如果你想您的每一次隨機數是更大的價值,你可以設置一個範圍

(min + (int)(Math.random() * ((max - min) + 1)));

,增加minmax每次迭代。

+0

重點是讓程序永遠運行。 – user3013760

+0

你有什麼可能的原因 – redFIVE

+0

這是一個需要永久運行的項目。 – user3013760

相關問題