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
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    >>>Java Help(newbie) with guess my number!!!!!

    /**
    hello, I have problem with looping the program. My code doesn't loop as I expect. By the way logic is fine, just loop isn't working!! Any help and suggestions would be appreciated.
    I want to display the following result;
    Guess a number between 1- 100
    is it 50? (h/c/l): l
    is it 25? (h/c/l): c
    Do you want to play again(y/n):y
    Guess a number between 1- 100
    is it 50? (h/c/l): h
    is it 75? (h/c/l):c
    do you want to play again?(y/n): n


    */
    PHP Code:
    import java.util.Scanner;



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

          
    int max=100;
          
    int min=0;
          
    int mid=50;
          
    String inp=" ";
          
    char playAgain=' ';
           
     
    char choice=' ';


    Scanner in=new Scanner(System.in);



    do{
        
    System.out.println("Guess a number between 1 - 100.");
        
     
        
      while(
    choice!='c')
     {
        
    System.out.print("is it "+mid+"?" " (h/l/c)");
        
    choice=in.next().charAt(0);

        if(
    choice =='l')
        {
            
    max=mid;
            
    mid=(min+mid)/2;
        }

        if(
    choice =='h')
        {

            
    min=mid;
            
    mid=(max+mid)/2;
        }

     }
           
    System.out.println("Great! Do you want to play again? (y/n): "  );
         
    playAgain in.next().charAt(0);

        
       }while(
    playAgain!='n');
      }

    Last edited by Fou-Lu; 09-11-2013 at 01:40 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There's a forum for java so I'll move this there.
    Which loop isn't looping, the outer or the inner?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    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
    •