This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

RockPaperScissors in Java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! For my Java class we have to create a game of Rock Paper Scissors between two players. I have no idea where I'm going wrong and was wondering if someone could help me. The code launches and lets me input either rock, paper, or scissors, however it will not give me an output of my results. I honestly have no clue on what to do. Thank you
Screen-Shot-2019-10-07-at-1.55.15-AM.png
[Thumbnail for Screen-Shot-2019-10-07-at-1.55.15-AM.png]
Code
Screen-Shot-2019-10-07-at-2.01.14-AM.png
[Thumbnail for Screen-Shot-2019-10-07-at-2.01.14-AM.png]
What Displays
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Nice to see somebody who knows how to indent code But there are all sorts of things wrong with your code. Aren't you supposed to use object oriented (=OO) programming? Because you haven't.
One thing: you are changing your inputs to UPPER‑CASE and then comparing them to constants (constants are a good thing) in Book Title Case.
 
Genesis Chavira
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what object oriented programming means, sorry, this is my first time working with Java... and someone mentioned the Upper Case letters but they did not tell me what was wrong with it
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Insert a line something like this:-I can't get the right line number because your screenshot is invisible just at the moment.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the game you ae showing, you didn't enter rock nor paper nor scissors. You aren't following your own instructions.
 
Sheriff
Posts: 6266
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Genesis Chavira!

Instead of a screenshot, please copy and paste your code into the reply and UseCodeTags (that's a link).  That helps everybody to see your code clearly.
 
Genesis Chavira
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
try(Scanner keyboard1= new Scanner(System.in)){
try(Scanner keyboard2= new Scanner(System.in)){

String P= "Paper", R= "Rock", S= "Scissors";
String s1, s2;
System.out.println("Player 1 enter Rock, Paper, or Scissors");
s1= keyboard1.next();
s1= s1.toUpperCase();
System.out.println("Player 2 enter Rock, Paper, or Scissors");
s2= keyboard2.next();
s2= s2.toUpperCase();

if (s1.equals(s2)) {
System.out.println("No One Wins.");
}
else if(s1.equals(P) && s2.equals(R)) {
System.out.println("Paper beats Rock,Paper Wins!");
}
else if(s1.equals(P) && s2.equals(S)) {
System.out.println("Scissors beats Paper,Scissors Wins!");
}
else if(s1.equals(R) && s2.equals(S)) {
System.out.println("Rock beats Scissors,Rock Wins!");
}
else if(s1.equals(R) && s2.equals(P)) {
System.out.println("Paper beats Rock,Paper Wins!");
}
else if(s1.equals(S) && s2.equals(P)) {
System.out.println("Scissors beats Paper,Scissors Wins!");
}
else if(s1.equals(S) && s2.equals(R)) {
System.out.println("Rock beats Scissors,Rock Wins!");
}
}

}
}
}

That's all the code I have
The lines before it is just information my professor wanted about the project so technically my line 7 is my line 1
 
Genesis Chavira
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Insert a line something like this:-I can't get the right line number because your screenshot is invisible just at the moment.



I typed this in, and I'm starting to get an output, however, how can I get it to show me which one wins, for each possible scenario?
Screen-Shot-2019-10-07-at-4.27.41-PM.png
[Thumbnail for Screen-Shot-2019-10-07-at-4.27.41-PM.png]
 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"S" will NEVER equals() "Scissors" -- S (the variable) is NOT the same as "S" (a String literal).

If you assign "Scissors" to the variable S, then S.equals("S") will be false.

toUpperCase() converts all the characters of a String to uppercase, so the expression "Scissors".equals(someString.toUpperCase()) will ALWAYS be false. By the way, that's safer than writing someString.toUpperCase().equals("Scissors") because it won't thrown a NullPointerException if someString is null.
 
Genesis Chavira
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What can I do to fix my errors?
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've been given a lot of tips that should help you find your problem. You should go back and read your code again and make sure your code makes logical sense. A bug is really a miscommunication. You're not actually telling the computer what you think you're telling it to do. You won't learn if you don't figure it out for yourself. Give it a try.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this expression, for example: s1.equals(P) && s2.equals(R)

if s1 is "P" and P (the variable) is "Paper" will s1.equals(P) ever be true?
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!