Java String Method返回Null?

时间:2014-11-30 22:56:43

标签: java methods

我的任务是创建一个简单的程序,与用户一起玩Rock-Paper-Scissors。不幸的是,当我尝试运行程序时,我的返回(句子+结果)都返回null。我是新手使用方法,所以请在这里解释我做错了什么......谢谢!

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

3 个答案:

答案 0 :(得分:2)

要使这项工作,您需要替换

determineOutcome(outcome, sentence);
System.out.println(sentence+outcome);

String valueReturned = determineOutcome(outcome, sentence);
System.out.println(valueReturned);

因为Java是按值传递的,而不是通过引用传递的。这意味着determineOutcome方法将使用其自己的outcomesentence副本,而不是修改属于调用它的方法的版本。

但是,该方法实际上并没有使用您传递给它的两个参数。如果您只是完全省略参数,并将其更改为public static String determineOutcome() ...并在方法中声明String sentence;String outcome;,那就不那么令人困惑了。

答案 1 :(得分:1)

当调用返回结果的函数时,您必须在System.out.println()中调用该函数,或者将结果分配给随后打印的变量。

选项1:

String result = determineOutcome(outcome, sentence);
System.out.println(result);

或选项2:

System.out.println(determineOutcome(outcome, sentence));

答案 2 :(得分:0)

除了其他人在答案中提出的观点之外,看起来你的while循环永远不会被执行。变量tiewinlose永远不会小于0,因此while循环的条件永远不会为真。您可以尝试将其更改为:

 while (lose <= 0 || win <= 0 || tie <= 0);

但要注意,这将导致无限循环,因此您可能需要重新考虑您的逻辑。您可能不需要循环,具体取决于您尝试做什么。