我已经尝试调试代码很长时间了,但仍然不知道错误出在哪里,请您能帮我吗? 该程序会将用户输入的内容添加到地图中,直到用户对“是否要添加更多学生?”的问题回答“否”。 但是,无论用户输入“是”还是“否”,程序都会停止运行。 do / while循环可能存在问题,我不确定
//there is swith statement above and HashMap<String, ArrayList> students = new HashMap()
case 2:
ArrayList<Integer> scores = new ArrayList<>();
String name;
String answer;
do {
System.out.print("Please enter student's name: ");
name = in.nextLine();
in.nextLine();
System.out.print("Please enter " + name + "'s scores: ");
scores.add(in.nextInt());
System.out.println("Do you want to add more students yes/no?");
answer = in.nextLine();
in.nextLine();
} while (answer.equals("no"));
students.put(name, scores); //adding names and scores into HashMap<String, ArrayList> students = new HashMap()
if (answer.equals("no")) {
System.out.println("Student Quiz Scores");
keys.forEach((i) -> {
System.out.println(i + "'s quiz scores: " + students.get(i));
});
}
答案 0 :(得分:0)
使用answer = in.next()代替answer = in.nextLine()。
next()=查找并返回此扫描器的下一个完整标记。完整标记的前面是与分隔符模式匹配的输入。
nextLine()=使此扫描程序前进到当前行并返回跳过的输入。