[英]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);
}
}
}
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.