簡體   English   中英

Java String方法返回Null?

[英]Java String Method returning Null?

我的任務是制作一個簡單的程序,與用戶一起玩剪刀石頭布。 不幸的是,當我嘗試運行程序時,我的return(sentence + outcome)都返回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);
    } 
}

要使其正常工作,您需要更換

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 sentence; String outcome; 里面的方法。

調用返回結果的函數時,必須在System.out.println()中調用該函數,或者將結果分配給一個變量,然后進行打印。

選項1:

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

或選項2:

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

除了其他人在回答中提出的要點外,您的while循環似乎從未執行過。 變量tiewinlose永遠不會小於0,因此while循環的條件永遠不會為真。 您可以嘗試將其更改為:

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

但是請注意,這將導致無限循環,因此您可能需要重新考慮自己的邏輯。 根據您要執行的操作,這里可能不需要循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM