gpt4 book ai didi

java - 尝试在 Eclipse 中执行 boolean 方法时出错

转载 作者:行者123 更新时间:2023-12-01 18:12:47 24 4
gpt4 key购买 nike

我正在使用“Head First Java Second Edition”这本书来学习该语言,但是当在 Eclipse 中输入示例时,它不允许我使用 boolean 方法而不给出错误。使用 Eclipse 使用 Java 初始化 boolean 方法的正确方法是什么?这是我输入的内容:

public class BeerSong {
public static void main (String [] args) {
int beerNum = 99;
String word = "bottles";

While (beerNum > 0);


If (beerNum == 1); {
word = "bottle";
}

System.out.println(beerNum + " "+ word + " of beer on the wall");
System.out.println(beerNum + " "+ word + " of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;

If (beerNum > 0); {
System.out.println (beerNum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}

}
}
}

最佳答案

whileif 是正确的拼写(没有大写)。另外,请勿在 if 之后附加“;”,因为那一端是 block 。

if (beerNum > 0) {
System.out.println (beerNum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}

请注意第一个 if block 的行中缺少分号。其他 if 和 while 语句中的分号也是如此。

if (...) 执行下面的 block ,但分号使其成为空 block ,因此...

if ( something ); { 
doSomething
}

相当于...

if (something ) {
// do absolutely nothing here
}
doSomething;

...因为 if block 执行“;” (什么也没有,而不是 doSomething block ),之后,无论如何,带有 doSomething 的 block 都会被执行。因此,ifwhile 等后面没有分号

关于java - 尝试在 Eclipse 中执行 boolean 方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31723099/

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