Misoxeny
Misoxeny

Reputation: 93

Java while loop issues

Im brand new to java, and I'm writing this short program where you guess a number between 1 and 10. The correct number is stored as an integer. If you guess a lower number, its supposed to say "The correct number is higher", and if you guess a higher it should say "The correct number is lower". Here's what I have:

    import javax.swing.*;

public class Oppg3 {
    public static void main(String[] args) {
        int number = 7;
        int guessed = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 10"));
        while(guessed>number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a lower number."));
        while(guessed<number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a higher number."));
        JOptionPane.showMessageDialog(null, guessed + " is correct!");

    }

}

So obviously that wont work, cause if you enter a lower number it'll jump to the next one, and that will come up as correct even if its higher. So how do I fix this so that it'll check for both statements? Sorry for bad explaining. Thanks.

Upvotes: 1

Views: 257

Answers (9)

Nejc
Nejc

Reputation: 700

Use an outer while loop, with condition, if the guessed number is not equal to the actual number. In the body of the while clause you split the cases upper and lower with an if statement. The whole code would look something like this:

import javax.swing.*;

public class Oppg3 {
    public static void main(String[] args) {
        int number = 7;
        int guessed = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 10"));
        while(guessed!=number) {
            if(guessed>number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a lower number."));
            else guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a higher number."));
        } 
        JOptionPane.showMessageDialog(null, guessed + " is correct!");

    }

}

Upvotes: 0

Andrey Atapin
Andrey Atapin

Reputation: 7945

The logic is incorrect. You want a user to reenter a number every time he's wrong. That's why your loop is going to look like while(guessed != number) { ... }. Inside, you need to accept the input, validate and give an output.

I won't put the right code here, cause it's better to discover it yourself for you.

Upvotes: 0

Averroes
Averroes

Reputation: 4228

You have to use the while to loop while the user don't guess the correct number. Inside the loop you have to check if the number is higher or lower.

So the program would look something like this:

while (the user don't guess the number){
//
if (the guessed number is higher){...}
if (the guessed number is lower){...}
//
}

Upvotes: 0

Sconibulus
Sconibulus

Reputation: 732

The simplest solution is to replace your current whiles with ifs, and contain them both in a

while(guessed!=number)
{
    if(guessed>number) //stuff
    if(guessd<number) //otherstuff
}

Upvotes: 3

npinti
npinti

Reputation: 52185

If I where you I would do the following:

  • I would use a do while loop.
  • In the method body, I would check to see if the user guessed the number.
  • If yes, congratulate the user and the loop exits.
  • Else if the number is higher, I will inform the user accordingly and allow him to give another try.
  • Else, I will tell the user the number is lower and will let him go on another try.

As is, if your user gives a number which is larger on the second loop it should tell the user that the number is correct, regardless of the value, which is not the desirable behaviour.

Upvotes: 2

Erro
Erro

Reputation: 143

Replace your final line with this.

if(guessed == number){
    JOptionPane.showMessageDialog(null, guessed + " is correct!");
}

Or change it all,

bool correct = false;
while(correct == false)
{
     if(guessed == number)
     {
      enter code here
      correct = true;
     }
     else if(guessed > number)
     {
     enter code here
     }
     //check for guessed < number
}

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37566

Try this:

While (ReadNumber != guessed)
{
  //if Higher then ...show appropriate message
  //if lower then ...show appropriate message
  //ReadNumber = read Input again
}

Upvotes: 0

Jesper
Jesper

Reputation: 206786

Instead of two while loops, create one loop that goes around as long as the guessed number is not equal to the correct answer. Inside the loop, create if statements to check if the guessed number is higher or lower than the correct answer, and do the appropriate actions.

Upvotes: 1

skunkfrukt
skunkfrukt

Reputation: 1570

Try using one while loop around both statements, and if clauses for them individually.

Upvotes: 0

Related Questions