Java:如何在另一个文件中导入变量?

标签 java eclipse

在做任何事情之前阅读底部(但它可能很长)

我正在尝试制作一个基于文本的 Minecraft 服务器,但我在 Mines.java 中遇到了问题。我想从 Main.java 将整数“money”导入到 Mines.java 中的 [您将看到 7 斜杠注释]。我试过 import [package].Main.main.money,但没有用。

我正在使用 Mars.1 Eclipse。

主.java

// NOTE: Please read 'cc.txt' in src folder before modifying

package com.quaggles.main;

import java.util.Scanner;

public class Main {
    // System.out.println(""); (to copy)
    // System objects
    static boolean running = true;
    static Scanner in = new Scanner(System.in);
    static String input;
    static String username;
    // Game variables
    static int coins = 100;
    static int level = 1;
    // Methods that may come in handy
    public static void PETC() {
        System.out.println("Press enter to continue...");
        try {
            System.in.read();
        } catch (Exception e) {
            // Handle any exceptions
        }
    }
    // Doesn't work in java console (sorry Eclipse users) (still testing)
    public static void clearConsole() {
        try {
            final String os = System.getProperty("os.name");

            if (os.contains("Windows")) {
                Runtime.getRuntime().exec("cls");
            } else {
                Runtime.getRuntime().exec("clear");
            }
        } catch (final Exception e) {
            // Handle any exceptions
        }
    }

    public static void main(String[] args) {
        USER: while (running) {
            System.out.println("What is your username:");
            input = in.nextLine();
            if (input.equals("")) {
                System.out.println("You typed nothing! Please try again.");
                PETC();
                clearConsole();
                continue USER;
            } else {
                username = input;
                break USER;
            }
        }
        clearConsole();
        System.out.println("Running on " + System.getProperty("os.name") + "\n");
        System.out.println("Welcome to...");
        System.out.println("   _____                           _____            __ _");
        System.out.println("  / ____|                         / ____|          / _| |");
        System.out.println(" | (___   ___ _ ____   _____ _ __| |     _ __ __ _| |_| |_");
        System.out.println("  \\___ \\ / _ \\ '__\\ \\ / / _ \\ '__| |    | '__/ _` |  _| __|");
        System.out.println("  ____) |  __/ |   \\ V /  __/ |  | |____| | | (_| | | | |_");
        System.out.println(" |_____/ \\___|_|    \\_/ \\___|_|   \\_____|_|  \\__,_|_|  \\__|\n");
        System.out.println("    v0.0.1 beta");
        System.out.println("------------------------------------------");
        System.out.println("ServerCraft is a project made by Quaggle\n" + "that tries to answer the question:\n"
                + "What if a Minecraft server was text based?");
        System.out.println("------------------------------------------\n");
        PETC();
        clearConsole();
        System.out.println("------------------------------------------");
        GAME: while (running) {
            input = in.nextLine().toLowerCase();
            if (input.equals("exit")) {
                break GAME;
            } else if (input.equals("help")) {
                clearConsole();
                System.out.println("HELP");
                System.out.println("------------------------------------------");
                System.out.println("help - shows all* commands");
                System.out.println("stats - shows your stats");
                System.out.println("updates - show any updates on the game");
                System.out.println("mine - go to the mines");
                System.out.println("home");
                System.out.println("exit - quits the game :(\n");
                System.out.println("*not actually all - find them out yourself ;)");
                PETC();
                clearConsole();
                continue GAME;
            } else if (input.equals("stats")) {
                System.out.println("STATISTICS");
                System.out.println("------------------------------------------");
                System.out.println("Coins: " + coins);
                System.out.println("Level " + level);
                PETC();
                clearConsole();
                continue GAME;
            } else if (input.equals("updates")) {
                System.out.println("UPDATES");
                System.out.println("------------------------------------------");
                System.out.println("");
                System.out.println("1-8-16 - PRECIOUS");
                System.out.println("Added mining and a money system\n");
                System.out.println("1-7-16 - Mr. Krabs! I have an idea!");
                System.out.println("I finally started working on the game after countless reconsiderations & regrets!");
            } else if (input.equals("mine")) {
                com.quaggles.main.Mines.mine();
            } else {
                System.out.println("\'" + input + "\' is not a command.");
            }
        }
        System.out.println("Thanks for playing, " + username + "!");
        PETC();
        System.exit(0);
    }
}

地雷.java

// NOTE: Please read 'cc.txt' in src folder before modifying
// WORK IN PROGRESS: I am still working on this, (may be very buggy)

package com.quaggles.main;

import java.util.Scanner;

import com.quaggles.main.Main.*; //unneeded at the moment

public class Mines {
    // System objects
    static boolean running = true;
    static Scanner in = new Scanner(System.in);
    static String input;
    /////// Import 'money' from Main.java
    static int pickaxe = 1;
    static int pickaxeXP = 0;
    static int pickaxeLevelUp = 100;
    // Game variables
    public static void mine() {
        GAME: while (running) {
            System.out.println("Welcome to the...");
            System.out.println("  __  __ _                 ");
            System.out.println(" |  \\/  (_)                ");
            System.out.println(" | \\  / |_ _ __   ___  ___ ");
            System.out.println(" | |\\/| | | '_ \\ / _ \\/ __|");
            System.out.println(" | |  | | | | | |  __/\\__ \\");
            System.out.println(" |_|  |_|_|_| |_|\\___||___/");
            System.out.println("------------------------------------------");
            System.out.println("Type \'mine\' to start mining");
            input = in.nextLine();
            if (input.equals("mine")) {
                /////// money
            }
        }
    }
}

编辑:我刚刚意识到“钱”不存在,我忘记了我将它重命名为“硬币”,这应该可以消除任何混淆。

好的,我会尽量深入。

有人在我的“如何执行另一个文件”时说,也许我应该参加 Java 编程类(class),而不是通过 Stack Overflow 学习编程语言,我尊重这一点。

我只是想说清楚,我喜欢以自己独特的方式学习东西。我今年 11 岁,从我的角度来看,youtube 上的所有类(class)要么解释的太慢,到第 11 集我们仍将学习整数,要么他们进入的速度太快。还有那些在youtube 上的要花钱,所以不要给我推荐任何东西,没关系。此外,您不必回答。谢谢你,不管是谁。

最佳答案

我会尽可能先回答您的具体问题。没有名为 money 的变量在类里面com.quaggles.main.Main但是有一个 coins变量,所以如果这就是您的意思,方法如下:

Mines.java:

        if (input.equals("mine")) {
            System.out.println("My coins are: " + Main.coins);
        }

注意 1:同一 Java 包(即命名空间)中的类不需要 import , 他们也不需要完全指定命名空间,因此您可以简单地引用 MainMines (即没有命名空间 com.quaggles.main )。

注释 2:自 Main.coinsstatic这个 coins 只有一个实例在服务器中;据推测,这意味着服务器只能处理一个玩家。

注释 3:Main电话 Mines.mines()Mines然后调用Main.coins -- 这叫做 circular reference .作为专业人士,我们努力避免循环引用,因为它们会导致不可预测的行为。考虑重构您的代码,或许包括一个 Account 的列表s,即每个玩家一个,其中包含 coinsmoney .

祝你学习顺利,不要因为负面评论而气馁。

关于Java:如何在另一个文件中导入变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689758/

相关文章:

java - 使用调车场的计算器 - 收到 postfix 后如何继续

python - 如何使用 eclipse 和 pydev 调试 SCons 脚本?

mysql - BIRT - org.eclipse.birt.data.engine.odaconsumer.OdaDataException : Cannot get the result set metadata

java - 在 aspectj 中使用 cflow() 时如何删除 java..* 调用?

java - 为什么我的 Eclipse Luna 4.4 在使用 Oracle JDK8 64 位的 Debian Wheezy 7.4 上崩溃?

java - 如何在 Eclipse 上通过 testng 执行使用 Maven active-profiles

c++ - 从 Eclipse 获得可分发程序需要做什么?

java - Hibernate 不会通过合并或更新来更新实体

java - Pi 近似中的死字母 (java)

java - 数组索引不递增(需要在方括号中进行转换)