【问题标题】:I'm having trouble with creating a interactive menu in Java在 Java 中创建交互式菜单时遇到问题
【发布时间】:2013-10-01 19:28:57
【问题描述】:

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

【问题讨论】:

  • 您正在尝试使用没有whiledo-while 循环。这在语法上是不正确的。
  • 而且,没有break;s 的 switch-case 语句会提供一些惊喜...
  • 你能在Java中打开一个字符串吗?我想你需要打开一个字符,对吧?是的,如果你有评论 //Error saying while is expected,需要替换为类似 `while (//some condition);
  • @nhgrif 你可以从 Java 7 开始。
  • 在 java 7 中你可以切换字符串,但我认为你可以使用 check.toUpperCase 或 toLowerCase 并避免每种情况使用 2 种情况。

标签: java class switch-statement


【解决方案1】:

你在做的时候错过了。

do {
  ...
}while(condition)

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

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

每个case都应该有自己的break,否则如果你去case A,它会去case 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;

如果没有任何情况,也会执行默认情况。

【讨论】:

  • 所以每次休息后我都会有一段时间;让程序按你说的做?
  • @nickkbmod 不,while 独立于 switch,它属于 do,在这种情况下 switch 语句应该在 do { ...here... } while(condition)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多