Web Analytics Made Easy -
StatCounter Rock paper scissors - CodingForum

Announcement

Collapse
No announcement yet.

Rock paper scissors

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rock paper scissors

    can someone help me with this
    it is a rock paper scissors game
    when i run it this is what i get

    Enter 1 for Rock, 2 for Paper, and 3 for Scissors
    3
    Tie Game
    Play Again? y/n
    Scores:
    Ties= 1 Wins= 0 Losses= 0

    i am trying to get it to play again and not print out the scores until the player is done playing

    thanks for the help



    import java.util.Scanner;
    import java.util.Random;

    public class boobitty
    {
    public static void main (String[] args)
    {
    int Computer=0,Player=0,tie=0,compic,pscore;
    String str=" ";
    Random generate= new Random();
    Scanner scan=new Scanner(System.in);
    while (str=="Y");
    {
    compic=generate.nextInt(3)+1;
    System.out.println ("Enter 1 for Rock, 2 for Paper, and 3 for Scissors");
    pscore=scan.nextInt();
    if (compic==pscore)
    {
    System.out.println("Tie Game");
    tie++;
    System.out.println ("Play Again? y/n");
    str = scan.nextLine();
    }
    else
    {
    switch (pscore)
    {
    case 1:
    {
    if (compic==2)
    {
    System.out.println ("Paper beats Rock");
    System.out.println ("Computer wins");
    Computer++;
    System.out.println ("Play Again? y/n");
    str = scan.nextLine();
    }
    else
    {
    System.out.println ("Rock beats Scissors");
    System.out.println ("Player wins!");
    Player++;
    System.out.println ("Play Again? y/n?");
    str = scan.nextLine();
    }
    break;
    }
    case 2:
    {
    if (compic==1)
    {
    System.out.println ("Paper beats Rock");
    System.out.println ("Player wins!");
    Player++;
    System.out.println ("Play Again? y/n?");
    str = scan.nextLine();
    }
    else
    {
    System.out.println ("Scissors beat Paper");
    System.out.println ("Computer wins!");
    Computer++;
    System.out.println ("Play Again? y/n?");
    str = scan.nextLine();
    }
    break;
    }
    case 3:
    {
    if (compic==1)
    {
    System.out.println ("Rock beats Scissors");
    System.out.println ("Computer wins");
    Computer++;
    System.out.println ("Play Again? y/n?");
    str = scan.nextLine();
    }
    else
    {
    System.out.println ("Scissors beat Paper");
    System.out.println ("Player wins");
    Player++;
    System.out.println ("Play Again? y/n");
    str = scan.nextLine();
    }
    break;
    }
    default:
    {
    System.out.println("Enter 1 2 or 3");
    System.out.println ("Would you like to play again? y/n");
    str=scan.nextLine();
    break;
    }
    }

    }
    }



    if (str=="Y")
    {
    System.out.println ("Play Again? y/n");
    str = scan.nextLine();
    }
    else
    {
    System.out.println ("Scores:");
    System.out.println ("Ties= "+tie+" Wins= "+Player+" Losses= "+Computer);


    }





    }
    }

  • #2
    First off: http://www.codingforum.net/showthread.php?t=53446

    So let's talk about your code. In java and most languages that uses references you can't do a String comparison such as if(str == "Y")

    With == it is looking at the memory address and seeing if it's the same, you want to know if the letter is the same. String has many functions which include stuff like equals and equalsIgnoreCase()

    so for example
    Code:
    public class test
    {
    
      public static void main(String args[])
      {
        String test = "test";
        if(test.equals("test"))
           System.out.println("Test is true");
       else
           System.out.println("Test is false");
        if(test.equalsIgnoreCase("TEST"))
           System.out.println("TEST is true");
        else
           System.out.println("TEST is false");
      }
    }
    Go ahead and copy and paste that into a java file and compile and run it. See what pops out.

    You're while statement will never be entered, because there is a semi-colon at the end of it. The correct way to use whiles are as such:

    Code:
    public class whileTest
    {
    
       public static void main(String args[])
       {
          int i = 0;
          while(i < 4)
          { 
            System.out.print("*");
            i++;
           }
       }
    }
    You're while loop also wont be entered past that because you're initial string setting is a blank, so the string wont equal "Y" meaning it wont enter the loop.

    And Lastly, if you want the player to choose again you need to move your if statement inside the while loop, or it'll never be reached.

    Hope that helps.
    -Aradon
    "To iterate is human, to recurse divine." -L. Peter Deutsch

    Comment


    • #3
      Thanks
      i am only having one problem now
      Code:
      if (str.equals("y"))
                     {
                        System.out.println ("Play Again? y/n");
                        str = scan.nextLine();
                     }
                     else
                     { 
                        System.out.println ("Scores:");
                        System.out.println ("Ties= "+tie+" Wins= "+Player+" Losses= "+Computer); 
                     }
      when its like this it won't let me enter anything when i scan.nextLine

      and how would i do something like
      if (str.notequals)

      Comment


      • #4
        I'm rusty on my Java, but how bout... if(! (str.equals("y"))

        Comment


        • #5
          ok and how can i get it to scan for a string

          Comment


          • #6
            I'll be honest that I don't know much about the scanner class since I get all my IO directly through bufferedReaders and the like.

            As far as not, Java has a logical operator that specifies not anything.

            Code:
            !
            So if you wanted to say..

            Code:
            if(!str.equals("something"))
              System.out.println("It doesn't equal Something!!!");
            "To iterate is human, to recurse divine." -L. Peter Deutsch

            Comment


            • #7
              Here is a piece from one of my old codes. Hope it helps:
              Code:
              String string1;
              String string2;
              Scanner scan = new Scanner (System.in);
              System.out.println ("Please enter your name:");
              string1 = scan.nextLine();
              System.out.println ("Please enter you last name:");
              string2 = scan.nextLine();
              System.out.println ("Hi " + string1 + " " + string2 );
              Note that you can use the same scanner for both strings and numbers(doubles, ints, float, etc)/
              And if you scan a string and want to convert it to int for example, use int n = Integer.parseInt(string);
              Last edited by daniel_g; Oct 1, 2006, 04:04 AM.

              Comment


              • #8
                thank you
                here is my rock paper scissors game
                Code:
                   
                import java.util.Scanner;
                   import java.util.Random;
                
                    public class boobitty
                   {
                       public static void main (String[] args)
                      {
                         int Computer=0,Player=0,tie=0,compic,pscore;
                         String str="y"; 
                         Random generate= new Random();
                         Scanner scan=new Scanner(System.in);
                         while (str.equals("y"))
                         {
                            compic=generate.nextInt(3)+1;
                            System.out.println ("Enter 1 for Rock, 2 for Paper, and 3 for Scissors");
                            pscore=scan.nextInt();
                            if (compic==pscore)
                            {
                               System.out.println("Tie Game");
                               tie++;
                            }
                            else
                            {
                               switch (pscore)
                               {
                                  case 1:
                                     { 
                                        if (compic==2)
                                        {
                                           System.out.println ("Paper beats Rock");
                                           System.out.println ("Computer wins");
                                           Computer++;
                                        }
                                        else
                                        { 
                                           System.out.println ("Rock beats Scissors");
                                           System.out.println ("Player wins!");
                                           Player++;
                                        }
                                        break;
                                     }
                                  case 2:
                                     {
                                        if (compic==1)
                                        {
                                           System.out.println ("Paper beats Rock");
                                           System.out.println ("Player wins!");
                                           Player++;
                                        }
                                        else
                                        { 
                                           System.out.println ("Scissors beat Paper");
                                           System.out.println ("Computer wins!");
                                           Computer++;
                                        }
                                        break;
                                     }
                                  case 3:
                                     {
                                        if (compic==1)
                                        {
                                           System.out.println ("Rock beats Scissors");
                                           System.out.println ("Computer wins");
                                           Computer++;
                                        }
                                        else
                                        { 
                                           System.out.println ("Scissors beat Paper");
                                           System.out.println ("Player wins");
                                           Player++;
                                        } 
                                        break;
                                     }
                                  default:
                                     {
                                        System.out.println("Enter 1 2 or 3");
                                        break;
                                     }
                               }
                               
                            
                            
                            } 
                            Scanner scen = new Scanner (System.in);
                				System.out.println ("Play again? y/n"); 
                				str = scen.nextLine();
                
                                  
                            if(!(str.equals("y")))
                            
                            {
                               System.out.println ("Scores:");
                               System.out.println ("Ties= "+tie+" Wins= "+Player+" Losses= "+Computer);
                            }
                            else
                            { 
                               System.out.println ("playing again"); 
                            }
                         }
                      
                      }
                   }

                Comment

                Working...
                X