Ниже приведен код, демонстрирующий вложенный оператор 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.");
}
}
}
Как изменить этот вложенный оператор if на один оператор if с логическим оператором && (И) и добиться того же результата?
Это то, что я попробовал. Я создал один оператор if, объединив два условия и сделав их одним условием (оценка >= 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 ... -a-logical
Замена вложенного оператора if одним оператором if с логическим оператором && (И) для достижения того же результата. ⇐ JAVA
Программисты JAVA общаются здесь
1726938485
Anonymous
Ниже приведен код, демонстрирующий вложенный оператор 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.");
}
}
}
Как изменить этот вложенный оператор if на один оператор if с логическим оператором && (И) и добиться того же результата?
Это то, что я попробовал. Я создал один оператор if, объединив два условия и сделав их одним условием (оценка >= 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.");
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79009407/replacing-a-nested-if-statement-with-a-single-if-statement-that-has-a-logical[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия