【问题标题】:Random() method keeps changing final String in JavaRandom() 方法不断改变 Java 中的最终字符串
【发布时间】:2015-03-30 13:58:49
【问题描述】:

我正在尝试创建一个石头剪刀布游戏并教自己更多关于 Java 子程序的知识。为了检查其背后的逻辑是否正确,我在程序中分两部分编写了检查对方玩家当前手牌位置的部分。但是,每次都不一样,即使我在单独的方法中将它分配给最终的字符串。

import java.util.Scanner;

public class RockPaperScissors {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String choice = "start";

        System.out.println("Welcome to a friendly game of Rock, Paper, Scissors!");
        System.out.println("Type in 'start' , 'continue' or 'yes' to play the game, and 'quit' or 'no' to quit the game");

        choice = input.nextLine();

        try {
            while (choice.equalsIgnoreCase("start") || choice.equalsIgnoreCase("continue") || choice.equalsIgnoreCase("yes")) {

                System.out.println("Rock, Paper or Scissors?");

                String answer = input.nextLine();

                System.out.println("Enemy player chose " + set()); // checks current position
                comparison(answer);
                System.out.println("Would you like to continue?");
                choice = input.next();

            }
            System.out.println("Thank you for playing!");
            System.out.println("Have a wonderful day!");
            System.exit(1);
        } catch (Exception e) {
            System.out.println("Invalid player input.");
        }
    }

    static String random() {
        int random = (int) (Math.random() * 3);
        String current = null;
        switch (random) {
        case 0:
            current = "Rock";
            break;
        case 1:
            current = "Paper";
            break;
        case 2:
            current = "Scissors";
            break;
        }
        return current;
    }

    static String set() {
        final String a = random();
        return a;
    }

    static void comparison(String filler) {
        String answer = null;
        System.out.println(set()); // checks current position

        if (filler.equalsIgnoreCase(random())) {
            System.out.println("Draw!");
            System.out.println();
            System.out.printf("You both chose %s ", random());
            System.out.println();
        } else if (filler.equalsIgnoreCase("Rock")) {
            if (set().equals("Scissors"))
                System.out.println("Rock beats Scissors, you win!");
            else
                System.out.println("Paper beats Rock, you lose...");
        } else if (filler.equalsIgnoreCase("Paper")) {
            if (set().equalsIgnoreCase("Rock"))
                System.out.println("Paper beats Rock, you win!");
            else
                System.out.println("Scissors beat Paper, you lose...");
        } else if (filler.equalsIgnoreCase("Scissors")) {

            if (set().equalsIgnoreCase("Paper"))
                System.out.println("Scissors beat paper, you lose...");
            else
                System.out.println("Rock beats scissors, you lose...");
        } else
            System.out.println("Unknown player input.");

    }

}

【问题讨论】:

  • 你可以只使用你的 random() 方法,它的效果与 set() 相同,返回随机字符串值:Rock、Paper 或 Scrissors。

标签: java function methods subroutine


【解决方案1】:

正如其他人所说,每次调用方法时它都会再次运行,因此该方法中的变量和返回值会发生变化。
因此,您必须随机运行该方法一次,然后使用然后将该结果传递到它所在的位置需要:

//...
String enemy = random();
System.out.println("Enemy player chose " + enemy); //checks current position
comparison(answer, enemy);
//...
static void comparison(String answer, String enemy) {
     //do not call random() or set() in here
     //just use the answer and enemy parameters
}

【讨论】:

  • 我正在准备相同的答案,你的可能是最接近解决问题的答案。
  • 谢谢,最后只是添加了额外的参数并删除了 set() 方法。
【解决方案2】:

final 局部变量仅在声明它的方法内不可更改 - 并且对于每次调用,它都可能不同。

但是,您可以为此使用字段 - 第一次,您将使用随机选择初始化该字段,在进一步调用时,您将使用该字段的值。

如果你不做所有事情static,这会更好。但是要使您当前的代码正常工作,您可以这样做:

private static String choice;

static String set()
{
    if (choice == null) {
        choice = random();
    }
    return choice;
}

【讨论】:

    【解决方案3】:

    问题是

    static String set()
    {
        final String a = random();
    
        return a;
    }
    

    每次调用这个函数时,都会创建一个新的最终字符串 a。变量 a 仅在本地创建并在方法完成时分派,尽管系统不记得它已经是 final

    如果您希望您的 AI 只选择一次,请在您的 main 方法的开头创建一个最终字符串。

    public static void main(String args[]){
    .
    .
    final String pick=set();
    try
    

    【讨论】:

      【解决方案4】:

      你的方法 set() 是错误的。它使用内部变量'a',每次调用方法时都会擦除(在堆栈上创建新的)。您需要将变量 a 更改为类变量。 无论如何 final 在这里不起作用,您需要检查变量 a 是否为空,然后才分配值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多