Error when running the following code:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
/**
* Determination of score levels
* Use the nesting of conditional operators to complete this question: students with an >=90 academic score are represented by A, those between 60-89 are represented by B, and those below 60 are represented by C.
* (a>b)?a:b This is a basic example of a conditional operator
*/
class ClassifyGrade
{
public static void main(String[] args)
{
System.out.println("Please enter the score for a particular student.");
Scanner aInt = new Scanner(System.in);
int score = aInt.nextInt(); // This way we have a score to experiment with
String grade = classify(score);
System.out.println(grade);
}
public static String classify(int n)
{
n>=90?(grade = "A"):(n>=60?(grade = "B"):(grade = "C"));
return grade;
}
}
Error:
Main.java:26: error: not a statement
n>=90?(grade = "A"):(n>=60?(grade = "B"):(grade = "C"));
^
1 error
The reason for the mistake is:
Java ternary operator?: different from C++
In Java, N> = 90?(grade = “A”):(n> = 60?(grade = “B”):(grade = “C”)); This is just an expression, not a statement,
There are specific requirements for expressions in JAVA. Namely: expression E;
To form an expression statement, the expression E must only be :
1) assignment expression,
2) autoincrement ++ expression,
3) autodecrement — expression,
4) method call expression,
5)new expression (object creation expression)
The following statement is easier to understand:
Conditional statement?[expression 1] : [expression 2] where expression 1 is executed if the conditional statement is true, otherwise expression 2 is executed. Expression 1 or expression 2 should have a return value, which means that expression 1 or expression 2 can be some value, such as the integer 123. In my code, grade = “B” is an assignment statement that cannot return any value.
Correct code:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
/**
* Determination of score levels
* Use the nesting of conditional operators to complete this question: students with an >=90 academic score are represented by A, those between 60-89 are represented by B, and those below 60 are represented by C.
* (a>b)?a:b This is a basic example of a conditional operator
*/
class ClassifyGrade
{
public static void main(String[] args)
{
System.out.println("Please enter the score for a particular student.");
Scanner aInt = new Scanner(System.in);
int score = aInt.nextInt(); //This way we have a score to experiment with
String grade = classify(score);
System.out.println(grade);
}
public static String classify(int n)
{
String grade = n>=90?"A":(n>=60?"B":"C");
return grade;
}
}
See the reference post:
Why can’t the ternary operator stand alone as a sentence, but a method that returns a value can stand alone as a sentence?