2013-10-04 81 views
-1

我前一段時間寫的這個短代碼:方法和變量調用問題

public class Check { 
public static int gold, silver; 
public int level=1; 

public static void main(String[] args) { 
    System.out.println("You are now level " + level + "!"); 
    String num1 = JOptionPane.showInputDialog("Enter a positive number:"); 
    int num2 = Integer.parseInt(num1); 
    if (num2 < 0) { 
     next(); 
    } else { 
     main(); 
    } 
} 
public void next() { 
    System.out.println("Thank you!"); 
} 

}

我有3個問題與此代碼:

  1. 如果我做一個公共靜態整數變量,我不能在聲明它時設置一個數字。我必須在聲明時設置一個數字。編輯:我的不好,可以在聲明時給它分配一個數字。

    如果我創建一個公共的Integer變量,我可以聲明它併爲它設置一個數字,但由於某種原因,我不能在public static void Main中使用它,我也必須這樣做。由於next()不是靜態的void,我不能從main(String [] args)void中調用它。 我不想讓next()靜態,因爲那樣我就無法使用非靜態的公共整數。

  2. 我不能從main()本身返回(調用)main()。有必要檢測到無效輸入。

我能對這些問題做些什麼?

+1

您應該閱讀以獲得對java變量的基本理解http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – digitaljoel

回答

2

如果你不想使用靜態方法,你必須在你的main方法中創建一個類對象,並用它來調用next()方法。

Check obj = new Check(); 
obj.next(); 
+0

謝謝,效果很好。 另外,是否有任何理由來創建靜態空隙,或者我更好地使新空隙非靜態? – BlueRay101

+0

'static'和'void'是兩個完全不同的東西。 「靜態」方法通常最好避免 - 它們傾向於顯示糟糕的OO設計。 –

2

If I make a public static Integer variable, I cannot set a number to it while declaring it.

當然可以。

If I make a public Integer variable, I can declare it and set a number to it, but for some reason I cannot use it in the public static void Main

這是因爲靜態方法不能使用非靜態屬性。

I cannot return (call) main() from main() itself. It's necessary for when invalid input is detected.

是的,你可以,但你需要傳遞參數。

+0

謝謝,但寫作main();給我以下錯誤: 方法main在類中Check不能應用於給定的類型; – BlueRay101

+0

@BarSharabani是的,正如Nicolai指出的那樣,你需要對主要方法進行論證。 –

1
  1. 你確實在某處失誤。
  2. 您無法訪問來自靜態方法的非靜態靜態成員(main是靜態的)。
  3. 你忘了參數

試試這個變種:

public class Check { 
    public static int gold, silver; 
    public static int level = 1; 

    public static void main(String[] args) { 
     System.out.println("You are now level " + level + "!"); 
     String num1 = JOptionPane.showInputDialog("Enter a positive number:"); 
     int num2 = Integer.parseInt(num1); 
     if(num2 < 0) { 
      next(); 
     } 
     else { 
      main(args); 
     } 
    } 

    public static void next() { 
     System.out.println("Thank you!"); 
    } 
} 
+0

謝謝,它完美的作品。 但我有一個問題:如果我只做public int(非靜態),我不能在靜態方法中使用它。這是爲什麼? – BlueRay101

+0

你必須閱讀java教程來完全理解這一點。在兩個詞中,靜態成員是類成員,非靜態成員是對象成員。閱讀本文可能有助於瞭解http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html。教程的主頁是:http://docs.oracle.com/javase/tutorial/java/TOC.html。 – Nicolai

0

幾點意見。

1)如果我做一個公共靜態整型變量,我不能設置一個數字來它,而它聲明

爲什麼?

你應該很容易能夠這樣聲明它:

public static int level = 1; 

然後,你的代碼將正常工作。

2)避免靜態 - 不從main打電話給你的程序邏輯,使用main來引導你的應用程序:

public int gold, silver; 
public int level = 1; 

public static void main(String[] args) { 
    new Check().read(); 
} 

public void read() { 
    System.out.println("You are now level " + level + "!"); 
    String num1 = JOptionPane.showInputDialog("Enter a positive number:"); 
    int num2 = Integer.parseInt(num1); 
    if (num2 < 0) { 
     next(); 
    } else { 
     read(); 
    } 
} 

public void next() { 
    System.out.println("Thank you!"); 
} 

所以,你做的一切實例作用域,然後創建的Check實例在main

我還會注意到,您正在使用擺脫了Swing的單線程模型的EDT的Swing GUI類,因此此代碼存在根本性缺陷。