Home > Software engineering >  Continuos loop until condition is not met
Continuos loop until condition is not met

Time:11-23

Im trying to place the user in a loop if a certain condition is met: if he gets the correct answer, random questions would be generated till the user fails then their score would be added

            System.out.println("You chose easy elimination mode");
            
            firstNumbereasy = randN.nextInt(9) 2; // new first random number generated 
             secondNumbereasy= randN.nextInt(firstNumbereasy - 1); // new second random number generated
            System.out.println("What is "   firstNumbereasy   getRandomOperator()  secondNumbereasy); 
            b = scanner.nextDouble();
            c  ;
            
            
            
            
      while (c==b) {
            c = (int) (firstNumbereasy secondNumbereasy);
              c = (int) (firstNumbereasy-secondNumbereasy);
              c = (int) (firstNumbereasy*secondNumbereasy);
              c= (int)   (firstNumbereasy/secondNumbereasy); 
              
              firstNumbereasy = randN.nextInt(9) 2; // new first random number generated 
                 secondNumbereasy= randN.nextInt(firstNumbereasy - 1); // new second random number generated
                System.out.println("What is "   firstNumbereasy   getRandomOperator()  secondNumbereasy); 
                b = scanner.nextDouble();
                c  ;}

Ive even tried using a while loop , but it doesnt loop the user to another random generated question when he meets the condition (c==b)

CodePudding user response:

  1. What's up with c? You've got it incrased after the question, so I assume it was meant to be Score or something.
  2. It's (c) assigned value AFTER the if. It should be before the if, so it's never gonna change value from the one before the code frgment shown.
  3. C is set as an int and you've got clearly possibilities of division

Here's my (quickly made) code for addition only:

    System.out.println("You chose easy elimination mode");
    
    Scanner scanner = new Scanner(System.in);
    Random ran = new Random();
    int score = 0;
    while(true)
    {
        int c = 0;
        int firstNumbereasy = ran.nextInt(9) 2; // new first random number generated 
        int secondNumbereasy= ran.nextInt(firstNumbereasy - 1); // new second random number generated
        System.out.println("What is "   firstNumbereasy  " "    secondNumbereasy); 
        int b = scanner.nextInt();
        c = firstNumbereasy   secondNumbereasy;
        
        
        
        
        if (c!=b)
        {
            System.out.println("Score: "  score); 
            break;
        }
        else
        {
            score  ;14
                    
        }
    }

You can edit this code into a function that runs at the selection and, instead of set true and break statement use a boolean.

  • Related