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?
Read More:
- [Solved] JAVA Operate Database Error: You have an error in your SQL syntax; Dao layer SQL statement error
- org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying XXX
- [How to Solve] Invalid bound statement (not found)
- [Solved] Mybatis Batch Modify Error: multi-statement not allow
- Mybatis Error: Invalid bound statement (not found)
- [Solved] Mybatis-Plus Error: Invalid bound statement (not found)
- The precision of decimal calculation with double and float in Java is not accurate
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- HQL statement query error: could not resolve property [Solved]
- How to Solve Mybatis error: invalid bound statement (not found)
- Springboot mybatis Integrate Error: Invalid bound statement (not found): com…DepartmentMapper.save
- How to Solve Error: Invalid bound statement (not found)
- Error: unreachable statement [How to Solve]
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- There was an unexpected error (type=Internal Server Error, status=500).Invalid bound statement (not
- [Solved] IDEA springboot Startup Error: java.lang.UnsatisfiedLinkError: no tcnative-1 in java.library.path
- Mongodb java version 3.X, prompt “XXX (an encryption algorithm) is not available” when there is a user name and password
- [Solved] java: Internal error in the mapping processor: java.lang.NullPointerException
- java: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x590