You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

公共静态int方法中赋值的玩家分数未生效问题求助

问题原因

Java里的基本数据类型(比如int)是值传递,你把main方法里的playerScore传给其他方法时,传递的是变量的副本,方法里修改的只是这个副本,根本不会影响main里的原变量。所以不管你在checkCorrectAnswer里怎么给副本赋值,main里的playerScore始终是初始的0。

修复方案

这里给你两种最直接的修复方式:

方式一:让方法返回更新后的分数,层层传递回去

修改每个处理分数的方法,让它们返回更新后的playerScore,最终在main方法里接收这个返回值,更新原变量。

修改后的完整代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        /*Questions*/
        String[] questions = new String[5];
        questions[0] = "What year did the hit cartoon show Tom & Jerry debut in for the first time?";
        questions[1] = "In the famous japanese franchise Dragon Ball Z, most main characters can undergo what powerful transformation?";
        questions[2] = "The famous video game character, Sonic, is of what animal species?";
        questions[3] = "Java, the programming language, was created by whom?";
        questions[4] = "The famous social platform, Roblox, launched for the first time in what year?";
        /*Answers*/
        String[] answers = new String[5];
        answers[0] = "1941";
        answers[1] = "super saiyan";
        answers[2] = "hedgehog";
        answers[3] = "james gosling";
        answers[4] = "2006";

        Scanner playerInput = new Scanner(System.in);

        int playerScore = 0;

        /*Trivia Start*/
        System.out.println("Welcome to Jpeg Louie's Trivia v3 (now with methods!)");
        System.out.println("You'll be expected to type the correct answer. Mind your whitespaces and punctuations");
        System.out.println(" ");

        while(true){
            // 接收返回的更新后的分数
            playerScore = doTriviaQuiz(questions, answers, playerInput, playerScore);

            /*Trivia End*/
            System.out.println("----------------------------------------------------------");
            System.out.println("The trivia is over! Thanks for playing :)");
            System.out.println("CALCULATING SCORE");
            System.out.println("...");
            System.out.println("...");
            System.out.println("...");
            System.out.println("Your score was: " + playerScore + " correct questions out of " + questions.length);

            /*Special Score Message*/
            if(playerScore == 0){
                System.out.println("Looks like you got all questions wrong...");
                System.out.println("Try to get them all correct, I might reward you with something ;)");
            } else if(playerScore == questions.length){
                System.out.println("Congratulations! You beat the trivia quiz!");
                System.out.println("Here's a list of commands that trigger special events");
                System.out.println("(When prompted to play again, type the desired command to execute it)");
            }

            /*Play Again?*/
            System.out.println(" ");
            System.out.println("Play again? Type [Y] for Yes and anything else for No");
            String playAgain = playerInput.nextLine();

            if(playAgain.equalsIgnoreCase("y")){
                // 重新开始时重置分数
                playerScore = 0;
                System.out.println(" ");
            } else {
                break;
            }
        }
    }
    // 修改为返回int类型
    public static int doTriviaQuiz(String[] questions, String[] answers, Scanner playerInput, int playerScore) {
        return doQuestionsXTimes(questions, answers, playerInput, playerScore);
    }

    // 修改为返回int类型
    public static int doQuestionsXTimes(String[] questions, String[] answers, Scanner playerInput, int playerScore) {
        for(int i = 0; i < questions.length; i++) {
            // 接收每次答题后的分数更新
            playerScore = doQuestion(questions, answers, playerInput, playerScore, i);
        }
        return playerScore;
    }

    // 修改为返回int类型
    public static int doQuestion(String[] questions, String[] answers, Scanner playerInput, int playerScore, int i) {
        System.out.println("Question " + (i + 1) + ": " + questions[i]);
        String answer = playerInput.nextLine();

        // 接收判断后的分数更新
        return checkCorrectAnswer(answers, i, answer, playerScore);
    }

    // 修改为返回int类型
    public static int checkCorrectAnswer(String[] answers, int i, String answer, int playerScore) {
        if (answer.equalsIgnoreCase(answers[i])) {
            System.out.println("Correct!");
            playerScore = incrementPlayerScore(playerScore);
        } else {
            System.out.println("Incorrect! The answer was: " + answers[i]);
        }
        return playerScore;
    }

    public static int incrementPlayerScore(int playerScore){
        playerScore++;
        return playerScore;
    }
}

方式二:把playerScore设为类的静态成员变量

直接在类里定义静态的playerScore,这样所有静态方法都能直接修改它,不用传递参数。

修改后的核心代码片段:

public class Main {
    // 定义静态成员变量
    private static int playerScore = 0;

    public static void main(String[] args) {
        // ... 其他代码不变,去掉main里的int playerScore = 0;
        while(true){
            doTriviaQuiz(questions, answers, playerInput);
            // ... 其他代码不变

            if(playAgain.equalsIgnoreCase("y")){
                // 重置分数
                playerScore = 0;
                System.out.println(" ");
            } else {
                break;
            }
        }
    }

    // 所有方法去掉playerScore参数,直接操作静态变量
    public static void doTriviaQuiz(String[] questions, String[] answers, Scanner playerInput) {
        doQuestionsXTimes(questions, answers, playerInput);
    }

    public static void doQuestionsXTimes(String[] questions, String[] answers, Scanner playerInput) {
        for(int i = 0; i < questions.length; i++) {
            doQuestion(questions, answers, playerInput, i);
        }
    }

    public static void doQuestion(String[] questions, String[] answers, Scanner playerInput, int i) {
        System.out.println("Question " + (i + 1) + ": " + questions[i]);
        String answer = playerInput.nextLine();

        checkCorrectAnswer(answers, i, answer);
    }

    public static void checkCorrectAnswer(String[] answers, int i, String answer) {
        if (answer.equalsIgnoreCase(answers[i])) {
            System.out.println("Correct!");
            incrementPlayerScore();
        } else {
            System.out.println("Incorrect! The answer was: " + answers[i]);
        }
    }

    public static void incrementPlayerScore(){
        playerScore++;
    }
}
关键知识点

Java的参数传递规则:

  • 基本数据类型(int、double等)传递的是变量的副本,方法内修改副本不影响原变量。
  • 引用数据类型(对象、数组)传递的是对象引用的副本,但这个副本指向同一个对象,所以修改对象的内部属性会影响原对象。

内容的提问来源于stack exchange,提问作者jpeg_louie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 23:12:53