For loop; Problems with my for loop
(I realize my title is bad - I forgot to go back and change it)
Hi,
I am trying to make a program that has a user try to guess a number. I'm using a for loop for this purpose. My problem is after the for loop is over, it does not run the dialog box part of the code. If I move the dialog box part above the for loop it will work.
What do I need to do in order for it to do the dialog box once the for loop is finished?
Here is this part of my code:
Code Java:
for (int i = 1; i <= 5; i++)
{
numguess = scan.nextDouble();
if (numguess == num) System.out.print ("Yay! You guessed correctly!");
else if (numguess <= num) System.out.print ("Choose a higher number.");
else if (numguess >= num) System.out.print ("Choose a lower number.");
}
String input = JOptionPane.showInputDialog("Do you want to continue to play: y or n");
System.exit(0);
String playn = scan.next();
if (playn.equalsIgnoreCase("n"))
play = false;
'
Code Java:
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class hilo {
public static void main(String[] args){
double numguess;
boolean play = true;
while(play){
//boolean play = true;
Random randNum = new Random();
int num = randNum.nextInt(100);
Scanner scan = new Scanner(System.in);
System.out.print ("Enter your guess\n ");
for (int i = 1; i <= 5; i++)
{
numguess = scan.nextDouble();
if (numguess == num) System.out.print ("Yay! You guessed correctly!");
else if (numguess <= num) System.out.print ("Choose a higher number.");
else if (numguess >= num) System.out.print ("Choose a lower number.");
}
String input = JOptionPane.showInputDialog("Do you want to continue to play: y or n");
System.exit(0);
String playn = scan.next();
if (playn.equalsIgnoreCase("n"))
play = false;
}
}
Re: For loop; Problems with my for loop
If you want help, I suggest you provide an SSCCE that we can run- make it as short as possible (we don't actually have to play the game) while still demonstrating the problem.
Also, why is that System.exit() in there?
Re: For loop; Problems with my for loop
I don't know how to make it shorter while demonstrating the problem, so I just went back and added my entire code. I don't think it's a very long program...
I have the System.exit() in there because I thought I needed it if I am going to use the dialog box. I could be wrong about it...
Re: For loop; Problems with my for loop
Quote:
Originally Posted by
mingleth
I don't know how to make it shorter while demonstrating the problem, so I just went back and added my entire code. I don't think it's a very long program...
I don't see your entire program. I see some undeclared variables and some code that's not in a method.
Quote:
Originally Posted by
mingleth
I have the System.exit() in there because I thought I needed it if I am going to use the dialog box. I could be wrong about it...
What happened when you tested that theory?
Re: For loop; Problems with my for loop
You may notice that you have a scanner implemented in your forloop, but nothing ever scans in to fill it. In other words, "numguess" is null, so doesn't meet the requirements of any of your if statements.
Also, why are you scanning for a double but comparing it to an int? Make these the same for simplicity.
Re: For loop; Problems with my for loop
Quote:
Originally Posted by
KevinWorkman
Also, why is that System.exit() in there?
My thoughts exactly. I would have thought that it would not compile with an "unreachable statement" error.