2013-10-04 40 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类,因此此代码存在根本性缺陷。