[求助] 那位大虾帮我看一下我的代码错在哪里?
//ILoveJava.java
import javax.swing.JOptionPane;
class ILoveJava
{
public static void main(String arg[])
{
HelloWorld hello = new HelloWorld();
String response;
// response = JOptionPane.showInputDialog
// ("who is your favorite guy?");
JOptionPane.showMessageDialog(null,hello.response+" is a good guy.\n");
System.exit(0);
}
}
//HelloWorld.java
import javax.swing.JOptionPane;
class HelloWorld
{
String response;
response = JOptionPane.showInputDialog("Input the name:\n"); //本行有错?
}
搜索更多相关的解决方案:
代码
----------------解决方案--------------------------------------------------------
//ILoveJava.java
import javax.swing.JOptionPane;
class ILoveJava
{
public static void main(String arg[])
{
HelloWorld hello = new HelloWorld();
String response;
// response = JOptionPane.showInputDialog
// ("who is your favorite guy?");
JOptionPane.showMessageDialog(null,hello.response+" is a good guy.\n");
System.exit(0);
}
}
//HelloWorld.java
import javax.swing.JOptionPane;
class HelloWorld
{
String response = JOptionPane.showInputDialog("Input the name:\n"); //本行改
}
//看看行不行。。。 ----------------解决方案--------------------------------------------------------
可以了.
为什么response不能先声明再赋值呢?
我在ILoveJava里面都可以的.
----------------解决方案--------------------------------------------------------
是不是刚刚学好C啊……………… //HelloWorld.java import javax.swing.JOptionPane; class HelloWorld { String response; public HelloWorld(){ response = JOptionPane.showInputDialog("Input the name:\n"); //本行改 } } 这样就可以了……
----------------解决方案--------------------------------------------------------
在声明的时候进行附值是允许的,但声明后再重新附值则需要写在方法内,在C中是允许的,但java是不可以的,在ILoveJava里你是直接写在main这个方法内的,所以是可以的
----------------解决方案--------------------------------------------------------
那就是说声明后没有初始化再赋值就得写在函数内.
呵呵,有点不太适应
----------------解决方案--------------------------------------------------------
飘飘叶子 的话很受启发!!!
----------------解决方案--------------------------------------------------------