练习 4-K 程序骑行准入 Java 作业



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

公平场地骑行公司一直遇到不符合身高要求的人乘坐他们不应该乘坐的游乐设施的问题。他们需要你编写一个程序来帮助解决这个问题。进入公园后,每位顾客都会得到一个某种颜色的腕带,以指示他们可以乘坐哪些游乐设施。

如果客户身高不超过 36 英寸,他们将收到一条红色带子,并且只允许乘坐像海龟的减速旋转贝壳这样的慢速游乐设施。

如果他们在 36 到 54 英寸之间,他们将收到一个黄色带子,并被允许骑中等速度的游乐设施,如兔子的弹力复活节彩蛋。

如果他们身高 54 到 80 英寸,他们将获得一个绿色带,并被允许乘坐令人兴奋的游乐设施,如兴登堡:为你的生活跳跃。

如果他们身高超过 80 英寸,他们将不会收到任何乐队,因为他们太高了,无法在不撞到东西的情况下骑任何东西,这样他们就可以在工业大厅里闲逛,这对每个人都是完全安全的。

import java.util.Scanner;

公共类 Exercise4_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("")
}
}
}
}
}

}

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

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

这应该有效:

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

最新更新