rock paper scissors program

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2757 Views - Last Post: 09 October 2014 - 02:21 PM Rate Topic: -----

#16 melina2134   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 23-September 14

Re: rock paper scissors program

Posted 08 October 2014 - 02:28 PM

View Postmacosxnerd101, on 08 October 2014 - 01:11 PM, said:

Two points.
  • Why are you still not properly indenting your code? Please do this. It will make your code easier for everyone to read and debug; including most importantly, yourself.

  • The bulk of your code is outside the main() method. You should be getting a lot of compilation errors. We have already addressed this issue before.


how is it outside my main method.... its all in brackets..... i cant find an answer in the tutorial links you sent me or anything i've searched. ive labeled programs with this main method like this and theyve worked just fine
Was This Post Helpful? 0
  • +
  • -

#17 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: rock paper scissors program

Posted 08 October 2014 - 02:32 PM

Check out my last post. Your code was so hard to read that it looked like it was outside the main() method. This is why you need to start properly indenting your code, so folks who want to help you can follow the logic.

By the way- I indented your code and addressed your issues in my last post.
Was This Post Helpful? 0
  • +
  • -

#18 TimHGMoon   User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 190
  • Joined: 04-September 14

Re: rock paper scissors program

Posted 09 October 2014 - 12:10 PM

View Postmelina2134, on 08 October 2014 - 12:24 PM, said:

i just need to know what's wrong with my code. i've looked at many beginner coding websites that show the if then statements and even then i couldnt fix this. its supposed to play rock paper scissors with the computer however the input "0" is always either a draw or it wont even respond with whether or not the user lost. input "2" you only lose. "1" is always a draw. "0" as well. ???

import java.util.*;
import java.util.Scanner;
import java.lang.Math;

public class Lab3
{
	
		public static void main(String[] args){
		{
			System.out.println("Play rock paper scissors! Choose: scissors (0), rock (1), or paper (2).");
			System.out.println();
		}
		Scanner input = new Scanner(System.in);
		int scissor = 0;
		int rock = 1;
		int paper = 2;
		Random random = new Random();
		int randomIndex = random.nextInt(2) + 0;
		int computer = randomIndex;
		int guess = input.nextInt();
	if (guess == computer){
			System.out.println("It's a draw!");
		}
	if (guess < computer | guess == 2 && computer== 0 ){
			System.out.println("You lost. Better luck next time!");
			}
	if (guess > computer | guess == 0 && computer == 2){
			System.out.println("You won!");
		}
	}
}



1. You don't need java.util.Scanner because you already have java.util.*
You don't need java.lang.Math because you're not using it at all.
int scissor = 0, int rock = 1, int paper = 2.
2.int randomIndex = random.nextInt(2) + 0; ... why are you adding it to 0? why are you adding anything to 0. A better way to do this would be to do int randomIndex = random.nextInt(3) + 1;
3. your if statement is way too complicated, you can just do
if(guess < computer)
{
System.out.println("You lost. Better luck next time!");
}
if (guess > computer)
{
System.out.println("You won!");
}


Was This Post Helpful? 0
  • +
  • -

#19 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: rock paper scissors program

Posted 09 October 2014 - 12:18 PM

View PostTimHGMoon, on 09 October 2014 - 02:10 PM, said:

1. You don't need java.util.Scanner because you already have java.util.*
You don't need java.lang.Math because you're not using it at all.
int scissor = 0, int rock = 1, int paper = 2.
2.int randomIndex = random.nextInt(2) + 0; ... why are you adding it to 0? why are you adding anything to 0. A better way to do this would be to do int randomIndex = random.nextInt(3) + 1;


Good catch on these. I feel that * imports are generally to be avoided - it's better to explicitly import the things you need. This serves as a kind of documentation: you can see what sorts of things the program will be doing by reading the imports. However, you're right that importing java.util.Scanner is not necessary if you're already importing java.util.*

Quote

3. your if statement is way too complicated, you can just do
if(guess < computer)
{
System.out.println("You lost. Better luck next time!");
}
if (guess > computer)
{
System.out.println("You won!");
}




Tragically, this doesn't work. Consider that we're dealing with a circular ranking: rock beats scissors beats paper beats rock.
Was This Post Helpful? 0
  • +
  • -

#20 TimHGMoon   User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 190
  • Joined: 04-September 14

Re: rock paper scissors program

Posted 09 October 2014 - 02:21 PM

View Postjon.kiparsky, on 09 October 2014 - 12:18 PM, said:

View PostTimHGMoon, on 09 October 2014 - 02:10 PM, said:

1. You don't need java.util.Scanner because you already have java.util.*
You don't need java.lang.Math because you're not using it at all.
int scissor = 0, int rock = 1, int paper = 2.
2.int randomIndex = random.nextInt(2) + 0; ... why are you adding it to 0? why are you adding anything to 0. A better way to do this would be to do int randomIndex = random.nextInt(3) + 1;


Good catch on these. I feel that * imports are generally to be avoided - it's better to explicitly import the things you need. This serves as a kind of documentation: you can see what sorts of things the program will be doing by reading the imports. However, you're right that importing java.util.Scanner is not necessary if you're already importing java.util.*

Quote

3. your if statement is way too complicated, you can just do
if(guess < computer)
{
System.out.println("You lost. Better luck next time!");
}
if (guess > computer)
{
System.out.println("You won!");
}




Tragically, this doesn't work. Consider that we're dealing with a circular ranking: rock beats scissors beats paper beats rock.


eep.. forgot about the circular ranking.. guess I got too far ahead of myself :/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2