1

我得到了一项家庭作业,无论我多么努力(搜索谷歌、阅读教科书等),我都无法弄清楚如何正确嵌套这些语句。

Fair Grounds Ride Company 一直遇到不符合身高要求的人乘坐他们不应该乘坐的游乐设施的问题。他们需要你编写一个程序来帮助解决这个问题。进入公园后,每位顾客都会得到一条特定颜色的腕带,以指示他们可以继续哪些游乐设施。

如果客户身高不超过 36 英寸,他们将收到一条红色带子,并且只允许乘坐像 Turtle's Slowing Spinning Shell 这样的慢速游乐设施。

如果他们的身高在 36 到 54 英寸之间,他们将获得一条黄色带子,并被允许乘坐像 Rabbit's Bouncy Easter Egg 这样的中等速度的游乐设施。

如果他们的身高为 54 到 80 英寸,他们将获得一条绿色带子,并被允许参加激动人心的游乐设施,例如兴登堡:为你的生活而跳跃。

如果他们的身高超过 80 英寸,他们将不会收到任何乐队,因为他们太高了,不能骑任何东西而不撞到什么东西,所以他们可以在工业大厅里闲逛,这对每个人来说都是非常安全的。

导入 java.util.Scanner;

公共课练习4_K {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    boolean moreRiders = true;
    String bandColor = "";

    while (moreRiders) {
        System.out.println("Please enter height in inches (or 0 to exit)");
        int height = scanner.nextInt();

        if (height == 0) {
            System.out.println("bye bye");
            moreRiders = false;
        } else {
            if (height > 80) {
                bandColor = "Sorry, too tall";
            } else if (height <= 36) {
                bandColor = "red";
            } else if (height <= 80) {
                bandColor = "yellow";
            } else if (height >= 54){
                bandColor = "green";
            System.out.println("")

                }

            }
        }
    }

}

}

编写一个程序,接受由公平入口看门人输入的整数英寸,然后按照上述规则打印出适当的带颜色或“无带”。

4

1 回答 1

0

您只是将颜色保存在中,bandColor但从不使用它System.out.println。而且您必须注意编写 if-else-conditions 的顺序。

这应该有效:

while (moreRiders) {
        System.out.println("Please enter height in inches (or 0 to exit)");
        int height = scanner.nextInt();

        if (height > 80) {
            message = "Sorry, too tall";
        }
        else if (height >= 54) {
            message = "green";
        }
        else if (height > 36) {
            message = "yellow";
        }
        else if (height >= 1) {
            message = "red";
        }
        else if (height == 0) {
            message = "bye bye";
            moreRiders = false;
        }
        System.out.println(message);

        }

我也因为“再见”把你bandColor改成了message

于 2019-08-18T03:45:22.057 回答