Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: For loop; Problems with my for loop

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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;
     
    }
    }
    Last edited by mingleth; November 15th, 2011 at 09:16 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: For loop; Problems with my for loop

    Quote Originally Posted by mingleth View Post
    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 View Post
    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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: For loop; Problems with my for loop

    Quote Originally Posted by KevinWorkman View Post
    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.
    Improving the world one idiot at a time!

Similar Threads

  1. Java Scanners and loop problems
    By Studen2014 in forum Loops & Control Statements
    Replies: 2
    Last Post: September 10th, 2011, 09:27 AM
  2. Problems with incrementing/decrementing in a for loop..
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 13th, 2011, 06:15 AM
  3. [SOLVED] Problems with Loop Structure
    By Sean137 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 13th, 2010, 02:59 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 03:09 AM
  5. problems with loop in Java App
    By dmonx in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 04:13 PM