-2

In an infinite loop, how would I print a random number that will increase every set number of times( example 10 times )? Here is my code at the moment.

`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();

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

Could anyone tell me what I did wrong and how I could fix this code to make it work?

2 Answers2

0

What I understand is that you want to generate random numbers that get increasingly higher. Store the previous random generated value. Generate your new random number from whatever range you want, and add the previous number to ensure it increases. Also, as it appears your code is way clunkier than it has to be, I would suggest using arrays to cut down on all of the excessive instance variables you have.

Connor Neville
  • 7,291
  • 4
  • 28
  • 44
0

Having a program run forever is bad design.

That being said, if you want your random numbers to be of greater values every time you can set a range

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

and increase min and max every iteration.

user1231232141214124
  • 1,339
  • 3
  • 16
  • 22
  • what possible reason would you have have for that – user1231232141214124 Nov 22 '13 at 15:32
  • It's for a project which needs to be run forever. – user3013760 Nov 22 '13 at 15:34
  • ok well here is your answer, im not going to write your code for you, you should be able to figure it out – user1231232141214124 Nov 22 '13 at 15:34
  • Also, i'd take the time to learn how to clean up your code, I'd fail anyone who wasn't taking programming 101 with code like that. Not trying to be nasty, just honest. – user1231232141214124 Nov 22 '13 at 15:35
  • Well i just started learning to program , so sorry if im not somehow professional standard.. – user3013760 Nov 22 '13 at 15:39
  • All im saying is take some time an read a bit especially seeing as this question has been asked many times before, with much less guess work in what the original poster was asking. – user1231232141214124 Nov 22 '13 at 15:40
  • @user3013760 I think most importantly, you need to ask a smaller question. Don't dump your whole project as code into a question, explain what a small portion of it needs to do, and expect a good answer. Post code directly relevant to the question being asked, and as little extra code as possible. http://sscce.org/ – Cruncher Nov 22 '13 at 15:42