我在用Java创建交互式菜单时遇到了麻烦

时间:2013-10-01 19:28:58

标签: java class switch-statement

我们的任务是https://docs.google.com/document/d/16hHb8WUGehbYLCNrSpJspP8x5GJNCR2q2chnG_yFPSo/pub

它使用的文件https://docs.google.com/document/d/1BxIKe4ZEEpWHmBk2O2FJit6frk5BZSm6SozHOAKWqWU/pub

我不是想利用这个网站让人们为我做我的项目,我会指出正确的方向,因为我真的迷失了。

我的switch语句有点麻烦,让菜单开始工作。

package student;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.util.*;


public class TestStudent {

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

        InputStreamReader isr = new InputStreamReader (System.in );
        BufferedReader stdin = new BufferedReader( isr );


        String check, tempString, tempString2;
        int tempInt, tempInt2;
        boolean quit = false;

        do
        {
            System.out.println("A - Add student, D - Delete student, F - Find student, H - Help, S - Scores, X - Exit");
            check = stdin.readLine();
            switch (check)
            {

                case "A":
                case "a":
                    System.out.println("Enter the student's name");
                    tempString = stdin.readLine();
                    System.out.println("Enter the student's ID number");

                //I'm stuck on where to go from here

                case "D":
                case "d":
                    System.out.println("Enter the ID number of the student you wish to delete");
                    tempString = stdin.readLine();
                    System.out.println("Student has been deleted");

                case "F":
                case "f":
                    System.out.println("Enter the Name or ID number of the student you would like to find");
                    tempString = stdin.readLine();
                    System.out.println("");

                case "H":
                case "h":
                    System.out.println("");

                case "S":
                case "s":
                    System.out.println("Enter the Name or ID number of the Student");

                case "X":
                case "x":
                    System.out.println("System shutting down");
            }

       }    //Error saying while is expected     
   }   //Error "Illegal start of an expression
}

2 个答案:

答案 0 :(得分:1)

你在做的时候错过了。

do {
  ...
}while(condition)

所以我相信你想要的东西:

do {
  ...
  //your whole switch statement
  ...
}while(!quit) //exit when quit is true, stay if quit is not true.

每个案例都应该有自己的中断,否则如果你去案例A,那么它将转到案例D,依此类推。

        case "A":
        case "a":
            System.out.println("Enter the student's name");
            tempString = stdin.readLine();
            System.out.println("Enter the student's ID number");
            break; //add this to every case if you don't want to execute next case.

为了简化您的切换...如果您使用check = stdin.readLine().toLowerCase();

你可以避免使用2例1例:

案例“a”并删除案例“A”(可删除大写字母的案例)

如果你没有重置quit

,那么你永远不会结束

也许你可以为它添加另一个案例:

case "q":
    quit=true;
    break;

如果没有任何案例,也会执行默认案例。

答案 1 :(得分:0)

Java中的

Do-while循环。此链接将有助于解决您的一些问题。