Java 即使IF为true,ELSE语句仍在执行
以下代码允许用户输入足球队和分数。如果用户输入的足球队与arraylist中已存在的球队匹配,则执行代码 当外部if语句为true时,为什么else语句仍然执行?(即,即使执行了if语句,Java 即使IF为true,ELSE语句仍在执行,java,arrays,Java,Arrays,以下代码允许用户输入足球队和分数。如果用户输入的足球队与arraylist中已存在的球队匹配,则执行代码 当外部if语句为true时,为什么else语句仍然执行?(即,即使执行了if语句,itCounter仍然递增)。如何确保在执行“If”之后不再执行它 int homeScore = 0; int awayScore = 0; int itCounter = 0; boolean again = true; while (again) { System.out.println("")
itCounter
仍然递增)。如何确保在执行“If”之后不再执行它
int homeScore = 0;
int awayScore = 0;
int itCounter = 0;
boolean again = true;
while (again) {
System.out.println("");
System.out.println("Enter name of Home team: ");
final String homeName = input.next();
Iterator<FootballClub> it = premierLeague.iterator();
while (it.hasNext()) {
if ((it.next().getClubName()).equals(homeName)) {
System.out.println("Enter number of goals scored by " + homeName + ":");
Scanner input2 = new Scanner(System.in);
homeScore = input2.nextInt();
premierLeague.get(itCounter).setGoalsScored(homeScore);
System.out.println("itCounter value = " + itCounter);
again = false;
} else {
itCounter++;
System.out.println("itCounter increased, value = " + itCounter);
}
if (itCounter == premierLeague.size()) {
System.out.println("You must enter a valid team!");
itCounter = 0;
System.out.println("itCounter increased, value = " + itCounter);
}
}
}
int homeScore=0;
int awayScore=0;
int itCounter=0;
布尔值=真;
同时(再次){
System.out.println(“”);
System.out.println(“输入主队名称:”);
最后一个字符串homeName=input.next();
迭代器it=premierLeague.Iterator();
while(it.hasNext()){
if((it.next().getClubName()).equals(homeName)){
System.out.println(“输入“+homeName+”:”)所取得的进球数;
扫描仪输入2=新扫描仪(System.in);
homeScore=input2.nextInt();
英超。获得(itCounter)。设定进球得分(homeScore);
System.out.println(“itCounter值=“+itCounter”);
再次=假;
}否则{
itCounter++;
System.out.println(“itCounter增加,value=“+itCounter”);
}
if(itCounter==premireleague.size()){
System.out.println(“您必须输入有效的团队!”);
itCounter=0;
System.out.println(“itCounter增加,value=“+itCounter”);
}
}
}
为团队打破查找循环:
boolean found = false;
Iterator<FootballClub> it = premierLeague.iterator();
int itCounter = 0;
while (it.hasNext()) {
if ((it.next().getClubName()).equals(homeName)) {
System.out.println("Enter number of goals scored by " + homeName + ":");
// Scanner input = new Scanner(System.in);
homeScore = input.nextInt();
premierLeague.get(itCounter++).setGoalsScored(homeScore);
again = false;
found = true;
break;
}
}
if( ! found ){
System.out.println("You must enter a valid team!");
}
boolean-found=false;
迭代器it=premierLeague.Iterator();
int itCounter=0;
while(it.hasNext()){
if((it.next().getClubName()).equals(homeName)){
System.out.println(“输入“+homeName+”:”)所取得的进球数;
//扫描仪输入=新扫描仪(System.in);
homeScore=input.nextInt();
英超.get(itCounter++).setgoalscore(homeScore);
再次=假;
发现=真;
打破
}
}
如果(!找到){
System.out.println(“您必须输入有效的团队!”);
}
注1:在所有变量正确重置的情况下,注意重新启动循环搜索团队
注2:不要在同一输入流上创建另一个扫描仪。为团队中断查找循环:
boolean found = false;
Iterator<FootballClub> it = premierLeague.iterator();
int itCounter = 0;
while (it.hasNext()) {
if ((it.next().getClubName()).equals(homeName)) {
System.out.println("Enter number of goals scored by " + homeName + ":");
// Scanner input = new Scanner(System.in);
homeScore = input.nextInt();
premierLeague.get(itCounter++).setGoalsScored(homeScore);
again = false;
found = true;
break;
}
}
if( ! found ){
System.out.println("You must enter a valid team!");
}
boolean-found=false;
迭代器it=premierLeague.Iterator();
int itCounter=0;
while(it.hasNext()){
if((it.next().getClubName()).equals(homeName)){
System.out.println(“输入“+homeName+”:”)所取得的进球数;
//扫描仪输入=新扫描仪(System.in);
homeScore=input.nextInt();
英超.get(itCounter++).setgoalscore(homeScore);
再次=假;
发现=真;
打破
}
}
如果(!找到){
System.out.println(“您必须输入有效的团队!”);
}
注1:在所有变量正确重置的情况下,注意重新启动循环搜索团队
注意2:不要在同一输入流上创建另一个扫描仪。您所指的是哪个
if
语句?必须在设置为再次=false的位置插入一个break语句。@sstan只有一个if-else语句@SamG:我明白了,但我只是想确定你指的不是第二个独立的if
,因为你问的没有意义。您可能认为if
和else
部分都在执行,但它们不是。也许你只是在观察while
循环的两个不同迭代。while循环的原样与(足球俱乐部:英超联赛){}的相同,它将迭代所有俱乐部,为每个非homeName
的俱乐部输入else
子句。这是已编码的,是否符合预期由您决定。您所指的是哪个if
语句?您必须在再次设置为false的位置插入一个break语句。@sstan只有一个if-else语句@SamG:我明白了,但我只是想确定你指的不是第二个独立的if
,因为你问的没有意义。您可能认为if
和else
部分都在执行,但它们不是。也许你只是在观察while
循环的两个不同迭代。while
循环的原样与(足球俱乐部:英超联赛){}的相同,它将迭代所有俱乐部,为每个非homeName
的俱乐部输入else
子句。这是编码,是否是预期的由你决定。