有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有变量/参数的简单程序中的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) 个答案

  1. # 1 楼答案

    看起来您没有捕获getWordFromUser方法调用的返回结果

    可以通过以下两种方式之一实现:

    String word = getWordFromUser("Enter a random word!");
    printWord(word);
    

    。。。或者

    printWord(getWordFromUser("Enter a random word!"));
    

    注意第一种方法。我声明一个变量word来存储值。您声明了一个要引用的变量Word,但在任何地方都没有声明它