好的,所以我已经为这个Java类中的最终项目构建了此代码。给出的说明如下:
选项2:监控系统 作为动物园管理员,重要的是要了解您所照看的动物的活动并监视它们的生活环境。创建一个执行以下所有操作的监视系统:
向用户询问是否要监视动物,监视栖息地或退出 显示从动物或栖息地文件中读取的动物/栖息地选项列表(基于先前的选择)
要求用户输入选项之一
通过在文件中找到适当的部分来显示监视信息
按类别和选择(例如“动物-狮子”或“栖息地-企鹅”)分隔各个部分
如果监视器检测到超出正常范围的内容,则使用对话框警告动物园管理员(这些将在文件中用新行表示 从...开始 *****。不要在对话框中显示星号。)
允许用户返回到原始选项
我开发的代码如下:
import java.util.Scanner;
public class Final {
static final Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
System.out.println("Menu:");
System.out.println("Monitor Animal");
System.out.println("Monitor Habitat");
System.out.println("Exit");
Scanner input1 = new Scanner(System.in);
String userInput1 = input1.nextLine();
if (userInput1.equals("Monitor Animal")); {
System.out.println("Details on lions");
System.out.println("Details on tigars");
System.out.println("Details on bears");
System.out.println("Details on giraffes");
System.out.println("Exit");
Scanner input2 = new Scanner(System.in);
String userInput2 = input2.nextLine();
switch(userInput2) {
case 1: userInput2 = "Details on lions";
System.out.println("Animal - Lion");
System.out.println("Name: Leo");
System.out.println("Age: 5");
System.out.println("Health concerns: Cut on left front paw");
System.out.println("Feeding schedule: Twice daily");
break;
case 2: userInput2 = "Details on tigers";
System.out.println("Animal - Tigar");
System.out.println("Name: Maj");
System.out.println("Health concerns: None");
System.out.println("Feeding schedule: 3x daily");
break;
case 3: userInput2 = "Details on bears";
System.out.println("Animal - Bear");
System.out.println("Name: Baloo");
System.out.println("Age: 1");
System.out.println("Health concerns: None");
System.out.println("Feeding schedule: None on record");
break;
case 4: userInput2 = "Details on giraffes";
System.out.println("Animal - Giraffe");
System.out.println("Name: Spots");
System.out.println("Age: 12");
System.out.println("Health concerns: None");
System.out.println("Feeding schedule: Grazing");
break;
case 5: userInput2 = "Exit";
break;
default: userInput2 = System.out.println("Error: Invalid Animal");
break;
}
if (userInput1.equals("Monitor Habitat")); {
System.out.println("Details on penguin habitat");
System.out.println("Details on bird house");
System.out.println("Details on aquarium");
System.out.println("Exit");
Scanner input3 = new Scanner(System.in);
String userInput3 = input3.nextLine();
switch(userInput3) {
case 1: userInput3 = "Details on penguin habitat";
System.out.println("Habitat - Penguin");
System.out.println("Temperature: Freezing");
System.out.println("Food source: Fish in water running low");
System.out.println("Cleanliness: Passed");
break;
case 2: userInput3 = "Details on bird house";
System.out.println("Habitat - Bird");
System.out.println("Temperature: Moderate");
System.out.println("Food source: Natural from environment");
System.out.println("Cleanliness: Passed");
break;
case 3: userInput3 = "Details on aquarium";
System.out.println("Habitat - Aquarium");
System.out.println("Temperature: Varies with output temperature");
System.out.println("Food source: Added daily");
System.out.println("Cleanliness: Needs cleaning from algae");
break;
case 4: userInput3 = "Exit";
break;
default: System.out.println("Error: Invalid Habitat");
break;
}
if (userInput1.equals("Exit")); {
System.out.println("Goodbye!");
}
}
}
它无法编译,我无法弄清楚我在做什么错。有一个更好的方法吗?我已经尝试了一天半,现在大约需要3个小时。任何帮助将不胜感激!
答案 0 :(得分:0)
您正在执行的 if-else 语句的格式是错误的。表达式后不提供分号。格式为
if(expression){
//statement
}
答案 1 :(得分:0)
在switch语句中,您正在将int与字符串进行比较。因此,您应该将字符串与字符串进行比较。例如,在您的代码中:
Scanner input2 = new Scanner(System.in);
String userInput2 = input2.nextLine();
switch(userInput2) {
case "Details on lions":
System.out.println("Animal - Lion");
System.out.println("Name: Leo");
System.out.println("Age: 5");
System.out.println("Health concerns: Cut on left front paw");
System.out.println("Feeding schedule: Twice daily");
break;
答案 2 :(得分:0)
if上没有分号(示例);
只需输入
if(){
} // simple as that.
您的开关盒也有问题。需要成为
case 'Details on penguin habitat':
break;
case 'Details on bird house':
break