简体   繁体   English

Java开关盒未运行

[英]Java switch case not running

The switch case is not printing output, nor is it running. 开关盒没有打印输出,也没有运行。

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;

            }

        }
   }

}

The ultimate goal here is to create a sort of menu to prompt for user input, then navigate to whichever menu the user selects. 这里的最终目标是创建一种菜单来提示用户输入,然后导航到用户选择的菜单。 I want it all to continue to loop until the user inputs 'e', in which case it would break the loop. 我希望所有这些继续循环,直到用户输入“ e”,在这种情况下它将破坏循环。

The line when you are retrieving user input is mis-placed. 检索用户输入时的行放错了位置。 It should be located after you print the instructions: 打印说明后,它应该位于:

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;

    }

}

Note that I changed your while condition to read while (!exit) . 请注意,我将您的while条件更改为while (!exit)

You should also consider adding a default clause to the switch statement to handle the case when the user types another character. 您还应该考虑向switch语句添加一个default子句,以处理用户键入另一个字符时的情况。

I have updated your main method as below; 我已经更新了您的主要方法,如下所示; Your input collection happens outside the while loop, without any indicator to user to enter data hence you are not seeing anything eventhough program waits for user to enter. 您的输入集合发生在while循环之外,没有任何指示用户输入数据的信息,因此即使程序等待用户输入,您也看不到任何东西。 Also if the "i" then the while will enter never ending loop 另外,如果“ 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;

        }

     }
   }

You're retrieving user input before you actually enter the while loop. 在实际进入while循环之前,您正在检索用户输入。 You should call the user input inside the loop after you display the instructions. 显示说明后,应在循环内调用用户输入。

    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;

        }

     }
   }

}

On a side note, I highly recommend following Java naming conventions and renaming your classes to start with a capital letter. 另外,我强烈建议您遵循Java命名约定,并重命名您的类以大写字母开头。

In addition to the solution suggested, instead of using System.in.read() which reads exactly one byte at a time but your direct input can be more than one byte. 除了建议的解决方案之外,与其使用System.in.read()一次读取one字节,但您的直接输入可以超过one字节。 So instead of using read method, you can use the Scanner which you have initialized but not used. 因此,可以使用已初始化但尚未使用的Scanner ,而不是使用read方法。 For more information on System.in.read System.in.read() method 有关System.in.read更多信息System.in.read()方法

http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29 You can do something like 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;

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM