Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Tolls

Thread: Simple LOOP Question.

  1. #1
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Simple LOOP Question.

    Hi,

    I'm a beginner to java, and have written this simple program. I have a loop, and trying to figure how to loop around when i input "Y". The "N" input terminates the program fine. Please help.

    // code for generating a random number
    // include the java.util.Random library and declare a
    // Random variable then complete the rest of the code
    // for the GuessingGame

    import java.util.Random;
    import javax.swing.JOptionPane;

    public class GuessingGame
    {
    public static void main(String args[])
    {
    // Declare variables

    Random rand = new Random();
    String guessNumberString;
    String shouldContString;
    int LIMIT = 10;
    int value = rand.nextInt(LIMIT);
    int guessNumber;
    String answerString;
    boolean repeat = true;

    // Get Guess Number
    guessNumberString = JOptionPane.showInputDialog("Please Enter Guess Number: ");

    // Convert strings to integers
    guessNumber = Integer.parseInt(guessNumberString);

    // Execute the program
    System.out.println("Guess Number is : "+ guessNumber);
    System.out.println("Random Number is: "+ value);

    while (repeat)
    {

    // Get Yes or No
    answerString = JOptionPane.showInputDialog ("Do you want to play this game? (Y/N): ");

    if ( answerString == "Y")
    repeat = true; // This would continue the loop

    if( guessNumber < value ) // Too Low
    System.out.println("TOO LOW");
    else if( guessNumber > value ) // Too High
    System.out.println("TOO HIGH");
    if( guessNumber == value ) // Correct
    System.out.println("CORRECT");
    else
    repeat = false; // This would break the infinite while loop

    }
    }
    }

  2. #2
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: Simple LOOP Question.

    You are using == to test but it should just be =... Play with it using the correct way to test true and see what you come up with then.. If you need more help ask I only looked at that one detail and then replied.

    Java Code:
    if ( true = true){ System.out.println("Always true");}
    Edit:
    Please place your code between code tags. BB Code List - Java Programming Forum - Learn Java Programming

    Edit edit: You are also missing brackets to include what to do if the test is true.. I posted an example using code tags, make sure you add code tags if you need more help
    Last edited by Gotcha; 06-16-2015 at 06:35 AM. Reason: Added code tages link, missed brackets

  3. #3
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Simple LOOP Question.

    How about now?

    // code for generating a random number
    // include the java.util.Random library and declare a
    // Random variable then complete the rest of the code
    // for the GuessingGame

    import java.util.Random;
    import javax.swing.JOptionPane;

    public class GuessingGame
    {
    public static void main(String args[])
    {
    // Declare variables

    Random rand = new Random();
    String guessNumberString;
    String shouldContString;
    int LIMIT = 10;
    int value = rand.nextInt(LIMIT);
    int guessNumber;
    String answerString;
    boolean repeat = true;

    // Get Guess Number
    guessNumberString = JOptionPane.showInputDialog("Please Enter Guess Number: ");

    // Convert strings to integers
    guessNumber = Integer.parseInt(guessNumberString);

    // Execute the program
    System.out.println("Guess Number is : "+ guessNumber);
    System.out.println("Random Number is: "+ value);

    while (repeat)
    {

    // Get Yes or No
    answerString = JOptionPane.showInputDialog ("Do you want to play this game? (Y/N): ");

    if ( answerString.equals ("Y"))
    repeat = true; // This would continue the loop

    if( guessNumber < value ) // Too Low
    System.out.println("TOO LOW");
    else if( guessNumber > value ) // Too High
    System.out.println("TOO HIGH");
    if( guessNumber == value ) // Correct
    System.out.println("CORRECT");
    else
    repeat = false; // This would break the infinite while loop

    }
    }
    }

  4. #4
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Simple LOOP Question.

    Java Code:
    // code for generating a random number
    // include the java.util.Random library and declare a 
    // Random variable then complete the rest of the code
    // for the GuessingGame
    
    import java.util.Random;
    import javax.swing.JOptionPane;
    
    public class GuessingGame
    {
       public static void main(String args[])
       { 
          // Declare variables
          
          Random rand = new Random();
          String guessNumberString;
          String shouldContString;
          int LIMIT = 10;
          int value = rand.nextInt(LIMIT);
          int guessNumber;
          String answerString;
          boolean repeat = true;
           
          // Get Guess Number
          guessNumberString = JOptionPane.showInputDialog("Please Enter Guess Number: ");    
    
          // Convert strings to integers
          guessNumber = Integer.parseInt(guessNumberString);
         
          // Execute the program
          System.out.println("Guess Number is : "+ guessNumber);
          System.out.println("Random Number is: "+ value);
           
           while (repeat)
           {  
      
          // Get Yes or No
          answerString = JOptionPane.showInputDialog ("Do you want to play this game? (Y/N): ");
    
           if ( answerString.equals ("Y")) 
             repeat = true;  // This would continue the loop  
                  
           if( guessNumber < value )  // Too Low
    		   System.out.println("TOO LOW");     
           else if( guessNumber > value ) // Too High
             System.out.println("TOO HIGH");
           if( guessNumber == value ) // Correct
             System.out.println("CORRECT");               
           else 
             repeat = false; // This would break the infinite while loop 
                  
            }         
       }
    }

  5. #5
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Simple LOOP Question.

    Sorry i got it now. Let me check for the missing bracket.

  6. #6
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Simple LOOP Question.

    I do not understand what you mean by missing brackets. I do not have such expression. What i have is the following,

    Java Code:
    while (repeat)
           {  
       
          // Get Yes or No
          answerString = JOptionPane.showInputDialog ("Do you want to play this game? (Y/N): ");
     
           if ( answerString.equals ("Y")) 
             repeat = true;  // This would continue the loop

  7. #7
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: Simple LOOP Question.

    Man I am sorry I dunno what I was thinking it is ==.. Yeah it is just missing the brackets
    Java Code:
           if ( answerString == "Y"){ 
             repeat = true;  // This would continue the loop
    }

  8. #8
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Simple LOOP Question.

    I tried with the brackets, the loops does not ask me to enter the number again if i answer "Y". Any idea how i can get the LOOP to ask me to enter number if i answer "Y" to the question?

    Good thing is both "Y" or "N" answers terminate the program.

    The code with the brackets.


    Java Code:
    // code for generating a random number
    // include the java.util.Random library and declare a 
    // Random variable then complete the rest of the code
    // for the GuessingGame
    
    import java.util.Random;
    import javax.swing.JOptionPane;
    
    public class GuessingGame
    {
       public static void main(String args[])
       { 
          // Declare variables
          
          Random rand = new Random();
          String guessNumberString;
          String shouldContString;
          int LIMIT = 10;
          int value = rand.nextInt(LIMIT);
          int guessNumber;
          String answerString;
          boolean repeat = true;
           
          // Get Guess Number
          guessNumberString = JOptionPane.showInputDialog("Please Enter Guess Number: ");    
    
          // Convert strings to integers
          guessNumber = Integer.parseInt(guessNumberString);
         
          // Execute the program
          System.out.println("Guess Number is : "+ guessNumber);
          System.out.println("Random Number is: "+ value);
           
           while (repeat)
           {  
      
          // Get Yes or No
          answerString = JOptionPane.showInputDialog ("Do you want to play this game? (Y/N): ");
    
           if ( answerString == "Y"){
             repeat = true;  // This would continue the loop  
              }    
           if( guessNumber < value )  // Too Low
    		   System.out.println("TOO LOW");     
           else if( guessNumber > value ) // Too High
             System.out.println("TOO HIGH");
           if( guessNumber == value ) // Correct
             System.out.println("CORRECT");              
           else 
             repeat = false; // This would break the infinite while loop 
                  
            }         
       }
    }

  9. #9
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Simple LOOP Question.

    question... don't you get the 'do you want to play this game? (Y/N): ' window every time you guess a number?

  10. #10
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Simple LOOP Question.

    Quote Originally Posted by Artemia View Post
    question... don't you get the 'do you want to play this game? (Y/N): ' window every time you guess a number?
    Hi, yes i do get the window. But i have to rerun the program at either if i answer YES or NO. The program wont loop when hitting Y.

  11. #11
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: Simple LOOP Question.

    I changed the approach slightly toying with it.. You need to make it get a new random number and continue.. Also I am sure there was something I forgot I wanted to change.. Also remember Y and y are different answers here... I would add a switch/case but this way works too and it is good to learn it well. It is used much more imo..

    Back your own code up if you are testing mine directly

    Java Code:
        // code for generating a random number
        // include the java.util.Random library and declare a
        // Random variable then complete the rest of the code
        // for the GuessingGame
    
        import java.util.Random;
        import javax.swing.JOptionPane;
    
        public class GuessingGame
        {
        public static void main(String args[])
        {
        // Declare variables
    
        Random rand = new Random();
        String guessNumberString;
        String shouldContString;
        int LIMIT = 10;
        int value = rand.nextInt(LIMIT);
        int guessNumber;
        String answerString;
        boolean repeat = true;
    
        // Get Yes or No
        answerString = JOptionPane.showInputDialog ("Do you want to play this game? (Y/N): ");
        System.out.println("Answer was: "+ answerString);
        //String not equals is tested like so
        if (!answerString.equals("Y")) {
        	System.out.println("Ok perhaps later");
        	System.exit(0);
        }
    	System.out.println("x to quit");	
    
    while (repeat){
        // Get Guess Number
        guessNumberString = JOptionPane.showInputDialog("Please Enter Guess Number: ");
        if (guessNumberString.equals("X")){
        	System.exit(0);
        }
        // Convert strings to integers
        guessNumber = Integer.parseInt(guessNumberString);
    
        // Execute the program
        System.out.println("Guess Number is : "+ guessNumber);
        System.out.println("Random Number is: "+ value);
    
        if( guessNumber < value ){ // Too Low
        System.out.println("TOO LOW");}
        else if( guessNumber > value ){ // Too High
        System.out.println("TOO HIGH");}
        if( guessNumber == value ){ // Correct
        System.out.println("CORRECT");
        /** Rudely quits here needs a new random number or game would be easy */
        System.exit(0);
        //repeat = false; // This would break the infinite while loop
        }
      }
     }
    }

  12. #12
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Simple LOOP Question.

    you won't get that if you put the 'do you want to play this game? (Y/N): ' outside of the loop
    then when the guess is correct you open a dialog window saying something like 'Correct! do you want to play again? (Y/N): '

    if you do it this way, you put the repeat value at True before the loop starts
    and you will only need a line of code saying that if you want play again equals 'N' that the repeat value need to be set to false.

    you will need to edit the loop a bit this way but it should work

    oh, in the time it took me to write this, you already did something like that :P

    P.S. to compare 2 string and not get it all hung-up on upper case or lower case letters you can use 'equalsIgnoreCase(String variable)'
    this would return true

    Java Code:
    class Test {
    	public static void main(String[] args) {
    		
    		String s1 = "y";
    		String s2 = "Y";
    		
    		System.out.println(s1.equalsIgnoreCase(s2));
    		
    	}
    
    }
    Last edited by Artemia; 06-16-2015 at 08:13 AM.

  13. #13
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Simple LOOP Question.

    Thanks. I figured it out. Here's my code.

    Java Code:
    import java.util.Random;
    import javax.swing.JOptionPane;
    
    public class GuessingGame
    {
       public static void main(String args[])
       { 
          // Declare variables
          
          Random rand = new Random();
          String guessNumberString;
          String shouldContString = "Y";
          int LIMIT = 10;
          int value = rand.nextInt(LIMIT);
          int guessNumber = 1;
          String answerString;    
    
          // Get Guess Number
                  
          while (shouldContString.equals("Y"))
             
           {
          guessNumberString = JOptionPane.showInputDialog("Please Enter Guess Number: ");
          
          guessNumber = Integer.parseInt(guessNumberString);
            
           if( guessNumber < value )  // Too Low
    		   {
             System.out.println("Guess Number is : "+ guessNumber);
             System.out.println("Random Number is: "+ value);         
             System.out.println("TOO LOW"); 
             shouldContString = JOptionPane.showInputDialog("Do You Want to Play Again: ");
             }    
           else if( guessNumber > value ) // Too High
             {
             System.out.println("Guess Number is : "+ guessNumber);
             System.out.println("Random Number is: "+ value);         
             System.out.println("TOO HIGH");
             shouldContString = JOptionPane.showInputDialog("Do You Want to Play Again: ");
             }
           else  // Correct
             {
             System.out.println("Guess Number is : "+ guessNumber);
             System.out.println("Random Number is: "+ value);
             System.out.println("CORRECT");  
             shouldContString = JOptionPane.showInputDialog("Do You Want to Play Again: ");
             value = rand.nextInt(LIMIT);
             }            
             
           }    
                   
       }
    }

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Simple LOOP Question.

    Since this line:
    Java Code:
    shouldContString = JOptionPane.showInputDialog("Do You Want to Play Again: ");
    happens in each part of your if/else block, it may as well be outside that block, as the last line of your while loop.
    No sense duplicating code like that.
    In addition these two lines:
    Java Code:
             System.out.println("Guess Number is : "+ guessNumber);
             System.out.println("Random Number is: "+ value);
    may as well be placed before the if block, as they are also the same in each pat h through your if/else.
    Aryaa likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: Simple LOOP Question.

    another small suggestion...it strictly accepts upper case "Y".instead u can do equalsIgnoreCase ...for comparison
    Ramya:cool:

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 09:27 AM
  2. Converting a simple for loop to multithreads
    By ki_ha1984 in forum New To Java
    Replies: 0
    Last Post: 06-08-2013, 02:51 PM
  3. Simple nested loop help (kinda urgent)
    By IAmACodingNoob in forum New To Java
    Replies: 8
    Last Post: 01-24-2012, 05:28 AM
  4. Need Some Help, Simple Loop For Hangman Game
    By Juo in forum New To Java
    Replies: 2
    Last Post: 11-14-2009, 07:51 PM
  5. simple loop syntax( ; ;), ( something : somthing)
    By xcallmejudasx in forum New To Java
    Replies: 4
    Last Post: 12-13-2008, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •