intro java coding. don't understand what i'm doing wrong - java

i'm really not sure what's wrong with my code. It's supposed to do rock paper scissors against the computer by taking in the user choice, comparing it to the random computer choice, and displaying the results.
I get two errors that i have no return statements for the 3rd and 4th methods. Also, when i run it without fixing the errors, the nested if statements starting at line 60 only print out one of the two println statements, which really makes zero sense to me.
import java.util.Random;
import java.util.Scanner;
public class Chapter5ProjectPart2 {
public static void main(String[] args) {
Random generator = new Random();
Scanner keyboard = new Scanner(System.in);
int userNum;
int compNum;
String userChoice = "";
String compChoice = "";
int rnum;
int result = 0;
boolean keepPlaying;
int input = 1;
do
{
compNum = generator.nextInt(2)+1;
compChoice = numToChoice(compNum);
menu();
userNum = keyboard.nextInt();
userChoice = numToChoice(userNum);
keyboard.nextInt();
System.out.println();
System.out.println("you chose " + userChoice);
System.out.println("the computer chose " + compChoice);
result = resultCheck(userNum, compNum);
if (result == 1) // user wins
{
if (userNum == 1) //user won choosing rock
{
System.out.println("rock beats scissors");
System.out.println("you win");
}
else if (userNum == 2) //user won choosing paper
{
System.out.println("paper beats rock");
System.out.println("you win");
}
else if (userNum == 3) //user won choosing scissors
{
System.out.println("scissors beats paper");
System.out.println("you win");
}
}
else if (result == 3) //user loses
{
if (userNum == 1) //user lost choosing rock
{
System.out.println("paper beats rock");
System.out.println("you lose");
}
else if (userNum == 2) //user lost choosing paper
{
System.out.println("scissors beats paper");
System.out.println("you lose");
}
else if (userNum == 3) //user lost choosing scissors
{
System.out.println("rock beats scissors");
System.out.println("you lose");
}
else if (result == 2) //draw
System.out.println("draw");
}
System.out.println("would you like to play again?");
System.out.println("1 = yes");
System.out.println("2 = no");
input = keyboard.nextInt();
keepPlaying = play(input);
} while (keepPlaying == true);
}
// method 1 (menu)
public static void menu()
{
System.out.println("Enter your choice of rock, paper, or scissors\n" + "1 = rock\n" + "2 = paper\n" + "3 = scissors");
}
// method 2 (result check)
public static int resultCheck(int userNum, int compNum)
{
if (userNum == 2 && compNum == 1)
return 1;
else if (userNum == 1 && compNum == 3)
return 1;
else if (userNum == 3 && compNum == 2)
return 1;
else if (userNum == compNum)
return 2;
else
return 3;
}
// method 3 (converting number choice to rock/paper/scissors
public static String numToChoice(int num)
{
if (num == 1)
return "rock";
else if (num == 2)
return "paper";
else if (num == 3)
return "scissors";
}
//method 4 (play again)
public static boolean play(int input)
{
if (input == 1)
return true;
else if (input == 2)
return false;
}
}

I get two errors that i have no return statements for the 3rd and 4th methods.
Right. Let's look at the third:
public static String numToChoice(int num)
{
if (num == 1)
return "rock";
else if (num == 2)
return "paper";
else if (num == 3)
return "scissors";
}
Suppose num isn't 1, 2, or 3? Then what should the return value of the method be? That's why you're getting the error, you need a final else (with no if) saying what that return value should be when none of the earlier branches has returned a value. Without it, the method causes a compile-time error.
Also, when i run it without fixing the errors, the nested if statements starting at line 60 only print out one of the two println statements, which really makes zero sense to me.
You can't run it without fixing the errors, because these are compile-time errors. If you try to compile this source code with those errors in place, it fails, and you don't get an updated class file. So if you then try to run, and it seems to work, you're running an earlier copy of the class file you compiled before those errors were there. That class file doesn't relate to the current source code, and so it's understandable that it would make no sense to you. You're not looking at what the JVM is running.
If you correct the methods so that things compile (by adding the final else with no if on it), then run the compiled result, things should make more sense. Meanwhile, you might want to delete the previous Chapter5ProjectPart2.class file, since it's out of date.

public static String numToChoice(int num)
{
if (num == 1)
return "rock";
else if (num == 2)
return "paper";
else if (num == 3)
return "scissors";
}
//method 4 (play again)
public static boolean play(int input)
{
if (input == 1)
return true;
else if (input == 2)
return false;
}
These methods should always return something. Method 3 for example, if int num is 4 it won't return anything. Solve it by adding:
else return "";

That's because you are not ending your else-if statement in methods 3 and 4 with an else clause.

You indeed lack return statements. Java expects a non-void method to always return a value or throw an exception. Having a non-void method end without returning or throwing an exception is an error.
public static String numToChoice(int num)
{
if (num == 1)
return "rock";
else if (num == 2)
return "paper";
else if (num == 3)
return "scissors";
}
Here, your method doesn't return anything if num isn't 1, 2 or 3. Likewise:
public static boolean play(int input)
{
if (input == 1)
return true;
else if (input == 2)
return false;
}
Here inputs that aren't 1 or 2 lack their return statements.

First of all, never run code that doesn't compile.
Second: examine this method:
public static boolean play(int input)
{
if (input == 1)
return true;
else if (input == 2)
return false;
}
What would the method return if input is 6, or -67, or 789? You didn't tell, and the compiler can't guess it. So it refuses to compile until you tell it what to return in these cases.
If the other cases should never happen, then throw an exception:
public static boolean play(int input)
{
if (input == 1)
return true;
else if (input == 2)
return false;
else {
throw new IllegalStateException("input is " + input + ". Something's really wrong");
}
}

Java method MUST return a value unless it states void as return.
In your play method, if input is not 1 or 2, Java won't return any value. This does not allow to compile in Java.
In your numToChoice method, if num is not 1, 2 or 3, Java won't return any value. This does not allow to compile in Java.
Add an else close to return a value in "unexpected" cases, and allow Java to compile.

Related

Method returns proper value but if loop does not act accordingly

I've written out all the code for an assignment for my programming class. The assignment requires us to create a program that allows the user to play rock paper scissors against a computer. We're required to have individual methods to obtain the computer's choice, the user's choice, check if the user's choice is valid, and also to determine the winner of the match. If a match doesn't end in a tie we need to print why the match was won, eg. "Scissors cuts Paper," and print who wins. Everything works properly, except when the user wins it's never counted or announced. For example instead of printing:
The computer's choice was rock. The user's choice was paper. Paper covers Rock. The user wins!
it prints:
The computer's choice was rock. The user's choice was paper. Paper covers Rock.
import java.util.Scanner;
import java.util.Random;
public class FinalRockPaperScissors {
//Computer's Choice
public static int computersChoice (int options) {
Random randGen = new Random();
int computerValue = randGen.nextInt(options)+1;
System.out.println(computerValue); //FOR TESTING ONLY
return computerValue;
}
//Player's Choice
public static int usersChoice () {
Scanner scnr = new Scanner(System.in);
System.out.print("Enter 1 for rock, 2 for paper, or 3 for scissors: ");
int userValue = scnr.nextInt();
if (isValid(userValue) == true) {
return userValue;
}
else {
userValue = 0;
return userValue;
}
}
//Check for valid user input
public static boolean isValid (int userInput){
if (userInput == 1 || userInput == 2 || userInput == 3) {
return true;
}
else {
return false;
}
}
//Checking winner
public static char determineWinner () {
char win;
int computerValue = computersChoice(3);
int userValue = usersChoice();
//print computer choices
if (computerValue == 1) {
System.out.println("The computer's choice was rock.");
}
else if (computerValue == 2) {
System.out.println("The computer's choice was paper.");
}
else if (computerValue == 3){
System.out.println("The computer's choice was scissors.");
}
//print user choices
if (userValue == 1) {
System.out.println("The user's choice was rock.");
}
else if (userValue == 2) {
System.out.println("The user's choice was paper.");
}
else if (userValue == 3){
System.out.println("The user's choice was scissors.");
}
//check who won
if (computerValue == 1) { //rock vs
if (userValue == 2) { //paper
System.out.println("Paper wraps Rock.");
return win = 'b';
}
else if (userValue == 3) { //scissors
System.out.println("Rock smashes Scissors.");
return win = 'a';
}
else if (userValue == 1){ //rock
return win = 'c';
}
else {
System.out.println("The user chose an invalid number. This round will be ignored.");
return win = 'd';
}
}
else if (computerValue == 2) { //paper vs
if (userValue == 2) { //paper
return win = 'c';
}
else if (userValue == 3) { //scissors
System.out.println("Scissors cuts Paper.");
return win = 'b';
}
else if (userValue == 1){ //rock
System.out.println("Paper wraps Rock.");
return win = 'a';
}
else {
System.out.println("The user chose an invalid number. This round will be ignored.");
return win = 'd';
}
}
else { //scissors vs
if (userValue == 2) { //paper
System.out.println("Scissors cuts Paper.");
return win = 'a';
}
else if (userValue == 3) { //scissors
return win = 'c';
}
else if (userValue == 1){ //rock
System.out.println("Rock smashes Scissors.");
return win = 'b';
}
else {
System.out.println("The user chose an invalid number. This round will be ignored.");
return win = 'd';
}
}
}
public static void main(String[] args) {
int userWins = 0;
int computerWins = 0;
int ties = 0;
int error = 0;
//for (int i = 0; i < 1; i++) { //5 for testing purposes
if (determineWinner() == 'a') {
System.out.println("The computer wins!");
System.out.println("");
computerWins++;
}
else if (determineWinner() == 'b') {
System.out.println("The user wins!");
System.out.println("");
userWins++;
}
else if (determineWinner() == 'c'){
System.out.println("The game is tied!");
System.out.println("");
ties++;
}
else {
error++;
}
//}
System.out.println("The number of ties is " + ties);
System.out.println("The number of user wins is " + userWins);
System.out.println("The number of computer wins is " + computerWins);
//output final winner
if (computerWins > userWins) {
System.out.println("Computer is the winner.");
}
else if (userWins > computerWins) {
System.out.println("User is the winner.");
}
else {
if (userWins == computerWins) {
System.out.println("User is the winner.");
}
else if (computerWins == ties) {
System.out.println("Computer is the winner.");
}
}
}
}
After some testing I've discovered that the problem may be with my userchoice() method. If I disable this method and give a set value for the user, everything works as it should. The problem is I don't know why it doesn't work, and as a result I can't fix it.
I think you're so new to Java and also let's start to show you your mistake in this code, you called determineWinner() multiple times which shouldn't be like that because you repeat the game four times to determine the result once, so you should call it once and get the returned value and check the value, like:-
char result = determineWinner(); //Play the game and get the result
// use it in conditions
if (result == 'a') {
System.out.println("The computer wins!");
System.out.println("");
computerWins++;
}
else if (result == 'b') {
System.out.println("The user wins!");
System.out.println("");
userWins++;
}
else if (result == 'c'){
System.out.println("The game is tied!");
System.out.println("");
ties++;
}
else {
error++;
}
In your main function You are calling determineWinner() every time you test your if condition. The proper way would be to store the returned value in a variable and then check if that variable is 'a', 'b' or 'c'. For example:
char result = determineWinner();
if (result == 'a') {
System.out.println("The computer wins!");
System.out.println("");
computerWins++;
}

For Loops In Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
So I'm creating a simple rock paper scissors and I have made a mistake recently. I'm just beginning to learn and I made a mistake and lost track of where. I'd really really appreciate any pointers of where I made a mistake. When it prints out the prompt, I type in what I want, and it just prints it out again. Thank you!
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class rock_Paper_Scissor {
public static void main (String[] args) {
String playerhand;
boolean x = true;
Scanner input = new Scanner(System.in);
Random num = new Random();
int rand = num.nextInt(2) + 1;
System.out.println("I challenge you to Rock Paper Scissor");
System.out.println("If you want to quit, type exit twice");
System.out.println("Type Rock, Paper, or scissor");
playerhand = input.nextLine();
String hands = playerhand.toLowerCase();
while (x == true) {
if (hands == "rock") {
if (rand == 1) {
System.out.println("Rock vs. Rock: TIE");
} else if (rand == 2) {
System.out.println("Rock vs. Scissor: YOU WIN");
} else if (rand == 3) {
System.out.println("Rock vs. Paper: YOU LOSE");
}
}
else if (hands == "paper") {
if (rand == 1) {
System.out.println("Paper vs. Rock: YOU WIN");
} else if (rand == 2) {
System.out.println("Paper vs. Scissor: YOU LOSE");
} else if (rand == 3) {
System.out.println("Paper vs. Paper: TIE");
}
}
else if (hands == "scissor") {
if (rand == 1) {
System.out.println("Scissor vs. Rock: YOU LOSE");
} else if (rand == 2) {
System.out.println("Scissor vs. Scissor: TIE");
} else if (rand == 3) {
System.out.println("Scissor vs. Paper: YOU WIN");
}
}
else if (hands == "exit") {
System.out.println("Thank you for playing!");
x = false;
}
System.out.println("Please type your hand to play again: ");
hands = input.nextLine();
}
}
}
In your all if condition try to use eqauls() method instead of == like this:
if ("rock".equals(hands)) {
...
else if ("paper".equals(hands)) {
...
else if ("scissor".equals(hands )) {
...
else if ("exit".equals(hands)) {
.equals() is used to compare strings values.
But == compare with references strings.

Need to get the entire thing in a loop, which loop is best suited? Where should I apply it?

I am nearly finished with a Java project of mine. The objective is to play a game of Rock Paper Scissors with the user and the computer. The computer generates a random number and that number must correlate with either one of the three choices. I got it to where it can play successfully, but here is the catch, in the case of a tie, the game is to repeat and start over, otherwise it is to terminate. I have it to where it terminates, but cannot figure out how to get it to repeat the entire process in the case of a tie. The methods must remain the way they are, but my professor said the answer is if the entire thing is in a loop. My question is which loop should I use and where should it be placed? Here is the code:
public class FahrDylanRockPaperScissors{
public static void main (String [] args){
String computer = computerChoice();
String user = userChoice();
determineWinner(computer, user);
}
public static String computerChoice( ){
Random rand = new Random();
int cinput = rand.nextInt(3)+ 1;
String computer = "thing";
if (cinput == 1)
computer = "Rock";
if (cinput == 2)
computer = "Paper";
if (cinput == 3)
computer = "Scissors";
return computer;
}
public static String userChoice(){
Scanner sc = new Scanner (System.in);
String user = "default";
do{
System.out.println ("Let's Play a game! Rock, Paper, Scissors!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + "\nPlease enter either Rock, Paper, or Scissors: " + "\nGood Luck!");
user = sc.nextLine();
}
while (isValidChoice (user) == false);
return user;
}
public static boolean isValidChoice(String choice){
boolean status;
if (choice.compareTo("Rock")== 0)
status = true;
else if (choice.compareTo("Paper")== 0)
status = true;
else if (choice.compareTo("Scissors")== 0)
status = true;
else{
status = false;
System.out.println("Error! Make sure you are capitalizing your choices");
}
return status;
}
public static void determineWinner(String computer, String user){
System.out.println (" Computer Choice: " + computer);
System.out.println ("Your Choice : " + user);
if (computer.compareTo( "Rock" ) == 0 && user.compareTo ("Scissors") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
System.out.println (" You win!!");
else if (computer.compareTo(user) == 0 )
System.out.println(" Tie! the game must be played again.");
}
}
You can use a do-while loop, because your code needs to be executed at least one time.
public static void main (String [] args){
boolean win = false;
do{
String computer = computerChoice();
String user = userChoice();
win = determineWinner(computer, user);
}while(!win);
}
For the first time you execute the whole code. Then the predicate is checked, and if someone won, the do-while will stop. But if win equals false it will be executed again.
You could achieve the same with only a while loop, or other loops. But because your code needs to be run at least one time a do-while suits well.
Edit:
You need to change your code, so that determineWinner returns back if someone won (return true) or if there is a tie (return false). I did not see that it currently has no return type when posting the answer.
A simple way to get the determineWinner method to work would be the following.
public static boolean determineWinner(String computer, String user){
System.out.println (" Computer Choice: " + computer);
System.out.println ("Your Choice : " + user);
if (computer.compareTo( "Rock" ) == 0 && user.compareTo ("Scissors") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
System.out.println (" Computer wins! Better luck next time!");
if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
System.out.println (" You win!!");
if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
System.out.println (" You win!!");
else if (computer.compareTo(user) == 0 ){
System.out.println(" Tie! the game must be played again.");
return false;
}
return true;
}
And for your coding style:
It's good practice to use brackets {} for if/else/for... even if you have only one statement, because it improves the readability of your code.
Edit 2:
Because you can't change something, the easiest way is probably the following:
public static void main(String[] args){
boolean tie = true;
do{
String computer = computerChoice();
String user = userChoice();
tie = (computer.compareTo(user) == 0);
determineWinner(computer, user);
}while(tie);
}
Even if determineWinner determines the winner you need it to give you feedback. If you can't get feedback, just determine if there will be a tie in your main-Method, and if you get a tie, repeat.
You could easily just make the three lines:
public static void playGame()
{
String computer = computerChoice();
String user = userChoice();
determineWinner(computer, user);
}
into one new method, something like playGame(). Then in the main function, you can call playGame(), and at the end of determineWinner(), you could have:
else if (computer.compareTo(user) == 0 )
{
System.out.println(" Tie! the game must be played again.");
playGame();
}
to play the game again.
Also, make sure you include the import statements for Random and Scanner. And make sure you close your scanner.
Use a flag for tie, something like this:
boolean tie = false;
combine with do while:
do {
} while (tie);
if tie == true than keep going until its false.
A do { ... } while () loop would be most natural for this problem, but pretty much any loop construct could serve.
Although you say
The methods must remain the way they are
you cannot do it if the determineWinner() method remains exactly as it now is. That method needs one way or another to communicate back to main() whether there is a tie, or else it must itself manage running a new game in the event of a tie. Were I free to do so, I would have it return a a boolean value indicating whether there was a tie. If it cannot be modified to return a value, then the other alternative would be for it to set a static variable to indicate whether there was a tie. Alternatively, you could solve this problem with recursion, but that would not involve a loop.

Java String Method returning Null?

I was tasked with making a simple program that will play Rock-Paper-Scissors with the user. Unfortunately, when I try to run the program, my return(sentence+outcome) both returns null. I am new to using methods, so please explain what I am doing wrong here... Thank you!
package rockpaperscissors;
/**
*
* #author Owner
*/
import java.util.Scanner;
import java.io.IOException;
public class RockPaperScissors {
static int weaponChoice;
static int computerChoice;
static int tie = 0;
static int lose = 0;
static int win = 0;
static String outcome;
static String sentence;
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
Scanner userInput = new Scanner(System.in);
System.out.println(" =========================");
System.out.println("====ROCK=PAPER=SCISSORS====");
System.out.println(" =========================");
int playAgain = 1; //sets a play again function
do {
System.out.println("Please select your weapon.");
System.out.println("1 - Rock");
System.out.println("2 - Paper");
System.out.println("3 - Scissors");
System.out.println("Choose wisely:");
String choice = userInput.nextLine();
weaponChoice = Integer.parseInt(choice);
do {
if (weaponChoice != 1 && weaponChoice != 2 && weaponChoice != 3) {
System.out.println("Please choose again, grasshopper. There are only"
+ " three choices.");
System.out.println("1 - Rock");
System.out.println("2 - Paper");
System.out.println("3 - Scissors");
choice = userInput.nextLine();
weaponChoice = Integer.parseInt(choice);
}
} while (weaponChoice != 1 && weaponChoice != 2 && weaponChoice != 3);
if (weaponChoice == 1) {
System.out.println("You have selected Rock as your weapon.");
} else if (weaponChoice == 2) {
System.out.println("You have selected Paper as your weapon.");
} else {
System.out.println("You have selected Scissors as your weapon.");
}
//Computer's Choice
computerChoice = 1 + (int) (Math.random() * ((3 - 1) + 1));
if (computerChoice == 1) {
System.out.println("The computer has chosen Rock.");
} else if (computerChoice == 2) {
System.out.println("The computer has chosen Paper.");
} else {
System.out.println("The computer has chosen Scissors.");
}
determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);
System.out.println("==SCORES==");
System.out.println("WINS: " + win);
System.out.println("TIES: " + tie);
System.out.println("LOSSES: " + lose);
System.out.println("Press 1 to play again, or any other number to exit.");
String play = userInput.nextLine();
playAgain = Integer.parseInt(play);
} while (playAgain == 1);
}
public static String determineOutcome(String outcome, String sentence) {
sentence = "Your result is: ";
do {
if (weaponChoice == 1 && computerChoice == 1 || weaponChoice == 2 && computerChoice == 2 || weaponChoice == 3 && computerChoice == 3) {
tie++;
outcome = "TIE";
} else if (weaponChoice == 1 && computerChoice == 3 || weaponChoice == 2 && computerChoice == 1 || weaponChoice == 3 && computerChoice == 2) {
win++;
outcome = "You WON!";
} else {
lose++;
outcome = "You LOSE. Better luck next time?";
}
} while (lose <0 || win < 0 || tie < 0);
return(sentence+outcome);
}
}
To make this work, you'll need to replace
determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);
with
String valueReturned = determineOutcome(outcome, sentence);
System.out.println(valueReturned);
because Java is pass by value, not pass by reference. That means that the determineOutcome method will work with its own copies of outcome and sentence, and not modify the versions that belong to the method that called it.
But also, the method is not actually using the two arguments you pass into it. It would be much less confusing if you just omit the parameters entirely, and change it to public static String determineOutcome() ... and declare String sentence; and String outcome; inside the method.
When calling a function that is returning a result you must either call the function within System.out.println() or assign the result to a variable which you then print.
Option 1:
String result = determineOutcome(outcome, sentence);
System.out.println(result);
or option 2:
System.out.println(determineOutcome(outcome, sentence));
In addition to the points other people made in their answers, it looks like your while loop is never being executed. The variables tie, win and lose are never less than 0, so the condition of the while loop is never true. You can try changing it to:
while (lose <= 0 || win <= 0 || tie <= 0);
But be warned, this will result in an infinite loop, so you may need to rethink your logic. You may not need a loop here, depending on what you're trying to do.

Error in Rock Paper Scissors program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The original prompt had us making a rock paper scissors game I think my code should work so far but I am getting the error "cannot find symbol" in line 64 when I try to track if a tie happens. Does anyone know what I am doing wrong?
import java.util.*;
public class RPS
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
boolean n = false;
System.out.println("Welcome to the Rock, Paper, Scissors game!!");
System.out.println("When you play, your choices are rock, paper, or scissors");
do
{
int compChoice = myRandom.nextInt(3);
String rock = "0";
String paper = "1";
String scissors = "2";
Integer.parseInt(rock);
Integer.parseInt(paper);
Integer.parseInt(scissors);
int compWins = 0;
int humanWins = 0;
int ties = 0;
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(myScanner.nextLine());
for(int i = 0; i < 3; i++)
{
if(choice == 0 && compChoice == 1)
{
System.out.println("I choose paper: I win this turn");
i++;
compWins++;
} else if(choice == 0 && compChoice == 2)
{
System.out.println("I choose scissors: You win this turn");
i++;
humanWins++;
} else if(choice == 1 && compChoice == 0)
{
System.out.println("I choose rock: You win this turn");
i++;
humanWins++;
} else if(choice == 1 && compChoice == 2)
{
System.out.println("I choose scissors: I win this turn");
i++;
compWins++;
} else if(choice == 2 && compChoice == 0)
{
System.out.println("I choose rock: I win this turn");
i++;
compWins++;
} else if(choice == 2 && compChoice == 1)
{
System.out.println("I choose paper: You win this turn");
i++;
humanWins++;
} else if(choice == compchoice)
{
System.out.println("This round is a tie");
i++;
ties++;
}
System.out.println("Score: me: " + compWins + "you: " + humanWins + "ties: " + ties);
}
if(compWins > humanWins)
{
System.out.println("Game over: I win!");
} else if(humanWins > compWins)
{
System.out.println("Game over: You win!");
}else
System.out.println("Game over: No one wins!");
System.out.print("Play again? (y/n)");
String ending = myScanner.nextLine();
if(ending == n)
{
n = true;
}
}
while(!n);
System.out.println("Thank you for playing!");
System.exit(0);
}
}
compchoice is not camel case. You declared it as compChoice.
You have 2 errors:
Java is case-sensitive, and you are using compchoice which is not declared anywhere. I guess you wanted to use compChoice instead:
} else if (choice == compChoice) {
You are comparing a String with a boolean in the line
if (ending == n) {
which is not a valid comparison because they are from different types. You may want to check your logic there.
Actually Java is case sinsitive, when you declared a variable you have to stick with that declaration.
So try to change :
} else if(choice == compChoice) // here you need to change compchoice to compChoice
{
System.out.println("This round is a tie");
i++;
ties++;
}
System.out.println("Score: me: " + compWins + "you: " + humanWins + "ties: " + ties);
Also You comparing a String with boolean, you need to parse the String to boolean then compare:
boolean ending = Boolean.valueOf(myScanner.nextLine());
if(ending == n)
{
n = true;
}

Categories

Resources