Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: while y/n

  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    while y/n

    PHP Code:
        import java.util.Random;
        
    import java.util.Scanner;
        class 
    indexerad_variable
        
    {
        public static 
    void main(String[] args)
        {
         
            
    Random generator = new Random();
            
    Scanner scan = new Scanner(System.in);
             
            
    int Tries=0;
            
    int number generator.nextInt(100) + 1;
            
    String igen;
         
            while(
    true)
            {
            
    System.out.print("\nNumber 1 - 100: ");
            
    Tries++;
            
    int guess scan.nextInt();
             
            if(
    guess number)
            {
            
    System.out.println("to low");
            }
             
            if(
    guess number)
            {
            
    System.out.println("to high");
            }
             
            if(
    guess == number)
            {
            
    System.out.println("Right you have tried "+Tries+" ");
            {
            }
             
    System.out.print("\nPlay again y/n: ");
             
    igen scan.next();
             if (
    igen.equalsIgnoreCase("N"))
             break;
            }
            }
            }
            } 
    The program works but when i press y it will com back to the last game. and if i type in a break under tries its only go 1 turn in the game. can anyone tell me what i need to do to fix this game?

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    you never call this again
    Code:
    int number = generator.nextInt(100) + 1;
    so the number is the same

    and if i type in a break under tries its only go 1 turn in the game.
    I don't understand what you are trying to do or are saying... you mean
    Code:
    Tries++;
    break;
    that will break on "break;"
    confused as to question or purpose....
    I would do two loops...
    Code:
    bool GameDone=false;
    bool CorrectInt=false;
    while(!GameDone)
    {
      initialize all your variables...
      while(!CorrectInt)
      {
       code for game
       on correct int guess set CorrectInt = true;
       }
       Ask if want to play again
       on n set GameDone = true;
    }

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

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