java - 避免重置整型变量

标签 java

我正在用 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.
    }
}
}

最佳答案

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

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() 之前死亡时,请调用此函数。

关于java - 避免重置整型变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18934618/

相关文章:

java - Eclipse Neon 忽略 eclipse.ini -vm

java - 如何为eclipse插件项目创建一个jar文件,该文件应该能够在没有eclipse软件的情况下下载和运行?

Java - 将多个字符串转换为图像格式

java - SWT 文本输出和换行

java - 无法为实现 Spring Security UserDetails 的类加载请求的类异常

java - 进行 Thymeleaf/OGNL 方法访问时出现 IllegalAccessException

java - 使用“持续时间”、“期间”或“年月”作为类字段类型是否合适?

java - 在调整窗口大小之前,ScrollPane 不会显示

java - 使子类在java中可观察

java - 倒计时和打印循环时遇到问题