0

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);
    } 
}
4
  • Where is weaponChoice and computerChoice declared or assigned? Commented Nov 30, 2014 at 22:59
  • Edited it to include entire code. @DavidWallace Could you possibly explain a bit more? Commented Nov 30, 2014 at 23:01
  • @AardenHartle Which part of my answer is causing you difficulty? Commented Nov 30, 2014 at 23:02
  • For simplicity, you could replace if (weaponChoice == 1 && computerChoice == 1 || weaponChoice == 2 && computerChoice == 2 || weaponChoice == 3 && computerChoice == 3) with if (weaponChoice == computerChoice). Commented Nov 30, 2014 at 23:48

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help. I just needed help understanding. Thank you again!
It doesn't look like the input variables are being assigned before the method is called so you're actually probably better off not using any parameters at all.
Oh, right, I see he's edited the question since I wrote my answer. I'll have to have a look at the new code.
1

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));

Comments

0

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.

1 Comment

Given what we expect him to be doing, he almost certainly doesn't want the loop at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.