문제

I'm still learning Java....

My task is to write a program which divides two doubles, but before it displays result of dividing, it has to say if they are dividible or not (without a rest). I HAVE TO use ternary operator.

code:

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));


    }



}

Compiler says for the line "String result = (z>0) ?....." that it requires boolean, and that it found int. Of course, compilation failed.

도움이 되었습니까?

해결책

You've missed the last closing bracket ()) of this statement.

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

It should be:

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

All the other stuff compiles for me.

다른 팁

My compiler says that you dont have last parenthesis ) in this line:

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

Try this:

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

It compiles for me.

The problem comes from the syntax error, due you have forgotten the last closing bracket () of the last system.out line.

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

Should be:

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

If you solve this, then there is no problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top