遇到错误,提示我的 if 语句始终为 false,我需要做什么?

huangapple go评论76阅读模式
英文:

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 &#39;if&#39; 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(&quot;Please enter your age: &quot;);
    int a = sc.nextInt();
    sc.nextLine();

    switch (a) {
        case 11 -&gt; System.out.println(&quot;You can view up to PG rated films unaccompanied&quot;);
        case 12, 13, 14 -&gt; System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
        case 15, 16, 17 -&gt; {
            System.out.println(&quot;You can view films up to a 15 age rating unaccompanied&quot;);


            if (a &gt;= 18) {
                System.out.println(&quot;You can watch any film unaccompanied&quot;);

            }
            if (a &lt; 11) {
                System.out.println(&quot;You are too young to view a film unaccompanied&quot;);
            }
        }
    }
    }


}

答案1

得分: 1

以下是提供的解决方案之一:

  1. 可以将用于检查成年年龄的 if 语句放在 switch 外面<br/>
    然后 switch 中的 default 是指未成年人的年龄范围 [0..10]
if (a &gt;= 18) {
    System.out.println("你可以单独观看任何电影");
} else {
    switch (a) {
        case 11 -&gt; System.out.println("你可以单独观看最高为 PG 级别的电影");
        case 12, 13, 14 -&gt; System.out.println("你可以单独观看最高为 12 岁级别的电影");
        case 15, 16, 17 -&gt; System.out.println("你可以单独观看最高为 15 岁级别的电影");
        default -&gt; System.out.println("你年龄太小,在 " + a + " 岁时不能单独观看电影");
    }
}
  1. default 中用三元运算符替换 if
switch (a) {
    case 11 -&gt; System.out.println("你可以单独观看最高为 PG 级别的电影");
    case 12, 13, 14 -&gt; System.out.println("你可以单独观看最高为 12 岁级别的电影");
    case 15, 16, 17 -&gt; System.out.println("你可以单独观看最高为 15 岁级别的电影");
    default -&gt; System.out.println(a &gt;= 18 ?
        "你可以单独观看任何电影" :
        "你年龄太小,在 " + a + " 岁时不能单独观看电影");
}
英文:

The following solutions can be provided:

  1. if statement to check adult age could be placed outside switch<br/>
    then default in switch is referring to minor ages `[0..10].
if (a &gt;= 18) {
	System.out.println(&quot;You can watch any film unaccompanied&quot;);
} else {
	switch (a) {
		case 11 -&gt; System.out.println(&quot;You can view up to PG rated films unaccompanied&quot;);
		case 12, 13, 14 -&gt; System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
		case 15, 16, 17 -&gt; System.out.println(&quot;You can view films up to a 15 age rating unaccompanied&quot;);
		default -&gt; System.out.println(&quot;You are too young to view a film unaccompanied at the age of &quot; + a);
	}
}
  1. replace if with a ternary operator in the default:
switch (a) {
	case 11 -&gt; System.out.println(&quot;You can view up to PG rated films unaccompanied&quot;);
	case 12, 13, 14 -&gt; System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
	case 15, 16, 17 -&gt; System.out.println(&quot;You can view films up to a 15 age rating unaccompanied&quot;);
	default -&gt; System.out.println(a &gt;= 18 ? 
		&quot;You can watch any film unaccompanied&quot; : 
		&quot;You are too young to view a film unaccompanied at the age of &quot; + 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&gt;=18) or if(a&lt;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(&quot;Please enter your age: &quot;);
    int a = sc.nextInt();
    if (a &gt;= 18) {
        System.out.println(&quot;You can watch any film unaccompanied&quot;);

   }else if (a &lt; 11) {
                System.out.println(&quot;You are too young to view a film unaccompanied&quot;);
            }
    switch (a) {
        case 11:
            System.out.println(&quot;You can view up to PG rated films unaccompanied&quot;);
            break;
        case 12:
            System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
            break;
        case 13:
            System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
            break;
        case 14:
            System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
            break;
        case 15:
            System.out.println(&quot;You can view films up to a 15 age rating unaccompanied&quot;);
            break;
        case 16:
            System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
            break;
        case 17:
            System.out.println(&quot;You can view films up to a 12 age rating unaccompanied&quot;);
            break;

    }
    }


}

Sample I/O

Input

12

Output

You can view films up to a 12 age rating unaccompanied

huangapple
  • 本文由 发表于 2020年10月1日 23:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64158726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定