2013-07-29 49 views
0
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以来​​一直使用它。该计划是为了找到素数,并且已经工作了一段时间了。自从我决定尝试切换语句以来,我一直有问题。当我去运行代码时,Eclipse IDE会提示“类型不匹配:无法从布尔型转换为int型”,因此我的程序拒绝运行。我用我投入类型的地方评论了我的代码,并且无处投入“布尔”类型的“我”。如果您对发生此问题的原因有任何疑问,请告诉我。如果您需要任何其他信息,请不要问!谢谢!“布尔”和“开关”语句(错误)

+2

你需要了解在一本书上的Java有一个例子,'之开关语句。这不是它应该使用的方式。 – dasblinkenlight

回答

5
switch(i){ 

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

请改用以下:

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

我还处理i==100情况下也是如此。这只是一个简单的练习给读者。

对于Enum,您只能使用switch,或者如果您具有不同的int值,那么您只关心单个值情况而非范围。

+0

请注意,这里的'break'语句只是有意义的,因为你的'if'实际上是在'for'循环中,并且会导致执行跳出'for'循环。在'switch'语句中,'break'跳到大括号的末尾;然而,['break'不会影响'if语句](http://stackoverflow.com/questions/4154553/the-command-break-in-java-what-if)。 –

+0

@JeffBowman对不起,我在这里一味地复制粘贴。现在已经修复了。 – hexafraction

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 
} 

注意,开关条款(a)在类型应该匹配的情况下子句的类型(1)。案例子句不能是任意的布尔值。

当然,如果你想指定正是条件是,你可以使用“的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

switch在Java和大多数其他语言中的语句对常量而不是条件有效。所以,你可以写

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语句here的详尽描述。

0

欢迎来到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

+0

@downvoter请解释你厌恶这个正确的答案,并附有引用和引文。 +1 – EJP

+1

他试图比较int值,而不是布尔值或任何其他非法的交换机,这可以通过阅读代码后的段落轻松看出。问题出在switch语句格式中,而不是使用不兼容的数据类型。上面的答案回答了一个没人问的问题。虽然它是正确的,但它对OP没有用处。 – Kon

+0

“自从我决定尝试切换语句以来,我一直在遇到问题。”他问他为什么不能使用条件作为案例。 – OldProgrammer