java - Collection/菜单

标签 java collections

    List<Student> studentInfo = new LinkedList<Student>();

    int choice;
    boolean flag = true;
    Student student = new Student();


    while(flag)
    {
        System.out.println();
        System.out.println("Press 1 to Add Student details");
        System.out.println("Press 2 to Display Student details");
        System.out.println("Press 3 to Sort");
        System.out.println("Press 4 to Search");
        System.out.println("Press 5 to Exit");
        System.out.println("Enter your choice: ");
        choice = sc1.nextInt();

        switch(choice)
        {
            case 1:  studentInfo.add(student.addDetails());
                     break;

            case 2:  System.out.println("Details of Students are as follows: ");
                     for(Student s : studentInfo){
                         System.out.println(s);
                     }
                     break;
            //More code

Student类中的addDetails()方法是:

        public Student addDetails() 
        {
             System.out.println("Enter the name: ");
             name = sc2.nextLine();
             this.setName(name);
             return this;
        }

我使用 case 1 block 获取学生详细信息,然后将它们添加到 StudentInfo 集合中。但是,当我显示最后输入的详细信息时,会覆盖所有以前的详细信息,而当我打印它们时,仅显示我添加的学生数量。有人可以告诉我我做错了什么吗?谢谢!

输出: 学生详情如下: 姓名=阿马尔,年龄=0,学期=0,sub_1_marks=0,sub_2_marks=0,sub_3_marks=0,百分比=0,totalMarks=0 姓名=阿马尔,年龄=0,学期=0,sub_1_marks=0,sub_2_marks=0,sub_3_marks=0,百分比=0,totalMarks=0

最佳答案

您不确定此问题的答案这一事实意味着它的答案可能会随着您的代码的开发而改变。如果您关注代码随着时间的推移而发展的事实,您通常会看到正确的路径。

对我来说,这段代码已经有问题了。事实上,如果您想添加新的菜单选项,则必须在两个不同的位置(打印列表和 case 语句)添加代码。

我首先将这两个区域重新整合到一个操作列表中。像这样的事情:

static boolean exit = false;

enum Action {
  AddDetails("Add student details") {
    @Override
    public void doIt() {
      // How to add details.
    }

  },
  DisplayDetails("Display student details") {
    @Override
    public void doIt() {
      // How to display details.
    }

  },
  SortDetails("Sort") {
    @Override
    public void doIt() {
      // How to sort details.
    }

  },
  SearchDetails("Search") {
    @Override
    public void doIt() {
      // How to search details.
    }

  },
  Exit("Exit") {
    @Override
    public void doIt() {
      // How to exit.
      exit = true;
    }

  };
  private final String description;

  Action(String description) {
    this.description = description;
  }

  public String getDescription() {
    return description;
  }

  public abstract void doIt();

}

public static void main(String args[]) {
  try {
    Scanner sc1 = new Scanner(System.in);
    do {
      // Show my menu.
      for (Action a : Action.values()) {
        System.out.println("Press " + a.ordinal() + " to " + a.getDescription());
      }
      System.out.println("Enter your choice: ");
      int choice = sc1.nextInt();
      // Should really do some range checks here.
      Action action = Action.values()[choice];
      // Perform the chosen function.
      action.doIt();
    } while (!exit);
  } catch (Throwable t) {
    t.printStackTrace(System.err);
  }
}

所以 - 为了回答你的问题 - 使用静态方法机制,但仅使用概念。如果您有许多不同的操作,Enum 是一个很好的替代品。

关于java - Collection/菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18739364/

相关文章:

java - Java Applet 连接我们的服务器调用 PHP 文件时出现问题

java - Spring Security oauth2 客户端 - Twitter 问题

java - 我应该重写 Collections 的 hashCode() 吗?

C# - StringDictionary - 如何使用单个循环获取键和值?

java - String IdentityHashMap 与 HashMap 性能对比

java - 如何使用 CAPTURE 绑定(bind)创建 AST?

java - Gradle 多模块项目编译失败,来自子模块中提到的依赖项的 "classNotFoundExceptions"

java - Ehcache与数据库不同步

c# - 从 .NET 词典中删除与谓词匹配的多个项目的最佳方法?

java - 使用 getter 和 setter 方法将列表添加到 bean 属性之间的区别?