2

我们的任务是这个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
}
4

2 回答 2

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;

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

于 2013-10-01T19:36:04.033 回答
0

Java 中的 Do-while循环。此链接将帮助您解决一些问题。

于 2013-10-01T19:32:59.147 回答