Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;如果(int==1)";不';行不通_Java_If Statement - Fatal编程技术网

Java &引用;如果(int==1)";不';行不通

Java &引用;如果(int==1)";不';行不通,java,if-statement,Java,If Statement,因此,我正在努力学习Java,并一直在编写一个基于21场比赛的基本程序;每名球员轮流进行1、2或3场比赛。参加最后一场比赛的选手输了 它一直进展顺利,直到我发现当用户即将获胜时,程序不会输入最终的else if语句;它将跳过它并返回给用户,而计算机没有进行任何匹配。附件是我写给计算机的方法 public static int ComputerPlays(int matches){ //Method for the computers turn int matchesTaken = 0;

因此,我正在努力学习Java,并一直在编写一个基于21场比赛的基本程序;每名球员轮流进行1、2或3场比赛。参加最后一场比赛的选手输了

它一直进展顺利,直到我发现当用户即将获胜时,程序不会输入最终的
else if
语句;它将跳过它并返回给用户,而计算机没有进行任何匹配。附件是我写给计算机的方法

public static int ComputerPlays(int matches){ //Method for the computers turn
    int matchesTaken = 0;

    if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
    else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
    else if (matches < 5){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }

    System.out.println("Computer takes " + matchesTaken + " matches");
    return(matches - matchesTaken);
}
publicstaticintcomputerplays(int匹配){//computers的方法
int matchesTaken=0;
如果(matches>7){matchesTaken=new Random().nextInt(3)+1;}
else如果(matches>4&&matches<8){matchesTaken=matches-4;}
else如果(matches<5){matchesTaken=matches-1;}
如果(matches==1){matchesTaken=1;}
System.out.println(“计算机获取”+matchesTaken+“匹配”);
返回(匹配项-匹配项);
}
else if(matches==1){matchesTaken=1;}
是我遇到问题的地方。任何帮助都将不胜感激


另外,我意识到我可以将
else if
转换为
else
语句,但主要的是我想知道问题是什么,而不仅仅是绕开它。

匹配项<5总是在匹配项==1之前到达,这意味着它永远不会触发第二个if。因此,您要么更改if的顺序,要么在小于5的条件中添加另一个参数,即(匹配项<5&&matches>1)。

条件
(匹配项==1)
将不可访问,因为您的前一个条件总是使您的后一个条件为真,即,
if(匹配项<5)
实际上考虑了
匹配项==1
。您可以将最后两个条件设置为:

else if (matches < 5 && matches != 1){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }
else如果(匹配<5&&matches!=1){matchesTaken=matches-1;}
如果(matches==1){matchesTaken=1;}
或者

else if (matches < 5 && matches > 1){ matchesTaken = matches - 1; }
    else if (matches == 1){ matchesTaken = 1; }
else如果(匹配<5&&matches>1){matchesTaken=matches-1;}
如果(matches==1){matchesTaken=1;}

您的问题在于

else if (matches < 5){ matchesTaken = matches - 1; }
else如果(匹配<5){matchesTaken=matches-1;}
当匹配项为1时,将为true。因此,将永远不会输入else if。你可以把它改成

else if (matches < 5 && matches != 1){ matchesTaken = matches - 1; }
else如果(匹配<5&&matches!=1){matchesTaken=matches-1;}
这样,当匹配数为1且以下块可以成功执行时,它将不会触发。

Put

else如果(matches==1){matchesTaken=1;}

以前

else如果(匹配<5){matchesTaken=matches-1;}


订单很重要。在IF_ELSE中,一旦匹配发生,它不会检查下一个条件。在您的情况下,作为
匹配您已经检查了
,否则如果(匹配<5)
则检查是无用的
else if(matches==1)
,因为上述条件将结束if链。所以你可以改变
else如果(匹配<5)
else if(matches<5&&matches!=1)
使最后一条语句也可访问

您应该将
(matches==1)
移动到
(matches<5)
条件之前。在
(匹配==1)
之前无法访问代码,因为
(匹配<5)
也满足此条件

更新代码:

public static int ComputerPlays(int matches){ //Method for the computers turn
    int matchesTaken = 0;

    if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
    else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
    else if (matches == 1){ matchesTaken = 1; }
    else if (matches < 5){ matchesTaken = matches - 1; }

    System.out.println("Computer takes " + matchesTaken + " matches");
    return(matches - matchesTaken);
}
publicstaticintcomputerplays(int匹配){//computers的方法
int matchesTaken=0;
如果(matches>7){matchesTaken=new Random().nextInt(3)+1;}
else如果(matches>4&&matches<8){matchesTaken=matches-4;}
如果(matches==1){matchesTaken=1;}
else如果(matches<5){matchesTaken=matches-1;}
System.out.println(“计算机获取”+matchesTaken+“匹配”);
返回(匹配项-匹配项);
}

使用else if时,请先使用相等条件(=),然后选择大于或等于(X>=Y)或小于或等于(X
if(matches>7){matchesTaken=new Random().nextInt(3)+1;}
else如果(matches>4&&matches<8){matchesTaken=matches-4;}
else如果(matches<5){matchesTaken=matches-1;}
如果(matches==1){matchesTaken=1;}
在代码中,首先执行
else if(matches<5){matchesTaken=matches-1;}


这一行首先执行,并且这一行也满足if
matches==1
,这就是为什么当
matches==1
1小于5时,它将永远不会转到下一行(
else if(matches==1){matchesTaken=1;}
)的原因。这意味着当matches等于1时,它将始终被捕获到“else if”语句中(matches<5){matchesTaken=matches-1;}”


这就是为什么(matches==1)永远不会执行的原因。

if
matches
的值为
1
部分
if(matches<5){
将被执行,因此您永远不会到达
if(matches==1)
此代码从不检查
匹配==1
,因为
首先匹配<5
匹配!这就是在调试器中单步执行代码的真正帮助所在。
if (matches > 7){ matchesTaken = new Random().nextInt(3)+1; }
else if (matches > 4 && matches < 8){ matchesTaken = matches - 4; }
else if (matches < 5){ matchesTaken = matches - 1; }
else if (matches == 1){ matchesTaken = 1; }