Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
import java.util.Scanner;
public class Lab3
{
    public static void main (String[]args)
    {
     Scanner in = new Scanner(System.in);
             System.out.println("enter a value for x:");
             int x = in.nextInt();
             System.out.println("enter a value for y:");
             int y = in.nextInt();
             if (x <= y )
             {
                 System.out.println("the smallest value was:"+ x);
             }
             else
             {
                 System.out.println("the smallest value was:"+ y);
             }




             String choice12;
             String choice234;
              System.out.println("Player 1: Choose rock, scissors, or paper:");
            String  choice1 = in.nextLine().toLowerCase();
            System.out.println(choice1);

              System.out.println("Player 2: Choose rock, scissors, or paper:");
              String choice2 = in.nextLine().toLowerCase();
              System.out.println(choice2);
               if (choice1.equals("rock") && choice2.equals("rock"))
        {
           System.out.println("It is a tie.");

        }
        if (choice1.equals("rock") && choice2.equals("scissors"));
        {
           System.out.println("Player 1 wins.");

        }
           if (choice1.equals("rock") && choice2.equals("paper"));
        {
           System.out.println("Player 2 wins.");
        }
           if (choice1.equals("paper") && choice2.equals("paper"));
        {
           System.out.println("it is a tie.");
        }
           if (choice1.equals("paper") && choice2.equals("rock"));
        {
           System.out.println("Player 1 wins.");
        }
           if (choice1.equals("paper") && choice2.equals("scissors"));
        {
           System.out.println("Player 2 wins.");
        }
           if (choice1.equals("scissors") && choice2.equals("paper"));
        {
           System.out.println("Player 1 wins.");
        }
           if (choice1.equals("scissors") && choice2.equals("scissors"));
        {
           System.out.println("it is a tie.");
        }
           if (choice1.equals("scissors") && choice2.equals("rock"));
        {
           System.out.println("Player 2 wins.");
        }


        }
    }
Posted
Comments
diego14567 21-Sep-12 21:54pm    
the second part of the code is suppose to simulate 2 players playing a game of rock paper scissors but when it runs only lets you enter the second players choice and prints out all the if options

1 solution

This is a classic problem:

When you enter Integer number you enter two things
1. the integer part
2. a new line.


and nextInt function only read the integer part and ignore the newline part. So, after finishing reading when you call the method nextLine, it reads the new line and complete the reading process. So to avoid this problem before reading choice1, call the nextLine method once, you don't need to save the line in any variable. Your problem will be solved
 
Share this answer
 
Comments
diego14567 21-Sep-12 23:58pm    
Thanks for the answer but i was able to solve it by using the next(); as typing in nextline(); twice would occur in the if statements being skipped

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900