15.0k views
0 votes
(a) Rewrite the following using if else statement:

int x;
String s=(x%3=0)? “Divisible by 3" : "Not divisible by 3";
System.out.println(s);​

1 Answer

6 votes

Answer:

int x;

String s;

if ((x%3) == 0) {

s = "Divisible by 3";

} else {

s = "Not divisible by 3";

}

System.out.println(s);​

Step-by-step explanation:

A : B ? C generally translates to if (A) { B } else { C }.

Note the == to compare to 0. The single = in the original expression is a mistake.

User Galois
by
4.8k points