为什么我得到 void type not allowed

why i'm getting void type not allowed

当我尝试打印方法时,我在第二行收到一条错误消息 此处不允许使用 void 类型

 public static void main(String[] args) {
System.out.println(printMenu());
  }

 public static void printMenu(){

 System.out.println("***** Welcome to KAU Flight Reservation System *****"+
        "\n 1. Add Flight Details in the System"+
        "\n 2. Add Passenger Details in the System"+
        "\n 3. Make a new Booking"+
        "\n 4. Search and Print a Booking"+
        "\n 5. List Flight Status"+
        "\n 6. Exit from the System ");

}

java中有两种函数。他们没有 return 任何值的过程和 return 值的函数。当您在方法声明中添加单词 void 时,这意味着这是一个过程方法,它没有 return 任何值,但是 System.out.print() 方法需要一个 Object 类型的参数。

你的 printMenu() 方法应该是这样的。

 public static String printMenu(){

 String menu = "***** Welcome to KAU Flight Reservation System *****"+
        "\n 1. Add Flight Details in the System"+
        "\n 2. Add Passenger Details in the System"+
        "\n 3. Make a new Booking"+
        "\n 4. Search and Print a Booking"+
        "\n 5. List Flight Status"+
        "\n 6. Exit from the System ";
return menu;

}
public class PrintMenu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printMenu();
    }

    public static void printMenu(){

         System.out.println("***** Welcome to KAU Flight Reservation System *****"+
                "\n 1. Add Flight Details in the System"+
                "\n 2. Add Passenger Details in the System"+
                "\n 3. Make a new Booking"+
                "\n 4. Search and Print a Booking"+
                "\n 5. List Flight Status"+
                "\n 6. Exit from the System ");

        }

}

您已经使用 printMenu 函数打印了菜单,因此不应在 system.out.print 函数中调用它。

您正在尝试调用 System.out.println() 中的 printMenu() 方法,该方法需要一个字符串值。但是,您的 printMenu() 方法 return 无效。

public static void main(String[] args) {
    System.out.println(printMenu());
}

public static String printMenu() {
    return ("***** Welcome to KAU Flight Reservation System *****"+
            "\n 1. Add Flight Details in the System"+
            "\n 2. Add Passenger Details in the System"+
            "\n 3. Make a new Booking"+
            "\n 4. Search and Print a Booking"+
            "\n 5. List Flight Status"+
            "\n 6. Exit from the System ");
}

有关 return 类型的更多信息,请查看此 Oracle Java 教程:http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html 我还建议您阅读其中一些教程,它们非常擅长教授一些基础知识。