在for循环中切换语句

时间:2018-06-09 04:56:27

标签: java

我有一个问题,一旦我用扫描仪选择四个选项中的一个,它会默认为那个开关盒,所以说我最初按1,输入我的字符串,然后按2,它&# 39; ll仍然执行,好像我点击了案例1.我在这里做错了什么?

  public static void menu()
{
    FileClass f = new FileClass();

    Scanner scan = new Scanner(System.in);
    System.out.println("Choose an option");
    System.out.println("1: Write To File\n2: Read From File\n3: Delete From File\n4: Exit Program");
    int choice = scan.nextInt();

    while(choice < 5) {
        switch(choice) {

            case 1:
            System.out.println("Enter a line you wish to write to the file: ");
            Scanner inputW = new Scanner(System.in);
            String lineWrite = inputW.nextLine();
            f.writeToFile(lineWrite);
            break;

            case 2:
            System.out.println("This is Everything on the File:");
            f.readFromFile();
            break;

            case 3:
            System.out.println("Enter a line you wish to delete from the file: ");
            Scanner inputD = new Scanner(System.in);
            String lineDelete = inputD.nextLine();
            f.deleteFromFile(lineDelete);
            break;

            case 4:
            System.exit(0);
            break;

        }
        System.out.println("Choose an option");
        System.out.println("1: Write To File\n2: Read From File\n3: Delete From File\nExit Program");
        scan.nextInt();
    }
    System.exit(0);
}

2 个答案:

答案 0 :(得分:1)

我认为你scan.nextInt()的最后一次使用是冗余的。我认为你的程序看起来像这样:

public static void main(String... args) {
    try (Scanner scan = new Scanner(System.in)) {
        int choice = 0;

        while (choice < 4) {
            System.out.println("Choose an option");
            System.out.println("1: Write To File");
            System.out.println("2: Read From File");
            System.out.println("3: Delete From File");
            System.out.println("4: Exit Program");

            choice = scan.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("Enter a line you wish to write to the file: ");
                    break;
                case 2:
                    System.out.println("This is Everything on the File:");
                    break;
                case 3:
                    System.out.println("Enter a line you wish to delete from the file: ");
                    break;
            }
        }
    }
}

答案 1 :(得分:0)

你有一个choice变量,你在循环之外初始化一次,永远不会再分配给它。您设置的初始值只是在无限循环中重复使用。如果您希望在迭代之间看到任何变化,您应该在循环中分配它。

另一点是你要重复两次菜单代码。通过将其放入循环中可以很容易地避免这种情况。

特定蒸汽的Scanner只需要创建一次并重复使用。 System.in上不需要多个扫描仪。

从技术上讲,您将输入&gt; = 4视为退出命令。不需要单独的switch条目,至少在您当前发布的代码中。

每次用户点击进入时,您都会在输入中获得换行符。换行将阻止您正确解析事物,因此您需要摆脱它。一种好方法是在调用Scanner.nextLine

之间致电nextInt
public static void menu()
{
    FileClass f = new FileClass();

    Scanner scan = new Scanner(System.in);
    int choice = 0;
    while(choice < 4) {
        System.out.println("Choose an option");
        System.out.println("1: Write To File\n2: Read From File\n3: Delete From File\n4: Exit Program");
        choice = scan.nextInt();
        scan.nextLine(); // Get rid of the trailing newline character

        switch(choice) {
            case 1:
                System.out.println("Enter a line you wish to write to the file: ");
                String lineWrite = scan.nextLine();
                f.writeToFile(lineWrite);
                break;

            case 2:
                System.out.println("This is Everything on the File:");
                f.readFromFile();
                break;

            case 3:
                System.out.println("Enter a line you wish to delete from the file: ");
                String lineDelete = scan.nextLine();
                f.deleteFromFile(lineDelete);
                break;
        }
    }
    System.exit(0);
}