Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if and else not executing.....

What I have tried:

Java
package practice;
import java.util.Scanner;

public class pract4 {

	public static void main(String[] args) {
	
	Scanner sc = new Scanner (System.in);


     System.out.println("enter rock,paper,scissor");
	 String user1 = sc.nextLine();	 
	 System.out.println("enter rock,paper,scissor");
	 String user2 = sc.nextLine();	
	 
	 
	 
	 if ( user1 ==("rock") && user2 ==("paper"))	{
	 
		 System.out.println("user1 wins!");
	 }
	
	  
	 else if (user1==("rock")&& user2==("scissor"))
	 {
		 System.out.println("user1 wins!");
	 }
		
	 else if (user1==("paper") && user2==("rock"))
	 {
		 System.out.println("user1 wins!");
	 }
	 
	 else if (user1==("paper") && user2==("scissor"))
	 {
		 System.out.println("user2 wins!");
	 }
		
	 else if (user1==("scissor") && user2==("rock"))
	 {
		 System.out.println("user2 wins!");
	 }
		
	 else if (user1==("scissor") && user2==("paper"))
	 {
		 System.out.println("user1 wins!");
	 }
		
	 else
	 {
		 System.out.println(user1==user2);
	 }
		
   }
}<pre>
Posted
Updated 3-Jun-23 23:25pm
v2
Comments
Graeme_Grant 4-Jun-23 4:37am    
Please trim your title so as not to be tagged as spam.
Richard Deeming 5-Jun-23 4:18am    
if ( user1 ==("rock") && user2 ==("paper"))	{
    System.out.println("user1 wins!");
}

That's some strange variation on the game; usually, "paper wraps rock" means that user2 would win in this case.

 
Share this answer
 
Comments
CPallini 5-Jun-23 2:14am    
5.
Patrice T 5-Jun-23 3:58am    
thank you
The first thing I'd do is change that: rather than getting the user to type out their action in full, prompt them for a number: 0 for Scissor, 1 for Rock, 2 for Paper.
That makes life a lot easier for them: they don't have to be able to type accurately, and easier for you because it's easier to verify what is happening: you just add a test for value entered is a valid number. If you work with strings, you make life difficult for yourself as "scissor" is not the same string as "Scissor, "SCISSOR", or any other combination of upper and lower case characters.

Now you can use numbers most of the time. First check if they are the same and if so, print that it's a draw.

Then check who wins:
Paper beats Rock, Rock beats Scissor, Scissor beats paper
Or using numbers:
2 beats 1, 1 beats 0, 0 beats 2
So you could just add one to user 2 input modulo 3 and compare that to the user 1 input: if they are the same, user1 wins. If not, user 2 does!
think about it: it's pretty obvious when you get your head around it!

Simpler tests, shorter, easier code.
 
Share this answer
 
Comments
Richard MacCutchan 4-Jun-23 7:00am    
+5 to counter the pondlife 1-vote.
CPallini 5-Jun-23 2:14am    
5.

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