Making a code where the user has to guess a number between 1 and 100, but my loop isn’t working

import java.util.Random;
import java.util.Scanner;

public class spele {

    public static void main(String[] args) {
        
        Scanner LENIJS = new Scanner(System.in);
        System.out.println("Ievadi skaitli no 1-100 ");
        int skai = LENIJS.nextInt();
       
        
        Random r = new Random();
        int zem = 1;
        int aug = 100;
        
        int a = r.nextInt(aug-zem) + zem;
        do {
            System.out.println(a);
        } while(skai == a);
    }

}

When i try to put the input in the loop it doesn’t work, I’d like it to be that u can write as many times as u want until u guess the number. While outputing if the number u guessed is right or wrong(will use the if function probably), but the loop isn’t working.

Answer

The problem is that you don’t ask for additional input inside of the do-while loop.

import java.util.Random;
import java.util.Scanner;

public class spele {

    public static void main(String[] args) {
        
        Scanner LENIJS = new Scanner(System.in);
        
       
        
        Random r = new Random();
        int zem = 1;
        int aug = 100;
        
        int a = r.nextInt(aug-zem) + zem;
        System.out.println("The number computer generated is " + a); //delete this line in production

        System.out.println("Ievadi skaitli no 1-100 ");
        int skai;
        do {
            skai = LENIJS.nextInt();
            if (skai != a){
                System.out.println("Wrong!");
            }
        } while(skai != a);


        System.out.println("Correct!");
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *