简体   繁体   English

当我进入该程序时,对#2和#3的选择将不起作用。 谁能帮助我了解为什么它会以这种方式运行?

[英]When I enter this program, my choices for #2 and #3 will not work. Can anyone help me understand why it is behaving this way?

When I enter this program, my choices for #2 and #3 will not work. 当我进入该程序时,对#2和#3的选择将不起作用。 I can choose option 1, enter the data, build the map, then exit and display the map correctly. 我可以选择选项1,输入数据,构建地图,然后退出并正确显示地图。 But when I try to remove or edit the student/grades, it will not work. 但是,当我尝试删除或编辑学生/成绩时,它将无法正常工作。 Can anyone help me understand why it is behaving this way? 谁能帮助我了解为什么它会以这种方式运行? I am also open for suggestions on ways to better my code as I am a noob to java. 我也愿意就改善我的代码的方式提出建议,因为我对Java不熟悉。

import java.util.*;

public class StudentGrades {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Choices choice = new Choices();
        final String WELCOME = "Welcome to the student and grade registry." + "\n" + "Choose from your options below: " + "\n" + "1: Add Students" + "\n" + "2: Remove Students" + "\n" + "3: Modify Grades" + "\n" + "4: Print All Grades" + "\n" + "5: Exit the System";
        int selection;
        boolean sent = true;
        while (sent != false){
            System.out.println(WELCOME);
            selection = in.nextInt();
            if (selection == 5){
                sent = false;
            }
            else if (selection == 1){
                choice.addStudent();
            }
            else if (selection == 2){
                choice.removeStudent();
            }
            else if (selection == 3){
                choice.editGrade();
            }
            else if (selection == 4){
                choice.printAll();
            }
            else{
                System.out.println("You have chosen an incorrect selection, please try again.");
            }
        }   
    }   
}
class Choices implements Comparator<Choices> {
        Scanner in = new Scanner(System.in);
        Map<String, String> map = new TreeMap<String, String>();
        final String STUDENT = "Please enter a student: ";
        final String GRADE = "Please enter a grade: ";
        final String ENTRY = "You have chosen to enter new students. " + "\n" + "To exit data entry at any time press 'e'. "; 
        final String REMOVE = "You have chosen to remove students. " + "\n" + "To exit data entry at any time press 'e'. ";
        final String REM_MESS = "You have chosen to remove ";
        final String REM_MESS2 = " with grade average of ";
        final String GRADE_EDIT = "You have chosen to edit grades. " + "\n" + "To exit data entry at any time press 'e'. ";
        boolean sent = false;
        String student;
        String grade;
        public int compare(Choices a, Choices b){
            return a.toString().compareToIgnoreCase(b.toString());
        }
        public void addStudent(){
            while (sent != true){
                System.out.println(ENTRY);
                System.out.println(STUDENT);
                student = in.nextLine();
                if (student.equals("e")){
                    sent = true;
                    break;
                }
                System.out.println(GRADE);
                grade = in.nextLine();
                if (student.equals("e")){
                    sent = true;
                    break;
                }
                map.put(student, grade);
            }
        }   
        public void removeStudent(){
            while (sent != true){
                System.out.println(REMOVE);
                System.out.println(STUDENT);
                student = in.nextLine();
                if (student.equals("e")){
                    sent = true;
                    break;
                }
                else {
                    System.out.println(REM_MESS + student + REM_MESS2 + map.get(student));
                    map.remove(student);                
                }
            }
        }
        public void editGrade(){
            while (sent != true){
                System.out.println(GRADE_EDIT);
                System.out.println(STUDENT);
                student = in.nextLine();
                if (student.equals("e")){
                    sent = true;
                    break;
                }
                else {
                    System.out.println(student);
                    System.out.println(GRADE);
                    grade = in.nextLine();
                    map.put(student, grade);                
                }
            }
        }
        public void printAll(){
            for (String key : map.keySet()){
                String name = map.get(key);
                System.out.println(key + ": " + name);
            }
        }   
    }

Sample output of the problem I am seeing... 我看到的问题的示例输出...

Welcome to the student and grade registry.
Choose from your options below: 
1: Add Students
2: Remove Students
3: Modify Grades
4: Print All Grades
5: Exit the System
1
You have chosen to enter new students. 
To exit data entry at any time press 'e'. 
Please enter a student: 
Tom
Please enter a grade: 
B
You have chosen to enter new students. 
To exit data entry at any time press 'e'. 
Please enter a student: 
e
Welcome to the student and grade registry.
Choose from your options below: 
1: Add Students
2: Remove Students
3: Modify Grades
4: Print All Grades
5: Exit the System
2
Welcome to the student and grade registry.
Choose from your options below: 
1: Add Students
2: Remove Students
3: Modify Grades
4: Print All Grades
5: Exit the System
2
Welcome to the student and grade registry.
Choose from your options below: 
1: Add Students
2: Remove Students
3: Modify Grades
4: Print All Grades
5: Exit the System

OK so I'm a little tired, but I got your code working as far as I can tell (Please tell me if i missed something): 好的,所以我有点累,但是就我所知,我让您的代码正常工作(请告诉我是否错过了什么):

import java.util.*;

public class StudentGrades {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Choices choice = new Choices();
        final String WELCOME = "Welcome to the student and grade registry." + "\n" + "Choose from your options below: " + "\n" + "1: Add Students" + "\n" + "2: Remove Students" + "\n" + "3: Modify Grades" + "\n" + "4: Print All Grades" + "\n" + "5: Exit the System";
        int selection;
        boolean loop = true;
        while (loop == true) {
            System.out.println(WELCOME);
            try { selection = in.nextInt(); }
            catch (Exception e) { System.out.println(e.toString()); break; }
            if (selection == 5) {
                loop = false;
            } else if (selection == 1) {
                choice.addStudent();
            } else if (selection == 2) {
                choice.removeStudent();
            } else if (selection == 3) {
                choice.editGrade();
            } else if (selection == 4) {
                choice.printAll();
            } else {
                System.out.println("You have chosen an incorrect selection, please try again.");
            }
        }
    }
}

class Choices implements Comparator<Choices> {

    Scanner in = new Scanner(System.in);
    Map<String, String> map = new TreeMap<String, String>();
    final String STUDENT = "Please enter a student: ";
    final String GRADE = "Please enter a grade: ";
    final String ENTRY = "You have chosen to enter new students. " + "\n" + "To exit data entry at any time press 'e'. ";
    final String REMOVE = "You have chosen to remove students. " + "\n" + "To exit data entry at any time press 'e'. ";
    final String REM_MESS = "You have chosen to remove ";
    final String REM_MESS2 = " with grade average of ";
    final String GRADE_EDIT = "You have chosen to edit grades. " + "\n" + "To exit data entry at any time press 'e'. ";
    boolean loop = true;
    String student;
    String grade;

    public int compare(Choices a, Choices b) {
        return a.toString().compareToIgnoreCase(b.toString());
    }

    public void addStudent() {
        while (loop == true) {
            System.out.println(ENTRY);
            System.out.println(STUDENT);
            student = in.nextLine();
            if (student.equals("e")) {
                loop = true;
                break;
            }
            System.out.println(GRADE);
            grade = in.nextLine();
            if (student.equals("e")) {
                loop = true;
                break;
            }
            map.put(student, grade);
        }
    }

    public void removeStudent() {
        while (loop == true) {
            System.out.println(REMOVE);
            System.out.println(STUDENT);
            student = in.nextLine();
            if (student.equals("e")) {
                loop = true;
                break;
            } else {
                System.out.println(REM_MESS + student + REM_MESS2 + map.get(student));
                map.remove(student);
            }
        }
    }

    public void editGrade() {
        while (loop == true) {
            System.out.println(GRADE_EDIT);
            System.out.println(STUDENT);
            student = in.nextLine();
            if (student.equals("e")) {
                loop = true;
                break;
            } else {
                System.out.println(student);
                System.out.println(GRADE);
                grade = in.nextLine();
                map.put(student, grade);
            }
        }
    }

    public void printAll() {
        for (String key : map.keySet()) {
            String name = map.get(key);
            System.out.println(key + ": " + name);
        }
    }
}

As far as your coding style and formatting, everything seems fine to me. 至于您的编码样式和格式,对我来说一切都很好。 :) :)

EDIT: BTW the break; 编辑:顺便说一下break; and loop = false are redundant. loop = false是多余的。 Consider choosing one and not both. 考虑选择一个而不是两个。

暂无
暂无

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

相关问题 谁能帮助我理解这段代码? - Can anyone help me understand this piece of code? 我无法在程序中使用PrinterWriter,有人可以帮我解决吗? - I can't use PrinterWriter in my program, could anyone help me figure it out? 谁能告诉我为什么我的程序不起作用? - Could anyone tell me why my program doesn't work? 无法简单地使用JTable默认单元格编辑器来验证文本。 有人可以帮助理解为什么吗? - This simple use of JTable default cell editor to validate text does not work. Can someone help understand why? 有人可以帮助我理解为什么我需要IF和WHILE来获得这个答案吗? - Can someone help me understand why I need an IF and WHILE for this answer? 任何人都可以帮助我理解 Android Studio 中的 SharedPreferences - Can anyone please help me understand SharedPreferences in Android Studio 谁能帮我理解ArrayList <ArrayDeque<ByteBuffer> &gt;类型? - Can anyone help me to understand the ArrayList<ArrayDeque<ByteBuffer>> type? 谁能帮助我了解Spring 4在此示例中的工作方式? - Can anyone help me to understand how Spring 4 works in this example? 有人可以帮助我了解我的上下文文件如何在Tomcat中使用并从程序中调用吗? - Can someone help me understand how my context file is utilized within Tomcat and called from my program? 当我连续输入2个以上的整数时,为什么我的前缀操作程序不起作用? - Why does my prefix operation program not work when I enter more than 2 integers in a row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM