Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top