2013-12-19 36 views
1

我還在學習Java ....必需的布爾值,找到int?

我的任務是寫一個程序,它分爲兩個雙打,但它顯示的劃分結果之前,它必須說,如果他們dividible或不(無休息)。我必須使用三元運算符。

代碼:

public class exercise { 

    public static void main(String[] args){ 
     double x = 8.4; 
     double y = 4.2; 
     double z = (int)(x % y); 
     String result = (z>0) ? "not dividible" : "dividible"; 
     System.out.println("This operation is: " + result); 
     System.out.println("The result of dividing is: " + (x/y)); 


    } 



} 

編譯說爲線 「字符串結果=(Z> 0).....?」,它需要布爾值,並且它發現INT。當然,編譯失敗了。

+5

該行確實*不*生成該錯誤。代碼*確實*至少有一個其他錯誤(並會導致*不同*錯誤消息),這讓我懷疑它不是*實際*代碼。 – user2864740

+3

代碼工作正常,除了你錯過了最後一條sysout語句中的括號 –

+0

我添加了缺失括號,謝謝你的提示。但它仍然不能編譯......它指向我(z> 0),說';'預期錯誤。 – jutreni

回答

6

您錯過了本聲明的最後一個右括號())。

System.out.println("The result of dividing is: " + (x/y); 

它應該是:

System.out.println("The result of dividing is: " + (x/y)); 
                 ↑ 

所有其他的東西編譯我。

+0

謝謝,@Maroun :) –

+2

我愛上了那個↑。我隨意編輯帖子,並將其放在那裏,希望它有道理。這一次它工作。 – Maroun

+1

雖然這是* an *錯誤,但它與報告的錯誤無關。 – user2864740

2

我的編譯器說,你不要有最後括號)在這一行:

System.out.println("The result of dividing is: " + (x/y); 

試試這個:

System.out.println("The result of dividing is: " + (x/y)); 

它編譯我。

1

由於您忘記了最後一個system.out行的最後一個右括號(),所以問題來自語法錯誤。

System.out.println("The result of dividing is: " + (x/y); 

應該是:

System.out.println("The result of dividing is: " + (x/y)); 

如果解決這個問題,那麼就沒有問題。