![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2011
Posts: 3
Rep Power: 0
![]() |
I have my code done but there are some errors and i need help fixing them but not sure how take a look and maybe someone could help me thanks!
this is the assignment: Welcome to Rock, Paper, Scissors! Please enter the number of rounds you would like to play: 2 Sorry, you need to enter an odd number. Please try again: 3 Rock, Paper, or Scissors?: Monkey Sorry, “Monkey” is not a valid entry. Rock, Paper, or Scissors?: Rock Computer chooses Paper. You lose. Rock, Paper, or Scissors?: Rock Computer chooses Rock. It’s a tie. Rock, Paper, or Scissors?: Paper Computer chooses Rock. You win! Rock, Paper or Scissors?: Rock Computer chooses Scissors. You win! User wins: 2 Computer wins: 1 User wins the game! And here is my code. i also need help with the whole ...Rock, Paper, or Scissors?: Monkey Sorry, “Monkey” is not a valid entry.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissor{
public static void main(String [] args){
int computerRandom;
String userInput = "";
char userChoice = 'j' ;
char computerChoice = 'j';
int computerWin = 0;
int userWin = 0;
Random random = new Random();
Scanner reader = new Scanner (System.in);
System.out.print("\nPlease enter the number of rounds you would like to play ");
if (userChoice % 2 == 0) {
//Enter error message here.
System.out.println ("Sorry, enter an odd number of lines! Please try again.");
if (userChoice == '3'){
}
while(userChoice != 'r' && userChoice != 'p' && userChoice != 's'){
System.out.print("\nPlease input r for Rock p for Paper or s for Scissor: ");
userInput = reader.nextLine();
userChoice = userInput.charAt(0);
System.out.print("\n" +userChoice);
}
computerRandom = random.nextInt(3);
switch (computerRandom){
case 1: computerChoice = 'r'; break;
case 2: computerChoice = 'p'; break;
case 3: computerChoice = 's'; break;
}
if (computerChoice == userChoice){
System.out.print("\nTie ");
}else{
if (computerChoice == 'r'){
if (userChoice == 'p'){
System.out.print("\nPaper beats rock, you win!");
userWin++;
}
if (userChoice == 's'){
System.out.print("\nRock beats scissor, you lose!");
computerWin++;
}
}
if (computerChoice == 'p'){
if (userChoice == 'r'){
System.out.print("\nPaper beats rock, you lose!");
computerWin++;
}
if (userChoice == 's'){
System.out.print("\nScissors beat paper, you win!");
userWin++;
}
}
if (computerChoice == 's'){
if (userChoice == 'r'){
System.out.print("\nRock beats Scissor, you lose!");
computerWin++;
}
if (userChoice == 'p'){
System.out.print("\nPaper beats rock, you win!");
userWin++;
}
}
}
System.out.print("\nThe score is: user: " + userWin + " computer: " + computerWin);
}
} |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() ![]() ![]() ![]() |
Re: Rock, Paper Scissors
Quote:
Which aspect of this requirement are you struggling with? You should be able to write an "else" clause which prints 'Sorry, "X" is not a valid entry.'.
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges Rules | How to Post a Question | How to Post Code Becoming a good programmer requires foresight of your code's execution. Becoming an excellent programmer requires foresight of your code's modification. |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2011
Posts: 3
Rep Power: 0
![]() |
Re: Rock, Paper Scissors
i just wanted to make sure everything is running right? and can you give me an example of an else clause?
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() ![]() |
Re: Rock, Paper Scissors
Very few people here will go to the effort of running your code and comparing it against your assignment specifications for the benefit of doing your homework. That is something that you should not need our help to accomplish. But we are more than happy to listen to you identify your problems and then proceed to provide guidance or assistance.
Here is an example of an "else" clause: if (computerChoice == userChoice){
System.out.print("\nTie ");
}else{
// ... else code here ...
}You will also need to employ the use of else if.
__________________
PFO's Folding@Home Team | Sane's Monthly Algorithms Challenges Rules | How to Post a Question | How to Post Code Becoming a good programmer requires foresight of your code's execution. Becoming an excellent programmer requires foresight of your code's modification. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2011
Posts: 1
Rep Power: 0
![]() |
Re: Rock, Paper Scissors
You can try my code..
![]() import java.io.*;
class RockPaperScissors {
BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) {
RockPaperScissors RPS = new RockPaperScissors();
System.out.println("Welcome to Rock, Paper, or Scissors!");
System.out.print("Please enter the number of rounds you would like to play : ");
int round = RPS.rounds();
int user = 0;
int computer = 0;
for (int i = 0; i < round; i++) {
if (RPS.choose(i + 1))
user++;
else
computer++;
}
RPS.result(user, computer);
}
void result(int user, int computer) {
System.out.println("RESULTS:");
System.out.println("User wins : " + user);
System.out.println("Computer wins : " + computer);
if (user > computer)
System.out.println("User wins the game!\n");
else
System.out.println("Computer wins the game!\n");
}
int rounds() {
try {
int temp = Integer.parseInt(BR.readLine());
if (temp % 2 == 0)
throw new Exception();
return temp;
}
catch (Exception err) {
System.out.print("Sorry, you need to enter an odd number. Please try again : ");
return rounds();
}
}
boolean choose(int val) {
System.out.print("[Round " + val + "] Rock, Paper, or Scissors? : ");
String input = "";
try {
input = BR.readLine();
}
catch(Exception err) {
}
if (input.equals("Rock"))
return process(1, val);
else if (input.equals("Paper"))
return process(2, val);
else if (input.equals("Scissors"))
return process(3, val);
else {
System.out.println("Sorry, \"" + input + "\" is not a valid entry!\n");
return choose(val);
}
}
boolean process(int user, int val) {
String[] hand = {"", "Rock", "Paper", "Scissors"};
int comp = (int)(Math.random() * 3) + 1;
if (user == comp) {
System.out.println("Computer chooses " + hand[user] + ". It's a tie.\n");
return choose(val);
}
else if (user == 1 && comp == 3) {
System.out.println("Computer chooses " + hand[comp] + ". You win!\n");
return true;
}
else if (user == 2 && comp == 3) {
System.out.println("Computer chooses " + hand[comp] + ". You lose!\n");
return false;
}
else if (user == 1 && comp == 2) {
System.out.println("Computer chooses " + hand[comp] + ". You lose!\n");
return false;
}
else if (user == 3 && comp == 2) {
System.out.println("Computer chooses " + hand[comp] + ". You win!\n");
return true;
}
else if (user == 2 && comp == 1) {
System.out.println("Computer chooses " + hand[comp] + ". You win!\n");
return true;
}
else {
System.out.println("Computer chooses " + hand[comp] + ". You lose!\n");
return false;
}
}
} |
|
|
|
![]() |
| Bookmarks |
| Tags |
| java developer, java error eclipse, java programming, java programming help, java question |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| This Triangle Needs a Pair of Scissors | Smjprogrammer | C | 16 | Sep 24th, 2010 7:37 PM |
| Rock, Paper, Scissor Game Help | bsiguenza | Python | 1 | Mar 25th, 2009 1:39 PM |
| Grim's Hex Paper | grimpirate | Coder's Corner Lounge | 9 | Jun 21st, 2007 11:05 PM |
| Why IRC Channel Admins Rock | grimpirate | Coder's Corner Lounge | 8 | Dec 10th, 2006 5:51 PM |
| Help with rock paper scissor program | Batalia2 | Visual Basic .NET | 14 | Mar 21st, 2005 4:11 PM |