2013-12-19 37 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)); 

如果解决这个问题,那么就没有问题。