(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:
'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;
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; } }