Chris
Chris

Reputation: 59

Java number guessing

a. declare a final int, and assign a value of 6 as the guessed number

    // b. create a Scanner to get user input

    // c. use a do {} while loop to prompt the user to enter an integer between 1 and 10,
    //    assign the user input to an int, and compare to the guessing number

    do{

    } while ();

    // d. if the number matches the guessed number,
    // print out a message that the number entered is correct.

I am stuck on the do while loop portion of the problem

import java.util.Scanner;

public class Number {
    public static void main(String[] args) {
        int value = 6;
        int guess = 0;
        int num = 0;

        do {
            System.out.println("Please enter a number between 1 and 10: ");

            Scanner number = new Scanner(System.in);
            guess = number.nextInt();
        } while (num <= 10);

        if (guess == value);
        System.out.println("Congratulations you guessed the correct number!");
        if (guess != value);
        System.out.println("The number does not match.");
    }
}

This is the result I am getting. I cannot figure out why it wont print the messages saying the number is correct or the number did not match.

Please enter a number between 1 and 10: 
4
Please enter a number between 1 and 10: 
5
Please enter a number between 1 and 10: 
6

Upvotes: 0

Views: 530

Answers (3)

DigitalJedi
DigitalJedi

Reputation: 1651

Remove the Semicolon after the ifstatement, and i strongly advise you to use {} to wrap all your statements.

Morover you should increase num inside your while loop, and move the guessing part inside your while loop. so the correct messages are printed out in every loop.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int value = 6;
        int guess = 0;
        int num = 0;


        do {
            System.out.println("Please enter a number between 1 and 10: ");

            Scanner number = new Scanner(System.in);
            guess = number.nextInt();
            num++;

            if (guess == value) {

                System.out.println("Congratulations you guessed the correct number!");
                break;
            }
            if (guess != value) {
                System.out.println("The number does not match.");
            }


        } while (num <= 10);

    }

}

Upvotes: 2

user10762593
user10762593

Reputation:

You code basically looks like this:

ask user for a number from 1 to 10
and do this forever 

now check whether the number entered is the right guess

So as long as the user does what he is told, you keep asking and reasking. You never get to "now check...".

The loop needs to encompass the checking for the right guess, and needs to be terminated on the right guess.

(Edited) I slightly misread the original code, thinking that 'num' was the input value. Nope, the input number is called 'number'. I suggest the counter be renamed for clarity, maybe 'guessCount'. And of course remember to increment it with each guess.

Upvotes: 1

baao
baao

Reputation: 73241

The semicolon after the if statement stops it from working and makes no sense

if (guess == value);
                   ^ No no no

Should be

if (guess == value) {
    System.out.println("Congratulations you guessed the correct number!");

}

Or

if (guess == value)
    System.out.println("Congratulations you guessed the correct number!");

You should also increment num or the while makes no sense

Upvotes: 2

Related Questions