Java 字符串方法返回 Null?

标签 java methods

我的任务是制作一个简单的程序,与用户一起玩石头剪刀布。不幸的是,当我尝试运行该程序时,我的 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是按值传递,而不是按引用传递。这意味着 defineOutcome 方法将使用其自己的 outcomesentence 副本,并且不会修改属于调用方法的版本它。

而且,该方法实际上并未使用您传递给它的两个参数。如果您完全省略参数,并将其更改为 public static String certainOutcome() ... 并声明 String Sent; ,那么混淆会少得多。字符串结果; 在方法内。

关于Java 字符串方法返回 Null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219267/

相关文章:

java - 无法将值插入数据库

java - 为什么我在无限循环代码中有不同的结果

java - “Main method not found in class…”但它在那里?那为什么会出错呢?

java - 密码和用户名存储在 Apache Ignite 中的哪里?

java - 直接向 Quarkus/RESTEasy Web 服务方法提供 SecurityIdentity

java - PDFReader 实用程序 radaee 导致应用程序崩溃

java - 无法调用方法,试图将变量从一个方法获取到另一个方法

java - 使用方法和多个类的一副纸牌

python - 在 Python 中,有没有办法在可迭代对象的每个项目上调用一个方法?

java - 在Java中调用具有相同名称的方法内的另一个方法