"boolean"和"switch"语句(错误)

Nov*_*ice 0 java casting

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);
}
}
Run Code Online (Sandbox Code Playgroud)

各位大家好,我希望你们都做得很好.这是我第一次使用Stack Overflow,所以我提前为我可能犯的任何错误道歉.那么,让我们来吧!我在学习Java时编写了上面的代码作为练习片,并且自从练习和使用Java以来​​一直在使用它.该计划旨在寻找素数,并已经工作了一段时间.自从我决定尝试切换语句以来,我一直遇到问题.当我去运行代码时,Eclipse IDE会说"类型不匹配:无法从布尔值转换为int",因此我的程序拒绝运行.我已经用我投射类型的点来评论我的代码,并且无处可选地将"i"转换为"boolean"类型.如果您对发生此问题的原因有任何疑问,请告诉我.如果您需要任何其他信息,请询问!谢谢!

ζ--*_*ζ-- 5

switch(i){
Run Code Online (Sandbox Code Playgroud)

只能i为每种情况切换单个值.

请改用以下内容:

if(i < 100){
    hex();
}
if(i > 100){
    baseTen();
}
Run Code Online (Sandbox Code Playgroud)

我也会处理这个i==100案子.这对读者来说只是一个简单的练习.

您只能switch为一个Enum或者如果您有不同的int值,您只关心单值案例,而不是范围.