简体   繁体   English

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

[英]I'm having trouble with creating a interactive menu in Java

Our assignment is this https://docs.google.com/document/d/16hHb8WUGehbYLCNrSpJspP8x5GJNCR2q2chnG_yFPSo/pub 我们的任务是这个https://docs.google.com/document/d/16hHb8WUGehbYLCNrSpJspP8x5GJNCR2q2chnG_yFPSo/pub

and it uses a file which is written here https://docs.google.com/document/d/1BxIKe4ZEEpWHmBk2O2FJit6frk5BZSm6SozHOAKWqWU/pub 并且它使用的文件写在这里https://docs.google.com/document/d/1BxIKe4ZEEpWHmBk2O2FJit6frk5BZSm6SozHOAKWqWU/pub

I am not trying to use this site to have people do my project for me, I would to be pointed in the right direction since I am truly lost. 我并不是想利用这个网站来让人们为我做我的项目,因为我真的迷路了,所以我会指出正确的方向。

I am having a bit of trouble with my switch statement and getting the menu to start working. 我在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
}

You are missing the while in the do. 您正在做的事情中错过了。

do {
  ...
}while(condition)

So I believe you want something like: 所以我相信你想要这样的东西:

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

Every case should have its own break, otherwise if you go to case A, it will go then to case D and so on. 每个案例都应有自己的休息时间,否则,如果转到案例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.

To simplify a bit your switch.. if you use check = stdin.readLine().toLowerCase(); 为了简化一点,您的切换..如果使用check = stdin.readLine().toLowerCase();

you can avoid using 2 cases for 1 case: 您可以避免1种情况使用2种情况:

case "a" and remove case "A" (cases of Capital letter may be removed) 大小写“ a”并删除大小写“ A”(大写字母的大小写可能会被删除)

Finally your while will never end if you don't reset the quit value 最后,如果不重置quit值,您的那一刻将永远不会结束

Maybe you can add another case for it like: 也许您可以为其添加另一种情况,例如:

case "q":
    quit=true;
    break;

There is also a default case that is executed if none of the cases are. 如果不存在任何情况,则还会执行一个默认情况。

Do-while loops in Java. Java中的Do-while循环。 This link will help with some of your problems. 此链接将帮助您解决一些问题。

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

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