2013-11-22 181 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

相关问题