2013-07-29 47 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