我正在尝试从方法Dialog返回用户输入(inputname),以便可以在稍后打印方法中打印,但是我收到错误消息“exercise4.java:85:error:expected < / p>
public static String Dialog(inputname) //asks user about station and prints result"
public static void main (String args [] )
{
Dialog(); // asks user about station and returns answer
Print(); // prints station information
System.exit(0);
}
// END Main method
public static String Dialog(inputname) //asks user about station
{
String inputname = JOptionPane.showInputDialog("What station would you like information about?");
return inputname;
}
public static void Print() //prints information
{
System.out.println("The name of the station is "+ inputname);
return;
}
}
答案 0 :(得分:0)
试试这个:
public static void main (String args [] ) {
String inputname = Dialog(); // asks user about station and returns answer
Print(inputname); // prints station information
System.exit(0);
}
// END Main method
public static String Dialog() {//asks user about station
String inputname = JOptionPane.showInputDialog("What station would you like information about?");
return inputname;
}
public static void Print(String inputname) {//prints information
System.out.println("The name of the station is "+ inputname);
return;
}
你必须做两件事:
您错过的是您丢失了用户的数据,因为您没有将其存储在变量中。
答案 1 :(得分:0)
下面:
public static String Dialog(inputname) //asks user about station
{
String inputname = JOptionPane.showInputDialog("What station would
...
和
public static void Print() //prints information
{
System.out.println("The name of the station is "+ inputname);
变量inputname
是第一种方法中的本地变量。在其他任何地方都可以看到不 - 尤其是在该方法之外。简单的解决方案:
public static void printName(String name) ...
然后
printName(getNameFromDialog())
我自由地将你的方法重命名作为A)遵循Java惯例,B)告诉读者他们实际做了什么。
或者,您可以在类中声明 static 字段,并使用该字段使用这两个方法(而不是使用局部变量resp。参数)。