【发布时间】:2018-12-07 04:02:07
【问题描述】:
我一直在尝试调试我的代码,但仍然无法弄清楚错误在哪里,请您帮帮我好吗? 该程序将用户输入添加到地图中,直到用户对问题“你想添加更多学生吗?”回答“否”。 但是,无论用户输入“是”还是“否”,程序都会停止运行。 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));
});
}
【问题讨论】:
-
你有很多电话给
nextLine(),但是在你打电话给nextInt()之后你有answer = in.nextLine()。这会将一个空的String解析为answer,循环条件将变为假,if将不会输入,程序将结束 -
我也认为你想要
while(!answer.equalsIgnoreCase("no"));
标签: java dictionary arraylist do-while