Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我进入这个项目时,我对#2和#3的选择将不起作用。有人能帮我理解它为什么会这样吗?_Java - Fatal编程技术网

Java 当我进入这个项目时,我对#2和#3的选择将不起作用。有人能帮我理解它为什么会这样吗?

Java 当我进入这个项目时,我对#2和#3的选择将不起作用。有人能帮我理解它为什么会这样吗?,java,Java,当我进入这个项目时,我对#2和#3的选择将不起作用。我可以选择选项1,输入数据,构建地图,然后退出并正确显示地图。但当我试图删除或编辑学生/成绩时,它将不起作用。有人能帮我理解它为什么会这样吗?由于我是java的noob,我也愿意听取关于如何改进代码的建议 import java.util.*; public class StudentGrades { public static void main(String[] args) { // TODO Auto-gene

当我进入这个项目时,我对#2和#3的选择将不起作用。我可以选择选项1,输入数据,构建地图,然后退出并正确显示地图。但当我试图删除或编辑学生/成绩时,它将不起作用。有人能帮我理解它为什么会这样吗?由于我是java的noob,我也愿意听取关于如何改进代码的建议

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);
            }
        }   
    }

好吧,我有点累了,但我已经尽我所能让你的代码正常工作了(如果我遗漏了什么,请告诉我):

import java.util.*;
公营班级学生成绩{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪输入=新扫描仪(系统输入);
选择=新的选择();
最后一个字符串WELCOME=“欢迎使用学生和成绩注册表。”+“\n”+”从下面的选项中选择:“+”\n”+“1:添加学生”+“\n”+“2:删除学生”+“\n”+“3:修改成绩”+“\n”+“4:打印所有成绩”+“\n”+“5:退出系统”;
int选择;
布尔循环=真;
while(循环==true){
System.out.println(欢迎);
请尝试{selection=in.nextInt();}
catch(异常e){System.out.println(e.toString());break;}
如果(选择==5){
循环=假;
}else if(选择==1){
choice.addStudent();
}else if(选择==2){
choice.removeStudent();
}else if(选择==3){
choice.editGrade();
}else if(选择==4){
choice.printAll();
}否则{
System.out.println(“您选择的选项不正确,请重试。”);
}
}
}
}
类选择实现了Comparator{
扫描仪输入=新扫描仪(系统输入);
Map Map=newtreemap();
最后一个字符串STUDENT=“请输入学生:”;
final String GRADE=“请输入等级:”;
final String ENTRY=“您已选择输入新学员。”+“\n”+”要随时退出数据输入,请按“e”;
final String REMOVE=“您已选择删除学生。”+“\n”+”要随时退出数据输入,请按“e”;
最后一个字符串REM_MESS=“您已选择删除”;
最终字符串REM_MESS2=“平均等级为”;
final String GRADE_EDIT=“您已选择编辑成绩。”+“\n”+”要随时退出数据输入,请按“e”;
布尔循环=真;
弦乐学生;
串级;
公共整数比较(选项a、选项b){
返回a.toString().compareToIgnoreCase(b.toString());
}
公共图书馆(学生){
while(循环==true){
系统输出打印项次(输入);
系统输出打印(学生);
student=in.nextLine();
如果(学生等于(e){
循环=真;
打破
}
系统输出打印项次(等级);
等级=in.nextLine();
如果(学生等于(e){
循环=真;
打破
}
地图。放置(学生、年级);
}
}
公共无效再投资(){
while(循环==true){
系统输出打印项次(删除);
系统输出打印(学生);
student=in.nextLine();
如果(学生等于(e){
循环=真;
打破
}否则{
System.out.println(REM_MESS+student+REM_MESS2+map.get(student));
地图。移除(学生);
}
}
}
公共职系(){
while(循环==true){
系统输出打印LN(等级编辑);
系统输出打印(学生);
student=in.nextLine();
如果(学生等于(e){
循环=真;
打破
}否则{
系统输出打印(学生);
系统输出打印项次(等级);
等级=in.nextLine();
地图。放置(学生、年级);
}
}
}
public void printAll(){
for(字符串键:map.keySet()){
字符串名称=map.get(键);
System.out.println(键+“:”+名称);
}
}
}
就您的编码风格和格式而言,我觉得一切都很好。:)
编辑:顺便说一下
中断
loop=false
是冗余的。考虑选择一个而不是两者。

我会试试你的代码,但是你能把你的问题缩小到更少的来源吗?肖恩,我不确定你的意思。每个单独的方法都独立工作,因为如果我先尝试每个方法,它将正常运行。当我试着返回并选择第二个选项时,它不起作用。肖恩,我支持这个令人厌倦的评论,哈哈。它仍然在做同样的事情。如果选择选项1,添加一些学生和分数,退出,然后选择选项2。我只是一遍又一遍地得到主方法菜单。我没有考虑try/catch短语,我很欣赏循环布尔语句的编辑。@user3424867我所做的是添加一个用户,输入e进入主循环,然后删除一个用户,最后再次输入e退出删除循环。我将再次尝试代码,看看还能找到什么:)我试图显示我看到的内容,但它没有正确显示。谢谢你的帮助。我使用了loop=false和break;最初是因为选项1。如果我在询问学生时只使用选项1中的loop=false,它仍然会询问分数,所以我想打破它,所以我找到了答案。我删除了loop=false,只使用break;它成功了。知道为什么吗?
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
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);
        }
    }
}