Jump to content

Java Rock Paper Scissors Help

Hello,

I was wondering if somebody could help me with this Java, i'm still learning, only started about a week ago, and I just cannot figure out what to do from here. I need the user to be able to enter in a win number and then have that many games be played to win. I also need to be able to have a constant score board with Computer Wins, Human Wins, and Ties. 

 

import javax.swing.JOptionPane;
import java.util.Random;

public class RPS2
{
    public static void main(String args[])
    {Random R = new Random();
    		int myNum = R.nextInt(3)+1;
    			
    		//	System.out.println(52 6f 73 65);
    		String numInputA = JOptionPane.showInputDialog("How many wins will end the tournament?");
    			double numInput2 = Double.parseDouble(numInputA);
    		String numInput = JOptionPane.showInputDialog("Which would you like?:\n\n1.Rock\n2.Paper\n3.Scissors");
    			double numInput1 = Double.parseDouble(numInput);
    			
    		if (myNum==1) {
    			JOptionPane.showMessageDialog(null,"I Pick Rock");
    		}
    			else
    		if (myNum==2) {
    			JOptionPane.showMessageDialog(null,"I Pick Paper");
    		}
    			else
    		if (myNum==3) {
    			JOptionPane.showMessageDialog(null,"I pick Scissors");
    		}
    		//
    		if (numInput1==1 && myNum==3) {
    			JOptionPane.showMessageDialog(null,"You Won\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		else
    		if (numInput1==2 && myNum==2) {
    			JOptionPane.showMessageDialog(null,"You Lose\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		else
    		if (numInput1==3 && myNum==1) {
    			JOptionPane.showMessageDialog(null,"You Tied\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		//
    		if (numInput1==2 && myNum==1) {
    			JOptionPane.showMessageDialog(null,"You Won\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		else
    		if (numInput1==2 && myNum==3) {
    			JOptionPane.showMessageDialog(null,"You Lose\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		else
    		if (numInput1==2 && myNum==2) {
    			JOptionPane.showMessageDialog(null,"You Tied\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    				//
    		if (numInput1==3 && myNum==2) {
    			JOptionPane.showMessageDialog(null,"You Won\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		else
    		if (numInput1==3 && myNum==1) {
    			JOptionPane.showMessageDialog(null,"You Lose\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		else
    		if (numInput1==3 && myNum==3) {
    			JOptionPane.showMessageDialog(null,"You Tied\n\n1.Wins\n2. Computer\n3. Humans\n4. Ties");
    		}
    		
    }
    
    
}

}

 

IM BACK BABY

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, kurtstir said:

Hello,

I was wondering if somebody could help me with this Java, i'm still learning, only started about a week ago, and I just cannot figure out what to do from here. I need the user to be able to enter in a win number and then have that many games be played to win. I also need to be able to have a constant score board with Computer Wins, Human Wins, and Ties. 

-snip-

 

Grab input and run the entire game in a for loop to start at 1 and increment every time a game is played until the your loop counter variable is greater than the integer input.

 

Poke me on PM and I might be able to help actually write that and a bit more later, those if/else's are untidy and a switch statement would be much cleaner if it's possible.

 

Also, that's Java, not JavaScript... Maybe I won't be able to help as much. I know you'll need to import Scanner or something like it to get input.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Store the required winnings, winnings, loses, ties into variables, for example int. Then you can easily access them. If you need to have it persistent, save it to a file and load when app is opened.

 

As for the UI, it looks weird/incomplete, but I have not used that myself and may be wrong.

As @LtStaffel said, you need to use Scanner if you need to get the input from console. If you are using a GUI, then it depends on the framework you use.

 

And yea, as said above, this is Java, not JavaScript. There's a very big difference.

HAL9000: AMD Ryzen 9 3900x | Noctua NH-D15 chromax.black | 32 GB Corsair Vengeance LPX DDR4 3200 MHz | Asus X570 Prime Pro | ASUS TUF 3080 Ti | 1 TB Samsung 970 Evo Plus + 1 TB Crucial MX500 + 6 TB WD RED | Corsair HX1000 | be quiet Pure Base 500DX | LG 34UM95 34" 3440x1440

Hydrogen server: Intel i3-10100 | Cryorig M9i | 64 GB Crucial Ballistix 3200MHz DDR4 | Gigabyte B560M-DS3H | 33 TB of storage | Fractal Design Define R5 | unRAID 6.9.2

Carbon server: Fujitsu PRIMERGY RX100 S7p | Xeon E3-1230 v2 | 16 GB DDR3 ECC | 60 GB Corsair SSD & 250 GB Samsung 850 Pro | Intel i340-T4 | ESXi 6.5.1

Big Mac cluster: 2x Raspberry Pi 2 Model B | 1x Raspberry Pi 3 Model B | 2x Raspberry Pi 3 Model B+

Link to comment
Share on other sites

Link to post
Share on other sites

@kurtstir lets sort your logic a little bit, make it neater.

 

#1 = rock
#2 = paper
#3 = scissors 

if mynum == comp then draw

else if mynum = 1
	if comp = 3 then you win
	else you lose
else if mynum = 2
	if comp = 1 then you win
	else you lose
else if mynum = 3
	if comp = 2 then you win
	else you lose

here is a version in python for you to look over, different syntax but logic is the same.

 

from random import randint
#1 rock
#2 paper
#3 scissors

wins= 0
lose = 0
draw = 0

games = int(input("Enter number of games to play"))
for i in range(games):
  myNum = randint(1,3)
  comNum = randint(1,3)
  if(myNum == comNum):
    print("You drew")
    draw += 1
  elif(myNum == 1):
    if(comNum == 3):
      print("You win")
      wins += 1
    else:
      print("You lose")
      lose += 1
  elif(myNum == 2):
    if(comNum == 1):
      print("You win")
      wins +=1
    else:
      print("You lose")
      lose += 1
  elif(myNum == 3):
    if(comNum == 2):
      print("You win")
      wins += 2
    else:
      print("You lose")
      lose += 1
  #end of loop
print("You won {} lost {} and drew {}".format(wins, lose, draw))

Output

Enter number of games to play 5
You win
You lose
You win
You drew
You lose
You won 3 lost 2 and drew 1

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×