java: 不兼容的类型:无法将boolean转换为int

huangapple go评论80阅读模式
英文:

java: incompatible types: boolean cannot be converted to int

问题

I encountered error 'incompatible types: boolean cannot be converted to int'

public static void main(String[] args) {

    for (int i = 1; i <= 100; i++) {
        switch (i) {

            case (i % 3 == 0 && i % 5 == 0):{
                System.out.println("Number #" + i + " :Number is divisible by both 3 and 5" + "-->" + "FizzBuzz");
                break;
                }

            case (i % 5 == 0 && i % 7 == 0): {
                System.out.println("Number #" + i + " :Number is divisible by both 5 and 7" + "-->" + "FizzBuzz");
                break;
            }
        }
    }
}
英文:

I encountered error 'incompatible types: boolean cannot be converted to int'

public static void main(String[] args) {

    for (int i = 1; i &lt;= 100; i++) {
        switch (i) {

            case (i % 3 == 0 &amp;&amp; i % 5 == 0):{
                System.out.println(&quot;Number #&quot; + i + &quot; :Number is divisible by both 3 and 5&quot; + &quot;---&gt;&quot; + &quot;FizzBuzz&quot;);
                break;
                }

            case (i % 5 == 0 &amp;&amp; i % 7 == 0): {
                System.out.println(&quot;Number #&quot; + i + &quot; :Number is divisible by both 5 and 7&quot; + &quot;---&gt;&quot; + &quot;FizzBuzz&quot;);
                break;
            }

答案1

得分: 2

在Java中,switch语句不像这样工作。你需要在case中使用编译时常量表达式,并且有一些类型限制。

只需使用if语句:

for (int i = 1; i <= 100; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
        System.out.println("Number #" + i + " :Number is divisible by both 3 and 5" + "--->" + "FizzBuzz");
    }

    if (i % 5 == 0 && i % 7 == 0) {
        System.out.println("Number #" + i + " :Number is divisible by both 5 and 7" + "--->" + "FizzBuzz");
    }
}

如果你想学习如何使用switch语句,可以参考例如Java教程(或者基本上通过搜索"Java switch语句"在互联网上找到的任何其他来源)。

英文:

The switch statement in Java just does not work like this. You need to use compile time constant expressions in the cases, and there are restrictions to what types you can use.

Just use if-statements:

for (int i = 1; i &lt;= 100; i++) {
    if (i % 3 == 0 &amp;&amp; i % 5 == 0) {
        System.out.println(&quot;Number #&quot; + i + &quot; :Number is divisible by both 3 and 5&quot; + &quot;---&gt;&quot; + &quot;FizzBuzz&quot;);
    }

    if (i % 5 == 0 &amp;&amp; i % 7 == 0) {
        System.out.println(&quot;Number #&quot; + i + &quot; :Number is divisible by both 5 and 7&quot; + &quot;---&gt;&quot; + &quot;FizzBuzz&quot;);
    }

If you want to learn how to use the switch statement, see for example The Java Tutorial (or basically any other source you find on the internet by searching "Java switch statement")

答案2

得分: 1

你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:

"Hmm you are getting that error cause you are trying use a boolean operator inside a case statement. In a switch-case statement case must contain the same type as that of switch."

英文:

Hmm you are getting that error cause you are trying use a boolean operator inside a case statement. In a switch-case statement case must contain the same type as that of switch.

huangapple
  • 本文由 发表于 2020年8月7日 03:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63290272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定