Results 1 to 5 of 5
  1. #1
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default loop "play again" in an 8 ball game, loops but ... (by IareSmart)

    Reposted because of a forum bug with the word "print" appearing in a thread topic.

    The original post was by IareSmart.

    db



    loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
    Hey this is my first post so if i make a mistake shout at me.

    I made an 8ball program but the problem is when it loops is wont let me answer my "out.print("What would u like to ask ? ");" , it just gives me a yes , no or maybe , then asks "out.println("Want to ask another question ?");"

    Iv only been learning code for 3 or 4 days so not very good at it xD

    Im guessing i need a break; somewhere or another loop ?

    heres the code

    Java Code:
    import java.util.Scanner;
     import static java.lang.System.out;
     import java.util.Random;
    
     class TestFor8Ball {
    
      public static void main(String args[]) {
       Scanner in = new Scanner(System.in);      
       char reply = 'y';
       Random myRandom = new Random();
       int random; 
       
     while (reply == 'y' || reply == 'Y')
     {
         out.print("What would u like to ask ? ");
       in.nextLine();
       out.println();  
       random  = myRandom.nextInt(10) + 1;
     
          if ( random > 5) {                                                            
                   out.println("The answer to your question is No");
         } else if (random < 5) {
                       out.println("The answer to your question is Yes");
         } else {
                   out.println("mmm Maybe i realy dont know");
               }
                             
                 out.println();
                 out.println("Want to ask another question ?");
                 out.println();
               
                 reply = in.findInLine(".").charAt(0);
     if (reply == 'n' || reply == 'N')
     {
     out.println();        
     out.println("Good Bye");
                 }        
                 }
                   }  
                   }
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: loop "play again" in an 8 ball game, loops but ... (by IareSmart)

    After you ask the question on line 29 what is the value that the user enters and is put into the reply variable?
    Add a println to print out those values so you can see what the code is doing.

  3. #3
    IareSmart is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: loop "play again" in an 8 ball game, loops but ... (by IareSmart)

    Thanks for reposting , i got it to work but didnt use a loop

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    import static java.lang.System.out;
    
    class Ball {
    
    public static void Ball8(){
    
    Scanner in = new Scanner(System.in);      
      
      
      
        
        
       Random myRandom = new Random();
       int random; 
        
      
      
      
      
      
    
    
     
     
     
        out.print("What would u like to ask ? ");
        in.nextLine();
        out.println();
                    
             
           
    
      random  = myRandom.nextInt(10) + 1;
             
      
          if ( random > 5) {                                                            
                  out.println("The answer to your question is No");
         
        
                        
        
        } else if (random < 5) {
                  out.println("The answer to your question is Yes");
                     
                        
                        
        } else {
                  out.println("mmm Maybe i realy dont know");
               
              
                        }
                 
                  out.println();
                 
                  out.println("Ask another question");
                 
                  out.println();
    
    Replay();
    
    
    
    }
    public static void Replay(){
    Scanner in = new Scanner(System.in);
    char reply;
    
    
    
    reply = in.findInLine(".").charAt(0);
    
    
    
    
    if (reply == 'Y' || reply == 'y'){
        System.out.println();
    new Ball().Ball8();
    
     
      
      }else{
       out.println();
       out.println("Good Bye");
      
    }
    
    }
    public static void main(String[] args)
    {
    new Ball().Ball8();

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: loop "play again" in an 8 ball game, loops but ... (by IareSmart)

    Please stop wearing out my scroll wheel with inordinate amounts of vertical whitespace (blank lines to you).

    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    IareSmart is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: loop "play again" in an 8 ball game, loops but ... (by IareSmart)

    haha sorry duno why there is so much blank space

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  2. Play soundclip and generate"wind" like sound
    By diablo2070 in forum New To Java
    Replies: 5
    Last Post: 03-13-2011, 08:47 PM
  3. Replies: 0
    Last Post: 06-07-2010, 06:23 PM
  4. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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