嵌套if-else vs if-else?
问题描述:
与if-else if语句相比,我对“嵌套if-else语句”的含义感到困惑。嵌套if-else vs if-else?
下面的问题要求使用它而不是if-else语句,但我不知道如何。帮助将非常感激!
if (playerThrow == ROCK && computerThrow == ROCK) {
System.out.println("Its a draw!");
} else if (playerThrow == ROCK && computerThrow == PAPER) {
System.out.println("Computer wins!");
} else if (playerThrow == ROCK && computerThrow == SCISSORS) {
System.out.println("Player wins!");
Code is from my textbook (I messed up), else I would have posted mine, sorry
答
一个nested if statement
基本上是在彼此像这样一系列if statements
...
if (<condition>)
{
if (<condition>)
{
...
} else {
...
}
} else {
...
}
If-else if statements
make usin g多幅条件更清洁,更易于阅读,全部是在同一水平上,像这样:
if (<condition 1>)
{
...
}
else if (<condition 2>)
{
...
}
else
{
...
}
益的一个小白问题 – Drew
我们曾@Drew ...... – ggdx
大家菜鸟,欢迎SO!你能不能编辑一下你的问题?我们(大部分时间)*不接受“法恩斯法师”。请直接写入问题。此外,链接可能会随着时间的推移而中断,因此只能将它们用作参考。无论如何,我们在这里帮助你;)。 – 3442