Java switch case 不是 运行

Java switch case not running

switch case 不是打印输出,也不是运行。

package aircrack.ng;

import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        infoMenu man = new infoMenu();
        airMonMenu airmon = new airMonMenu();
        boolean exit = false;
        char optSelect = (char) System.in.read();

        while (exit == false) {

            System.out.println("\nPlease select which menu you'd like to      open...");
            System.out.println("To view information about the tools included type: 'i' ");
            System.out.println("To enter the airmon menu type: 'a'");
            System.out.println("To exit simply type: 'e'\n");

            switch (optSelect) {
            case 'i':
                man.infoMenu();
                break;
            case 'a':
                airmon.airMonMenu();
                break;
            case 'e':
                exit = true;

            }

        }
   }

}

这里的最终目标是创建一种菜单来提示用户输入,然后导航到用户选择的任何菜单。我希望它一直循环直到用户输入 'e',在这种情况下它会中断循环。

您检索用户输入时的行放错了。它应该位于之后 你打印指令:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    infoMenu man = new infoMenu();
    airMonMenu airmon = new airMonMenu();
    boolean exit = false;
    while (!exit) {
        System.out.println("\nPlease select which menu you'd like to      open...");
        System.out.println("To view information about the tools included type: 'i' ");
        System.out.println("To enter the airmon menu type: 'a'");
        System.out.println("To exit simply type: 'e'\n");
        char optSelect = (char) System.in.read(); //should be here
        switch (optSelect) {
        case 'i':
            man.infoMenu();
            break;
        case 'a':
            airmon.airMonMenu();
            break;
        case 'e':
            exit = true;

    }

}

请注意,我将您的 while 条件更改为 while (!exit)

您还应该考虑在 switch 语句中添加一个 default 子句来处理用户键入另一个字符的情况。

我已将您的主要方法更新如下;您的输入收集发生在 while 循环之外,没有任何指示用户输入数据,因此即使程序等待用户输入,您也看不到任何内容。此外,如果 "i" 那么 while 将进入永无止境的循环

public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        boolean exit = false;

        while (exit == false) {
            System.out.println("Enter char---");
            char optSelect = (char) System.in.read();
          System.out.println("\nPlease select which menu you'd like to      open...");
          System.out.println("To view information about the tools included type: 'i' ");
          System.out.println("To enter the airmon menu type: 'a'");
          System.out.println("To exit simply type: 'e'\n");

          switch (optSelect) {
            case 'i':
                 man.infoMenu();
                break;
            case 'a':
                airmon.airMonMenu();
                break;
            case 'e':
                exit = true;

        }

     }
   }

您在实际进入 while 循环之前检索用户输入。显示指令后,您应该在循环内调用用户输入。

    package aircrack.ng;

    import java.util.Scanner;

    public class main {

      public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        infoMenu man = new infoMenu();
        airMonMenu airmon = new airMonMenu();
        boolean exit = false;


        while (!exit) {

          System.out.println("\nPlease select which menu you'd like to      open...");
          System.out.println("To view information about the tools included type: 'i' ");
          System.out.println("To enter the airmon menu type: 'a'");
          System.out.println("To exit simply type: 'e'\n");
          char optSelect = (char) System.in.read();

          switch (optSelect) {
            case 'i':
                man.infoMenu();
                break;
            case 'a':
                airmon.airMonMenu();
                break;
            case 'e':
                exit = true;

        }

     }
   }

}

附带说明一下,我强烈建议遵循 Java 命名约定并将您的 类 重命名为以大写字母开头。

除了建议的解决方案之外,不要使用 System.in.read(),它一次准确读取 one 个字节,但您的直接输入可以超过 one 个字节。 因此,您可以使用已初始化但未使用的 Scanner 而不是使用 read 方法。 有关 System.in.read 的更多信息 System.in.read() method

http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29 你可以这样做

Scanner in = new Scanner(System.in);
boolean exit = false;

while (!exit) {

  System.out.println("\nPlease select which menu you'd like to      open...");
  System.out.println("To view information about the tools included type: 'i' ");
  System.out.println("To enter the airmon menu type: 'a'");
  System.out.println("To exit simply type: 'e'\n");

  char optSelect = in.next().charAt(0);
  switch (optSelect) {
    case 'i':
        System.out.println("i");
        break;
    case 'a':
        System.out.println("a");
        break;
    case 'e':
        System.out.println("e");
        exit = true;

  }
}