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

Rock, Scissors, Paper. java with using charAt(0), can someone fix my programm

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package rock.scissors.paper;

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


/**
*
* @author skulldark
*/
public class RockScissorsPaper {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
      Scanner s = new Scanner (System.in);//Object for getting input from keyboard
      Random ran = new Random();// Get user input from keyboard
      int compPickInt = ran.nextInt(3);// Make a random number between 0 and 2 for the computer's choice.
      System.out.println("I have my pick behind my back.");
      char R=0, S=1, P=2;// R= Rock, S=Scissors, P= Paper
      System.out.println("What is your pick? R, S, P");
       char  userPick = s.next().charAt(0);// Get user input from keyboard
      System.out.println("My pick = " + compPickInt + " Your pick = " + userPick);
   
      if (userPick == compPickInt)
          System.out.println("It is a tie - we both picked Rock");
     
   else if (userPick == R && compPickInt == S)
           System.out.println("The ‘Rock’ player wins because a rock breaks scissors.");
     
   else if(userPick == R && compPickInt == P)
       
           System.out.println("The ‘Paper’ player wins because paper covers a rock.");
     
   else if (userPick == S && compPickInt == P)
         
           System.out.println("The ‘Scissors’ player wins because scissors cut paper");
     
       
   }
   }

     
         
     
   
   
 
Master Rancher
Posts: 259
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would also be easier for us (at least, for me personally) to read and evaluate your code if you'd use the code tags when posting code snippets.
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Brecht Geeraerts wrote:It would also be easier . . . to read . . . your code if you'd use the code tags . . .

More important: it would be easier for OP to read their own code. I shall try displaying the code with code tags soon. I can see at least one formatting error even without code tags; that should have been sorted out automatically by the IDE.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the formatting errors, what is going wrong? We can only help if we know the full details.
 
Saloon Keeper
Posts: 10657
227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some general advice regarding your code:

  • Make classes package private unless you need to use them outside their package.
  • Make classes final unless you intend to extend them. Making a class extensible is not easy at all, so get in the habit of using final a lot.
  • Don't put all your application code in the main method. Ideally, all main() should do is instantiate a class and then call a method on it.
  • Split your application up in classes that are responsible for different things. Right now you're mixing code for the game logic and the user interface.
  • Use an enum to represent that choices that a player can make.
  • Scanner.next() blocks until input is available. The command line doesn't make user input available until the user hits enter, so presumably you want to use Scanner.nextLine() and not Scanner.next().
  • Get rid of long sequences of else-if statements. It means your logic is not encapsulated properly.

  • You can solve a lot of your problems by creating an enum Choice, which contains the constants ROCK, PAPER and SCISSORS. Add a static parse() method that converts a String to an instance of Choice, and add a beats(Choice) instance method that determines whether one choice beats another.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    More formatting problems, I am afraid:-

  • Don't start your variable names with CapitalLetters. Unfortunately when you correct that, you will find that you have some poorly‑chosen variable names. You can't tell what P/p means. You will also find another problem about using s as a variable name!
  • Don't declare multiple variables on the same line. The compiler might tolerate it, but readers of your code won't.
  • Lines 31‑43 show inconsistent indentation and inconsistent spacing; those empty lines don't add to legibility.
  • You have missed out the {} for each of your ifs. See our formatting suggestions for more details.
  • Stephan is right about your multiple ifs. It means your code can only cope with three options. Please search for this game with enums, and you will probably find a better way to implement it.
  • Also look at line 32; that doesn't look right to me.


  • I am pleased to see you know how Random#nextInt(int) works. Many people would have got line 24 wrong.
     
    Marshal
    Posts: 7178
    491
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @OP

    Not sure how important is that if at all, but game is really called "Rock–Paper–Scissors" (difference sequence from yours).

    Also, package names concept is slightly different. Try to find information on web what is the common approach naming packages. I don't think package name rock.scissors.paper makes much sense here.
     
    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
    This is not logically correct:

    (Edited) Assuming you fix the main issue in this code, this will display "we both picked Rock" even when both picked Paper or Scissors, which will be confusing to the player.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, better than that; all his if (b) ...s evaluate to !b or false!
     
    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
    char literals should be enclosed in single quotes, like so:

    Also, line 29 should give you a clue:

    This will display something like this:

    Tip: char is an integer type in Java so you can do this:
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:. . .

    I tried the code and that does indeed happen. That gave me a hint as to what is going wrong.

    . . .

    You can do that, yes, but I think that sort of code is error‑prone because it is too easy to copy the numbers wrongly from an ASCII table. I would prefer char myLetter = 'J';

    There is an ASCII table on the Unicode website. Remember the numbers shown in the table's margins are hexadecimal.
     
    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
    The point I was trying to make was that char is an integer type.  

    Because of this, you can do what I showed by assigning a number like 74 to a char but you shouldn't because char literals like 'J' are clearer.

    I should have added that this is exactly the reason this compiles fine even though it doesn't do what OP meant it to do, as the comment suggests

     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It isn't so bad using 0 1 2. The problem is that the program has no way of matching those numbers with P R S entered from the keyboard.
     
    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

    Campbell Ritchie wrote:It isn't so bad using 0 1 2. The problem is that the program has no way of matching those numbers with P R S entered from the keyboard.


    It's not so bad in that it can be made to work but it's also not very good either because 0, 1, and 2 are poor representations of the idea of Rock, Paper, and Scissors. It's an encoding that is not really necessary and it can be easily avoided with some of the suggestions already given, particularly about the enums.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:. . . 0, 1, and 2 are poor representations of the idea of Rock, Paper, and Scissors.

    Agree.

    . . . enums.

    Who was it first mentioned enums? Was it Stephan?
     
    Abdallah Taha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    public class JavaApplication33Game {

       /**
        * @param args the command line arguments
        */
       public static void main(String[] args) {
           Scanner s = new Scanner(System.in);
           Random ran = new Random();
           
          System.out.println("I have my pick behind my back ");
          System.out.println("What is your pick? R, S, P");
          int compPickInt = ran.nextInt(3);
          char userPick = s.next().charAt(0);
          int R = '0'; //R = Rock
          int S = '1'; //P = Paper
          int P = '2'; //S = Scissors
         
          userPick = (char)R;
          userPick = (char)S;
          userPick = (char)P;
           
           System.out.println("My pick = " + userPick + " Your pick = " + compPickInt);
     
    Abdallah Taha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'am asking how to convert the numbers to a char variable with the value of R or S or P
     
    Abdallah Taha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:char literals should be enclosed in single quotes, like so:

    Also, line 29 should give you a clue:

    This will display something like this:

    Tip: char is an integer type in Java so you can do this:


    You are right about the display, I followed your instruction but I still have the same issue.
     
    Bartender
    Posts: 3509
    150
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Abdallah Taha wrote:I'am asking how to convert the numbers to a char variable with the value of R or S or P


    But you do not need to work with digits and chars.

    Suppose you have an arrays:

    Then you can have:

    and you can compare that to userInput.charAt(0);
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I merged your stuff with the following thread. I hope that is okay by you.
     
    Abdallah Taha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    public class JavaApplication33Game {

       /**
        * @param args the command line arguments
        */
       public static void main(String[] args) {
           Scanner s = new Scanner(System.in);
           Random ran = new Random();
           
          System.out.println("I have my pick behind my back ");
          System.out.println("What is your pick? R, S, P");
          int compPickInt = ran.nextInt(3);
          char userPick = s.next().charAt(0);
          int R = '0'; //R = Rock
          int S = '1'; //P = Paper
          int P = '2'; //S = Scissors
         
          userPick = (char)R;
          userPick = (char)S;
          userPick = (char)P;
           
           System.out.println("My pick = " + userPick + " Your pick = " + compPickInt);
     
    Rancher
    Posts: 4270
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What exactly is your problem?
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Abdallah Taha wrote:. . . convert the numbers to a char variable . . .

    I think your second thre‍ad is simply a continuation of your old question, so I have merged the two into one discussion.

    As Piet says, you are probably better off with an array:-This is another way to create that array:-
     
    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
    @OP: Think about what you're doing. You are asking the player to enter R, S, or P but then check the response against 0, 1, or 2. Does that make any sense to you?

    You: "Please tell me, R, S, or P."

    Player: "S"

    You: (did he just say 0, 1, or 2?... Hmmm I wonder why this isn't working)
     
    Abdallah Taha
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    package javaapplication33game;

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

    /**
         
    *
    * @author abdal
    */
    public class JavaApplication33Game {

       /**
        * @param args the command line arguments
        */
       
       public static void main(String[] args) {
           Scanner s = new Scanner(System.in);
           Random ran = new Random();
           
          System.out.println("I have my pick behind my back ");
          System.out.println("What is your pick? R, S, P");
          int compPickInt = ran.nextInt(3);
          int userPick = s.nextInt();
          int R= 0;
          int S= 1;
          int P= 2;
           
           
          System.out.println("My pick = "+compPickInt+  " Your pick = "+ userPick);
           
          System.out.println("I have my pick behind my back ");
           //Condition R
           if (userPick ==R  && compPickInt == 1){
          System.out.println("The ˜Rock player wins because a rock breaks scissors.");
           }
          if (userPick == R && compPickInt == 2) {
          System.out.println("The ˜Paper player wins because paper covers a rock.");}
         
          if (userPick == R && compPickInt == 0){
          System.out.println("It is a tie - we both picked Rock");}
         
         
           //Condition S
           if (userPick == S && compPickInt == 0){
          System.out.println("The ˜Rock player wins because a rock breaks scissors.");
           }
          if (userPick == S && compPickInt == 2) {
          System.out.println("The ‘Scissors’ player wins because scissors cut paper");}
         
          if (userPick == S && compPickInt == 1){
          System.out.println("It is a tie - we both picked Rock");}
         
           //Condition P
           if (userPick == P && compPickInt == 0){
          System.out.println("The ˜Paper player wins because paper covers a rock.");
           }
          if (userPick == P && compPickInt == 1) {
          System.out.println("The ‘Scissors’ player wins because scissors cut paper.");}
         
          if (userPick == P && compPickInt == 2){
          System.out.println("It is a tie - we both picked Rock");}


    }
    }

    Thank you for all, I fixed but I didn't understand how to use the convertion with the char, the question that the teacher ask is compPickInt will now be a 0,1, or 2. Use an ‘if-else if’ structure to convert the numbers to a char variable with the value of R or S or P and Prompt the user to enter R for Rock, S for Scissors or P for Paper. Use this java codeto store the user’s choice as a char variable:char userPick = s.next().charAt(0). I'll figure out with the teacher.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's get your code tags right; please read the link I posted earlier to find out how the code tags work.

    Abdallah Taha wrote:Thank you

    That's a pleasure

    . . . compPickInt will now be a 0,1, or 2. Use an ‘if-else if’ structure to convert the numbers to a char variable with the value of R or S or P . . .

    That is what you haven't done. The whole exercise might be useful for showing you how ifs and else‑ifs work, but it is not at all a good way to implement the game, nor is it object‑oriented programming.
     
    Skool. Stay in. Smartness. 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!