2015-04-06 66 views
0

我對Java很陌生並從書中學習。我剛剛在書中瞭解了鑄造類型和布爾值/邏輯運算符,並且我正面臨挑戰。真假值的程序邏輯運算符的表,我有那麼它顯示1和0,而不是真假進行調整,所以我想出了這個:我必須做一個Java挑戰showig true/false值爲1和0

/* Project 2-2: a truth table for the logical operators. 
    show 1 en 0 ipv true en false. 
*/ 

class LogicalOpTableSimon { 
public static void main(String args[]) { 
    int p, q; 
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); 

    p = 1; q = 1; 

    System.out.print(p + "\t" + q +"\t"); 
    System.out.print(p + "\t" + q + "\t"); 
    p = 0; 
    System.out.println(p + "\t" + p); 


    p = 1; q = 0; 

    System.out.print(p + "\t" + q +"\t"); 
    System.out.print(q + "\t" + p + "\t"); 
    System.out.println(p + "\t" + q); 

    p = 0; q = 1; 

    System.out.print(p + "\t" + q +"\t"); 
    System.out.print(p + "\t" + q + "\t"); 
    System.out.println(q + "\t" + q); 

    p = 0; q = 0; 

    System.out.print(p + "\t" + q +"\t"); 
    System.out.print(p + "\t" + q + "\t"); 
    p = 1; q = 0; 
    System.out.println(q + "\t" + p); 
} 
} 

這工作得很好,並表所示正確的值。但是,我想知道,因爲我剛剛在書中學會了如何轉換或轉換不同類型,是否意味着使用它?換句話說,我可以使用鑄造/轉換來獲得相同的結果嗎?我嘗試過不同的事情,但沒有奏效。或者也許我在想困難。感謝您的任何提示,如果你有他們:)。順便說一句,原來的代碼是:

// Project 2-2: a truth table for the logical operators. 
class LogicalOpTable { 
public static void main(String args[]) { 
boolean p, q; 
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); 

p = true; q = true; 

System.out.print(p + "\t" + q +"\t"); 
System.out.print((p&q) + "\t" + (p|q) + "\t"); 
System.out.println((p^q) + "\t" + (!p)); 

p = true; q = false; 

System.out.print(p + "\t" + q +"\t"); 
System.out.print((p&q) + "\t" + (p|q) + "\t"); 
System.out.println((p^q) + "\t" + (!p)); 

p = false; q = true; 

System.out.print(p + "\t" + q +"\t"); 
System.out.print((p&q) + "\t" + (p|q) + "\t"); 
System.out.println((p^q) + "\t" + (!p)); 

p = false; q = false; 

System.out.print(p + "\t" + q +"\t"); 
System.out.print((p&q) + "\t" + (p|q) + "\t"); 
System.out.println((p^q) + "\t" + (!p)); 
} 
} 

我在互聯網上搜索和閱讀更多的書。但我找不到一個演員版本或類似的東西。

+0

你不能投了'boolean'到' int'在Java中。相反,請使用'if-else'語句來顯示適當的值。 – 2015-04-06 08:08:55

+0

'int value =(boolean)? 1:0;' – Maroun 2015-04-06 08:09:33

+0

因此,如果我理解正確,您需要爲每個虛假值顯示0,併爲每個真實值顯示1?你爲什麼不創建一個'toInteger(boolean b)'方法,併爲你顯示的每個布爾值調用它? – 2015-04-06 08:09:36

回答

0

booleanint之間沒有辦法投射。 boolean不是Java中的數字類型。

下面的表達式可以用來代替:

boolean b = true; 
int i; 

i = (b ? 1 : 0); 

b = (i != 0); 
0

在你的代碼試試這個....

public String returnedValueFromAnd(Boolean p,Boolean q) 
{ 
    if(p&q) 
    { return "true";} 
    return "false"; 
} 
public String returnedValueFromOR(Boolean p,Boolean q) 
{ 
    if(p|q) 
    { return "true";} 
    return "false"; 
} 
public String returnedValueFromNegation(Boolean p) 
{ 
    if(!p) 
    { return "true";} 
    return "false"; 
} 
public String returnedValueFromXOR(Boolean p,Boolean q) 
{ 
    if(p^q) 
    { return "true";} 
    return "false"; 
} 

現在打電話要打印值的方法和通過參數p和q(如適用)。

+0

感謝您的回答。我沒有?但還是自己的方法的一部分,因爲我只在15的模塊2中。但是,現在我明白,鑄造是一種縮小轉換,不轉換任何不兼容的類型。我自己的思維錯誤:)所以,我只能猜測我的調整對於挑戰是合法的。這個挑戰並沒有說我必須使用演員或者如果對於這個問題的陳述。在這本書中沒有針對這個挑戰的「解決方案」,所以我從來不知道。非常感謝您的幫助! – Shibito 2015-04-07 17:56:17

0

我也試圖從同一本書中學習Java,遇到了這個問題並解決了它,如下所示。

package ChapterTwo; 

// Try this 2-2: a truth table for the logical operators. package ChapterTwo; 

public class LogicalOpTable { 

public static void main(String[] args) { 

boolean p, q; 

System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); 

p = true; q = true; 

System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t"); 
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t"); 
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p)); 

p = true; q = false; 

System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t"); 
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t"); 
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p)); 

p = false; q = true; 

System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t"); 
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t"); 
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p)); 

p = false; q = false; 

System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t"); 
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t"); 
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p)); 
} 
public static String printBoolean(boolean p) { 
    return p ? "1" : "0"; 
} 
public static String printBoolean2(boolean q) { 
    return q ? "1" : "0"; 

} 

}

1

,如果你要使用僅前兩章的東西,那麼代碼可能會是這樣的

package ChapterTwo; 
// Try this 2-2: a truth table for the logical operators. package ChapterTwo; 

public class LogicalOpTable { 
public static void main(String [] args){ 
    boolean p, q; 
    int x=0; 
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); 

    p = q = true; 
    if(p) x=1; System.out.print(x + "\t"); x=0; 
    if(q) x=1; System.out.print(x + "\t"); x=0; 
    if (p&q) x=1; System.out.print(x + "\t"); x=0; 
    if (p|q) x=1; System.out.print(x + "\t"); x=0; 
    if (p^q) x=1; System.out.print(x + "\t"); x=0; 
    if (!p) x=1; System.out.println(x + "\t"); x=0; 

    p = true; q = false; 
    if(p) x=1; System.out.print(x + "\t"); x=0; 
    if(q) x=1; System.out.print(x + "\t"); x=0; 
    if (p&q) x=1; System.out.print(x + "\t"); x=0; 
    if (p|q) x=1; System.out.print(x + "\t"); x=0; 
    if (p^q) x=1; System.out.print(x + "\t"); x=0; 
    if (!p) x=1; System.out.println(x + "\t"); x=0; 

    p = false; q = true; 
    if(p) x=1; System.out.print(x + "\t"); x=0; 
    if(q) x=1; System.out.print(x + "\t"); x=0; 
    if (p&q) x=1; System.out.print(x + "\t"); x=0; 
    if (p|q) x=1; System.out.print(x + "\t"); x=0; 
    if (p^q) x=1; System.out.print(x + "\t"); x=0; 
    if (!p) x=1; System.out.println(x + "\t"); x=0; 

    p = q = false; 
    if(p) x=1; System.out.print(x + "\t"); x=0; 
    if(q) x=1; System.out.print(x + "\t"); x=0; 
    if (p&q) x=1; System.out.print(x + "\t"); x=0; 
    if (p|q) x=1; System.out.print(x + "\t"); x=0; 
    if (p^q) x=1; System.out.print(x + "\t"); x=0; 
    if (!p) x=1; System.out.println(x + "\t"); x=0; 
} 
}