Ниже приведен код, демонстрирующий вложенный оператор IF для оценок за экзамен:
public class NestIfExample {
Код: Выделить всё
public static void main(String[] args) {
Scanner inputDevice = new Scanner(System.in);
// Asking the user to input their score
System.out.print("Enter your score: ");
int score = inputDevice.nextInt();
// Outer if statement
if (score >= 50) {
System.out.println("You passed the exam!");
// Inner if statement - only checked if the score is 50 or more
if (score >= 80) {
System.out.println("Great job! You scored in the top range.");
} else {
System.out.println("Good effort, but there's room for improvement.");
}
} else {
System.out.println("Unfortunately, you did not pass. Try again next time.");
}
}
Это то, что я попробовал. Я создал ОДИН ЗАЯВЛЕНИЕ ЕСЛИ, объединив два условия и превратив их в одно условие (оценка >= 80 и& оценка >=50).
Я не уверен, правильно ли это. Пожалуйста, дайте мне знать.
public static void main(String[] args) {
Scanner inputDevice = new Scanner(System.in);
Код: Выделить всё
// Asking the user to input their score
System.out.print("Enter your score: ");
int score = inputDevice.nextInt();
// Single if statement using logical AND (&&) operator
if (score >= 80 && score >= 50) {
System.out.println("You passed the exam!");
System.out.println("Great job! You scored in the top range.");
} else if (score >= 50) {
System.out.println("You passed the exam!");
} else {
System.out.println("Unfortunately, you did not pass. Try again next time.");
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... -logical-a