“布尔”和“switch”语句(错误)

问题描述 投票:0回答:4

我希望你们一切都好。这是我第一次使用 Stack Overflow,因此对于可能犯的任何错误,我提前表示歉意。那么,让我们开始吧!

import java.math.BigInteger;

public class Classes {

  static int i;        // "i" is initialized
  static int x = 200;  // FYI, x is the upper limit for primes to be found (ex, in this case, 0 - 200)

  public static void main(String[] args) {
    for (i = 0; i < x;) {                      // "i" is assigned the value of 0 
      BigInteger one = BigInteger.valueOf(i); // The following lines find the prime(s)
      one = one.nextProbablePrime();          // Order by which primes are found - int > BigInteger > int
      i = one.intValue();                    //'i" is re-assigned the a value
    
      if (i >= x) {     
        System.exit(i);     
      }
    
      switch (i) {
        case i < 100:      // ERROR HERE, "Type mismatch: cannot convert from boolean to int"
          hex();
          break;
        case i > 100:      // ERROR HERE, "Type mismatch: cannot convert from boolean to int"
          baseTen();
          break;
      }
    }
  }

  static void hex() { //Probably unimportant to solving the problem, but this is used to convert to hex / print
    String bla = Integer.toHexString(i);
    System.out.println(bla);
  }

  static void baseTen(){  //Probably unimportant to solving the problem, but this is used print
    System.out.println(i);
  }
}

我在学习 Java 时编写了上面的代码作为练习片段,并且从那时起就一直使用它来练习和玩弄 Java。该程序是为了查找素数而开发的,并且已经运行了一段时间了。

自从我决定尝试 switch 语句以来,我一直遇到问题。当我去运行代码时,Eclipse IDE 显示“类型不匹配:无法从布尔值转换为整数”,因此我的程序拒绝运行。

我已经用我转换类型的点对我的代码进行了注释,但我没有地方将“i”转换为“boolean”类型。如果您知道为什么会出现此问题,请告诉我。如果您需要任何其他信息,请询问!谢谢!

java casting
4个回答
5
投票
switch(i){

对于每种情况只能切换

i
的单个值。

请使用以下内容:

if(i < 100){
    hex();
}
if(i > 100){
    baseTen();
}

我也会处理

i==100
的案件。这是留给读者的一个简单练习。

您只能为

switch
指定
Enum
,或者如果您有不同的
int
值,则您只关心单值情况,而不是范围。


1
投票

switch
是一个旨在针对多种可能性测试一个变量的元素,如下所示:

switch (a) {
case 1:
  // this runs if a == 1
  break;
case 2:
  // this runs if a == 2
  // NOTE the lack of break
case 3:
  // this runs if a == 2 or a == 3
  break;
default:
  // this runs if a is none of the above
}

请注意,switch 子句 (

a
) 中的类型应与 case 子句 (
1
) 中的类型匹配。 case 子句不能是任意布尔值。

当然,如果你想准确指定条件是什么,你可以使用“if/else”块,如下所示:

if (a == 1) {
  //...
} else if (a == 2) {
  //...
} else if (a == 3) {
  //...
} else {
  // runs if none of the above were true
}

后一个例子更接近你想要的;不是用

==
来测试每个,而是直接评估
if
之后的每个布尔表达式。你的看起来更像这样:

if (i < 100) {
    hex();
} else if (i > 100) {
    baseTen();
}

当然,它们可以保留两个单独的子句,但因为它们是相互排斥的,所以仍然使用

else
是有意义的。您还没有考虑到
i == 100
的情况,这可能需要将
<
更改为
<=
>
更改为
>=


0
投票
Java 和大多数其他语言中的

switch
语句适用于常量,而不是条件。所以,你可以写

switch (i)
{
  case 1:
    do_something();
    break;
  case 2:
    do_something_else();
    break;
  default:
    third_option();
}

但是,您的条件涉及比较而不是条件,因此在 Java 中它们需要 if 语句:

if (i < 100)
  hex();
else if (i > 100)
  baseTen();

您可以在此处找到 switch 语句的详尽描述。


-1
投票

欢迎来到SO。如果您阅读 Java 文档,它会指出:

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type, or a compile-time error occurs.

请参阅此处的参考:Java Switch

© www.soinside.com 2019 - 2024. All rights reserved.