2016-01-09 48 views
3

閱讀做任何事情之前底部(但它可能會很長)Java:如何在另一個文件中導入變量?

我試圖做一個基於文本的Minecraft服務器,我有在Mines.java麻煩。我想從Main.java中將Mins.java中的整數'money'導入到[您將看到7斜槓註釋]中。我試過import [package].Main.main.money,但沒有奏效。

我正在使用Mars.1 Eclipse。

Main.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); 
    } 
} 

Mines.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編程課程,而不是通過Stack Overflow學習編程語言,我尊重這一點。

我只是想說清楚一點,我喜歡以自己特殊的方式學習東西。我是11歲,從我的角度來看,YouTube上的所有課程要麼解釋得太慢,以至於在第11集中,我們仍然會學習整數,或者他們過快地學習東西。而且那些不是在youtube上花錢,所以不要推薦我任何東西,這很好。此外,你不要回答。謝謝你,無論那是誰。

+1

對於** 11 ** - 歲,確實令人印象深刻。 –

+0

請擴大「*沒有工作*」。究竟發生了什麼? –

+3

雖然我明白你想'以你自己的方式學習',但它仍然有助於瀏覽書籍或採取其他免費課程,如https://www.udacity.com/course/intro-to-java-programming- -cs046或https://docs.oracle.com/javase/tutorial/。關鍵是編程使用「語言」,你需要能夠溝通。在這一點上,我不知道你的班級(思想家)的錢方法(認爲是門)應該連接到哪裏。我現在要離開我的高馬。在11試穿道具。 –

回答

2

我會盡可能先回答您的具體問題。有沒有在類com.quaggles.main.Main命名money變量,但有一個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服務器的單個實例;據推測,這意味着服務器只能處理一個玩家。

注3Main調用Mines.mines()Mines然後調用Main.coins - 這就是所謂的circular reference。作爲專業人士,我們努力避免循環引用,因爲它們可能導致不可預知的行爲。考慮重構你的代碼,可能包括一列Account,即,每個玩家包含coinsmoney

祝你好運與你的學習,不要因負面評論而氣餒。

相關問題