我是Java编程的新手。几天来我一直在试图解决这个问题。第一个循环跳过 if 语句并给我"无效条目",后续运行很好。我看过很多视频,读过多个问题/答案,但仍然无法弄清楚。
任何帮助或提示将不胜感激。谢谢
很抱歉没有发布图片而不是代码。
我的代码
包5级;
import java.util.Scanner;
公开课练习 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Number of loops");
int numberOfRuns=input.nextInt();
for(int i=0;i<=numberOfRuns;i++) {
String month;
System.out.println("What month were you born in?");
month=input.nextLine();
if(month.equalsIgnoreCase("December")||month.equalsIgnoreCase("January")||month.equalsIgnoreCase("February")) {
System.out.println("You were born in Winter");
}else if(month.equalsIgnoreCase("March")||month.equalsIgnoreCase("April")||month.equalsIgnoreCase("May")){
System.out.println("You were born in Spring");
}else if(month.equalsIgnoreCase("June")||month.equalsIgnoreCase("July")||month.equalsIgnoreCase("August")) {
System.out.println("You were born in Summer");
}else if(month.equalsIgnoreCase("September")||month.equalsIgnoreCase("October")||month.equalsIgnoreCase("November")) {
System.out.println("You were born in fall");
}else {
System.out.println("invalid entry");
}
}
}
}
跳过循环出现问题的原因是下一行被跳过了。 这是一个行终止问题,您应该检查下一行是否包含任何数据,如果没有,则可以将 for 重置为上次迭代。