• 0

Need Help With Java Rock Paper Sissors Code


Question

I am writting a rock paper scissors program for my java class. I have wrote a program that should work but i keep having trouble with getting my variables to work write. Sometimes compiler will read them and sometimes it wont. I am using jgrasp to write with. Any help would be greatly appreciated. Also this is my first time using java so i know there are some mistakes. I just cant find them.

import javax.swing.JOptionPane;

import java.util.Random;

public class RockPaperScissors

{

public static void main(String[] args)

{

JOptionPane.showMessageDialog(null, "Let's play Rock Paper Scissors.\n1 = Rock\n2 = Paper\n3 = Scissors");

String player;

player = JOptionPane.showInputDialog("Please Enter a number between 1 and 3.");

Random generator = new Random();

int computer = 1 + generator.nextInt(2);

boolean case1, case2, case3, case4, case5, case6, case7, case8, case9;

case1 = (player = 1) && (computer = 2);

case2 = (player = 1) && (computer = 3);

case3 = (player = 2) && (computer = 1);

case4 = (player = 2) && (computer = 3);

case5 = (player = 3) && (computer = 1);

case6 = (player = 3) && (computer = 2);

case7 = (player = 1) && (computer = 1);

case8 = (player = 2) && (computer = 2);

case9 = (player = 3) && (computer = 3);

if (case1)

JOptionPane.showMessageDialog(null, "Computer choses Paper. You lose.");

else if (case2)

JOptionPane.showMessageDialog(null, "Computer choses Scissors. You win.");

else if (case3)

JOptionPane.showMessageDialog(null, "Computer choses Rock. You win.");

else if (case4)

JOptionPane.showMessageDialog(null, "Computer choses Scissors. You lose.");

else if (case5)

JOptionPane.showMessageDialog(null, "Computer choses Rock. You lose.");

else if (case6)

JOptionPane.showMessageDialog(null, "Computer choses Paper. You win.");

else if (case7)

JOptionPane.showMessageDialog(null, "Both choose Rock. Tie.");

else if (case8)

JOptionPane.showMessageDialog(null, "Both choose Paper. Tie.");

else if (case9)

JOptionPane.showMessageDialog(null, "Both choose Scissors. Tie.");

System.exit(0);

}

}

5 answers to this question

Recommended Posts

  • 0

case1 = (player == 1) && (computer == 2);

Thank you but jgrasp still stops at case1. It now says

??RockPaperScissors.java:18: error: incomparable types: String and int

?ϧ? case1 = (player == 1) && (computer == 2);

?ϧ? ^

  • 0

The human player is providing a number when asked - but the data type returned by that function is actually a String. You need to convert that string into an Integer to successfully compare it to the computer's selection (which is an integer).

See Integer.parseInt

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Tim Cook: "The US over time began to stop having as many vocational kinds of skills." What's the point of wasting time getting those skills if you can't get a job with them? Good Lord, maybe he and his cohort of CEO's who exported all these jobs to China should just shut the f**k up :D
    • I made a new Cinematic/Trailer for the game, this will be the intro, still a work in progress!  I also updated the Steam page with a ton of new screenshots! 👀 https://store.steampowered.com/app/3925340/Incoherence_Dark_Rooms/  
    • Closed-loop cooling and a custom 800G network protocol let the $7.3B campus run as one AI training machine. Microsoft confirmed June 23, 2026, that its Fairwater campus in Mount Pleasant, Wisconsin, is fully operational — and the engineering behind it makes the facility something fundamentally different from every data center that came before it. Where conventional cloud infrastructure racks up general-purpose servers and parcels out workloads to each one independently, Fairwater links hundreds of thousands of NVIDIA GB200 Blackwell GPUs into a single, coherent cluster using a two-story building design, 800-gigabit-per-second Ethernet fabric, and a proprietary networking protocol co-developed with OpenAI and NVIDIA. The result, according to Microsoft, is the closest thing to a purpose-built AI supercomputer that any company has ever placed in commercial operation. https://www.techtimes.com/articles/319205/20260627/microsoft-opens-fairwater-wisconsin-ai-campus-runs-one-supercomputer-via-800g-ethernet.htm  
  • Recent Achievements

    • Conversation Starter
      jessse3334 earned a badge
      Conversation Starter
    • Reacting Well
      JuvenileDelinquent earned a badge
      Reacting Well
    • One Month Later
      Excellence2025 earned a badge
      One Month Later
    • Week One Done
      Excellence2025 earned a badge
      Week One Done
    • Week One Done
      flexorcist earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      +Edouard
      211
    3. 3
      PsYcHoKiLLa
      150
    4. 4
      Steven P.
      73
    5. 5
      macoman
      62
  • Tell a friend

    Love Neowin? Tell a friend!