带有变量/参数的简单程序中的java问题
为了好玩,我正在写一个简单的程序。我是编程新手,不知道如何解决这个问题。问题在于printWord方法。不确定要在括号中加什么才能使它起作用。有话就没用了。这是我的代码:`public class getWord{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
getWordFromUser("Enter a random word!");
printWord(Word);
}
/**
* gets a Word From the User
*/
public static String getWordFromUser(String prompt){
Scanner sc = new Scanner(System.in);
System.out.println(prompt);
return sc.nextLine();
}
/**
* Prints the random word that the user has entered
* @param Word
*/
public static void printWord(String Word){
System.out.println("The word you have entered is " + Word);
}
}
谢谢你的帮助
# 1 楼答案
看起来您没有捕获
getWordFromUser
方法调用的返回结果可以通过以下两种方式之一实现:
。。。或者
注意第一种方法。我声明一个变量
word
来存储值。您声明了一个要引用的变量Word
,但在任何地方都没有声明它