英文:
Getting error stating that my if statement is always false, what do i need to do?
问题
以下是翻译好的部分:
package im.ucm.co4025;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的年龄:");
int a = sc.nextInt();
sc.nextLine();
switch (a) {
case 11 -> System.out.println("您可以单独观看PG级别的电影");
case 12, 13, 14 -> System.out.println("您可以单独观看12岁及以下级别的电影");
case 15, 16, 17 -> {
System.out.println("您可以单独观看15岁及以下级别的电影");
if (a >= 18) {
System.out.println("您可以观看任何电影");
}
if (a < 11) {
System.out.println("您年龄太小,不能单独观看电影");
}
}
}
}
}
英文:
So i have been given a task for my degree level programming in Java, the task can be seen below:
My problem is getting errors stating:
-
Condition 'a >= 18' is always 'false'
Condition 'a < 11' is always 'false'
Task
1)In a new command line project, write code which will prompt the
user for the age of a young person (between the age of 11 and 18,
and determine the highest category of film they can view at the
cinema unaccompanied. The categories are PG (any age), 12, 15 and 18
2)Add a default clause stating that they are too young to see a film
unaccompanied.
3)Add an 'if' statement to check if the age entered is over 18. If
so, print a message stating that they can see any film they like,
but if the value is under 18, ensure the switch statement is
executed
My code can be seen below:
package im.ucm.co4025;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your age: ");
int a = sc.nextInt();
sc.nextLine();
switch (a) {
case 11 -> System.out.println("You can view up to PG rated films unaccompanied");
case 12, 13, 14 -> System.out.println("You can view films up to a 12 age rating unaccompanied");
case 15, 16, 17 -> {
System.out.println("You can view films up to a 15 age rating unaccompanied");
if (a >= 18) {
System.out.println("You can watch any film unaccompanied");
}
if (a < 11) {
System.out.println("You are too young to view a film unaccompanied");
}
}
}
}
}
答案1
得分: 1
以下是提供的解决方案之一:
- 可以将用于检查成年年龄的
if
语句放在switch
外面<br/>
然后switch
中的default
是指未成年人的年龄范围[0..10]
。
if (a >= 18) {
System.out.println("你可以单独观看任何电影");
} else {
switch (a) {
case 11 -> System.out.println("你可以单独观看最高为 PG 级别的电影");
case 12, 13, 14 -> System.out.println("你可以单独观看最高为 12 岁级别的电影");
case 15, 16, 17 -> System.out.println("你可以单独观看最高为 15 岁级别的电影");
default -> System.out.println("你年龄太小,在 " + a + " 岁时不能单独观看电影");
}
}
- 在
default
中用三元运算符替换if
:
switch (a) {
case 11 -> System.out.println("你可以单独观看最高为 PG 级别的电影");
case 12, 13, 14 -> System.out.println("你可以单独观看最高为 12 岁级别的电影");
case 15, 16, 17 -> System.out.println("你可以单独观看最高为 15 岁级别的电影");
default -> System.out.println(a >= 18 ?
"你可以单独观看任何电影" :
"你年龄太小,在 " + a + " 岁时不能单独观看电影");
}
英文:
The following solutions can be provided:
if
statement to check adult age could be placed outsideswitch
<br/>
thendefault
inswitch
is referring to minor ages `[0..10].
if (a >= 18) {
System.out.println("You can watch any film unaccompanied");
} else {
switch (a) {
case 11 -> System.out.println("You can view up to PG rated films unaccompanied");
case 12, 13, 14 -> System.out.println("You can view films up to a 12 age rating unaccompanied");
case 15, 16, 17 -> System.out.println("You can view films up to a 15 age rating unaccompanied");
default -> System.out.println("You are too young to view a film unaccompanied at the age of " + a);
}
}
- replace
if
with a ternary operator in thedefault
:
switch (a) {
case 11 -> System.out.println("You can view up to PG rated films unaccompanied");
case 12, 13, 14 -> System.out.println("You can view films up to a 12 age rating unaccompanied");
case 15, 16, 17 -> System.out.println("You can view films up to a 15 age rating unaccompanied");
default -> System.out.println(a >= 18 ?
"You can watch any film unaccompanied" :
"You are too young to view a film unaccompanied at the age of " + a);
}
答案2
得分: 0
因为if语句位于15、16、17的情况中,所以你会收到这个消息。因此,只有在运行if(a>=18)
或者if(a<11)
的情况下,a
可能的取值只能是15、16或17。
英文:
The reason you're getting this is because the if statements are in the case for 15,16,17.
Thus the only situation where if(a>=18)
or if(a<11)
is ran the only possible things a
could be is 15, 16, or 17.
答案3
得分: 0
你的代码中存在一些错误,你必须修复它们。
在 switch 中不能有 if 语句,最好将它们移到外面。
以下是正确的示例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your age: ");
int a = sc.nextInt();
if (a >= 18) {
System.out.println("You can watch any film unaccompanied");
} else if (a < 11) {
System.out.println("You are too young to view a film unaccompanied");
}
switch (a) {
case 11:
System.out.println("You can view up to PG rated films unaccompanied");
break;
case 12:
System.out.println("You can view films up to a 12 age rating unaccompanied");
break;
case 13:
case 14:
case 16:
case 17:
System.out.println("You can view films up to a 12 age rating unaccompanied");
break;
case 15:
System.out.println("You can view films up to a 15 age rating unaccompanied");
break;
}
}
}
示例输入/输出
输入
12
输出
You can view films up to a 12 age rating unaccompanied
英文:
You have some errors in your code which you must fix.
You can't have if statements in a switch. Better move them outside.
Here's what the working example should be:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your age: ");
int a = sc.nextInt();
if (a >= 18) {
System.out.println("You can watch any film unaccompanied");
}else if (a < 11) {
System.out.println("You are too young to view a film unaccompanied");
}
switch (a) {
case 11:
System.out.println("You can view up to PG rated films unaccompanied");
break;
case 12:
System.out.println("You can view films up to a 12 age rating unaccompanied");
break;
case 13:
System.out.println("You can view films up to a 12 age rating unaccompanied");
break;
case 14:
System.out.println("You can view films up to a 12 age rating unaccompanied");
break;
case 15:
System.out.println("You can view films up to a 15 age rating unaccompanied");
break;
case 16:
System.out.println("You can view films up to a 12 age rating unaccompanied");
break;
case 17:
System.out.println("You can view films up to a 12 age rating unaccompanied");
break;
}
}
}
Sample I/O
Input
12
Output
You can view films up to a 12 age rating unaccompanied
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论