1

我正在用 Java 制作一个游戏,你必须在其中照顾一只狗。我让我的游戏方法自己调用,这样我就不必多次复制和粘贴该方法中的内容。问题是我不知道如何绕过我声明的两个整数值,因为通过每个选择添加和减去整数值,再次调用该方法会将这些更改的值更改回默认值。

    import java.io.File;
    import java.util.Scanner;

    public class Main {

        public static Scanner kybd = new Scanner(System.in);

        public static void main(String[] args) {
            game();
        }

        public static void game() {
            Integer diet;
            diet = 5;
            Integer happiness;
            happiness = 10;
            System.out.println("");
            System.out.println("Dog \t Hunger: " + diet 
                               + "\n \t Happiness: " +        happiness);
            System.out.println("");
            System.out.println("1. Feed \n2. Play \n3. Ignore");
            System.out.println("");
            System.out.print("> ");
            String input = kybd.nextLine();
            if (input.equalsIgnoreCase("1")) {
                diet++;
                game(); // This is supposed to go to the 
                        // beginning with the changed value of diet.
            } else if (input.equalsIgnoreCase("2")) {
                happiness++;
                game(); // This is supposed to go to the 
                        // beginning with the changed value of happiness.
            } else if (input.equalsIgnoreCase("3")) {
                happiness--;
                diet--;
                game(); // This is supposed to go to the
                        // beginning with the changed value of happiness.
            } else {
                System.out.println("Invalid Input");
                game(); // This is supposed to go the beginning
                        // where you can change your input but 
                        // still has your changed values.
            }
    if (diet <= 0);
    {
        System.out.println("Your dog died because it did not eat.");
        game(); // This is supposed to go to the beginning 
                // with the default values.
    }
    if (diet > 10);
    {
        System.out.println("Your dog died because it was overfed.");
        game(); // This is supposed to go to the 
                // beginning with the default values.
    }
    if (happiness <= 0);
    {
        diet--;
        System.out.println("Your dog is no longer happy. He will not eat.");
    }
    {
        if (happiness > 10);
        System.out.println("Your dog died because it was too excited.");
        game(); // This is supposed to go to the 
                // beginning with the default values.
    }
}
}
4

1 回答 1

1

如果我以正确的方式理解您的问题,您唯一需要做的就是将这两个变量声明为类变量(静态或非静态)。所以最终的代码是:

import java.io.File;
import java.util.Scanner;
public class Main {

    public static Scanner kybd = new Scanner(System.in);

    public static int diet = 5;
    public static int happiness = 10;

    public static void main(String[] args) {
        game();
    }

问候

编辑狗死

public static die() {
  diet=5;
  happiness=10;
}

每次您的狗在调用 game() 之前死亡时调用此函数。

于 2013-09-21T16:02:00.763 回答