gpt4 book ai didi

Java - If 语句问题 - 更改条件会删除下面的其他语句

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:44 25 4
gpt4 key购买 nike

IT 世界的新手,有一些初学者的问题。因此,我正在学习一些教程,并遇到了一个我自己无法解决的问题。代码如下:

    boolean gameOver = true;
int score = 5000;
int levelCompleted = 5;
int bonus = 100;

if(score == 5000)
System.out.println("Your score is 5000");

if(score > levelCompleted){
System.out.println("Score is greater that levelCompleted");
System.out.println("5000 is greater that 5");
System.out.println("Some new text on true condition");
}

if(score<5000){
System.out.println("Score is equal to 5000");
}
else
{
System.out.println("Condition is false");
}

if((score < 5000) && (score > 1000)){
System.out.println("Both conditions are true");
}
else if(score<1000)
{
System.out.println("Else if condition is true");
}
else
{
System.out.println("None of the condtitions are true!");


if(gameOver == true){
int finalScore = score + (levelCompleted * bonus);
System.out.println("Your final score was " + finalScore);
}


if(gameOver == true){
score = 10000;
levelCompleted = 8;
bonus = 200;
int finalScore= score + (levelCompleted * bonus);
System.out.println("Your final score was " + finalScore);
}


System.out.println(score);
}

System.out.println(levelCompleted);

}

}

上面代码的输出显示了预期的结果,它们是:

Your score is 5000
Score is greater that levelCompleted
5000 is greater that 5
Some new text on true condition
Condition is false
None of the condtitions are true!
Your final score was 5500
Your final score was 11600
10000
8

但是,当我更改 if/else if/else 语句的 if 条件时

 if ( (score < 5000) &&  (score > 1000) )

if( (score == 5000) &&  (score > 1000) )

如您所见,我仅从 < 更改为 == ,并且得到以下输出。

Your score is 5000
Score is greater that levelCompleted
5000 is greater that 5
Some new text on true condition
Condition is false
Both conditions are true
5

那么,下面的另外两个 if-s 发生了什么?

最后两个“独立”system.out.println-s 在哪里?

另外,5现在从哪里来?我想来自 levelCompleted 变量,但是如何呢?

预先感谢您的帮助和解释!

最佳答案

缺少}。请参阅贾斯珀的评论。如果使用 Eclipse,请使用 Ctrl + Shift + F 自动设置代码格式。

关于Java - If 语句问题 - 更改条件会删除下面的其他语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37449638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com