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);
}
}
大家好,希望你们一切都好。这是我第一次使用 Stack Overflow,因此对于可能犯的任何错误,我提前表示歉意。那么,让我们开始吧!我在学习 Java 时编写了上面的代码作为练习片段,并且从那时起就一直使用它来练习和玩弄 Java。该程序是为了查找素数而设计的,并且已经运行了一段时间了。自从我决定尝试 switch 语句以来,我一直遇到问题。当我去运行代码时,Eclipse IDE 显示“类型不匹配:无法从 boolean 值转换为 int”,因此我的程序拒绝运行。我已经用我转换类型的点对我的代码进行了注释,但我没有将“i”转换为“boolean”类型。如果您知道为什么会出现此问题,请告诉我。如果您需要任何其他信息,请询问!谢谢!
最佳答案
switch(i){
每种情况只能切换 i
的单个值。
请改用以下内容:
if(i < 100){
hex();
}
if(i > 100){
baseTen();
}
我也会处理 i==100
情况。这是留给读者的一个简单练习。
您只能为 Enum
进行 switch
,或者如果您有不同的 int
值,则您只关心单值情况,而不是范围。
关于java - "boolean"s 和 "switch"语句(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914851/