简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM