Java交换机案例未运行

时间:2015-10-02 14:50:06

标签: java switch-statement

开关盒不是打印输出,也不是在运行。

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',在这种情况下,它会打破循环。

4 个答案:

答案 0 :(得分:4)

检索用户输入时的行被错误放置。在打印说明后,它应位于

@Model.MonthName

请注意,我已将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)语句中添加default子句来处理用户键入其他字符时的情况。

答案 1 :(得分:1)

我已更新您的主要方法,如下所示;您的输入集合发生在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;

        }

     }
   }

答案 2 :(得分:0)

您在实际进入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命名约定并将类重命名为以大写字母开头。

答案 3 :(得分:0)

除了建议的解决方案之外,不是使用System.in.read()一次只读取one个字节,而是直接输入可以超过one个字节。 因此,您可以使用已初始化但未使用的read,而不是使用Scanner方法。 有关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;

  }
}