我对Java还是很陌生,目前正在从事一项工作,其中我创建了一个数组列表来存储星期几和用户的每日温度输入。我在创建条件语句时遇到困难,如果用户输入单词“ week”,该条件语句将打印输入的天数+温度表以及平均温度。我已经能够打印所有这些信息,但这是我在努力的条件声明。
我尝试使用if-else和do-while循环创建条件,但是我继续遇到错误,或者使用下面的代码,如果输入了“ week”,则将什么也不会发生。我一直在使用“ days.equals”,但我认为这可能是我犯错的地方。
ArrayList<String> days = new ArrayList<String>();
ArrayList<Double> temp = new ArrayList<Double>();
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
days.add("Sunday");
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the temperature for Monday through
Sunday below.\n" );
for (int i = 0;i < 7; i++) { // Loop 7 times for each day of the week
System.out.print("Enter day of week: ");
days.add(scnr.next());
System.out.print("Enter temperature: ");
temp.add(scnr.nextDouble());
}
System.out.println("-----------------------------");
System.out.println("Day\t" + "\tTemperature");
System.out.println("-----------------------------");
for(int i = 0; i < 7; i++) {
System.out.println(days.get(i));
System.out.print("\t" + "\t");
System.out.println(temp.get(i));
}
double sum = 0.0;
for ( int i = 0; i < temp.size(); i++) {
sum += temp.get(i);
}
double average = (double) sum / temp.size();
do {
System.out.println("\n" + "Average temperature = " + average);
}
while(days.equals("Week"));
}
}
我希望程序在用户输入单词“ week”时输出以下内容。
-----------------------
Day Temperature
-----------------------
Monday 90.0
Tuesday 94.0
Wednesday 89.0
Thursday 84.0
Friday 82.0
Saturday 85.0
Sunday 88.0
Average temperature = 87.42857142857143
答案 0 :(得分:0)
如果我正确理解您的问题,那么您需要做的是在获得几天的温度后等待用户输入,检查该值是否为“ week”,然后仅打印您的值。 遵循以下内容应该可以。
ArrayList<String> days = new ArrayList<String>();
ArrayList<Double> temp = new ArrayList<Double>();
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
days.add("Thursday");
days.add("Friday");
days.add("Saturday");
days.add("Sunday");
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the temperature for Monday through
Sunday below.\n" );
for (int i = 0;i < 7; i++) { // Loop 7 times for each day of the week
System.out.print("Enter day of week: ");
days.add(scnr.next());
System.out.print("Enter temperature: ");
temp.add(scnr.nextDouble());
}
// You ask for user input here
System.out.print("Enter 'week' here to see temperature details: ");
String userChoice = scnr.next();
while ("week".equalsIgnoreCase(userChoice)) {
System.out.println("-----------------------------");
System.out.println("Day\t" + "\tTemperature");
System.out.println("-----------------------------");
for(int i = 0; i < 7; i++) {
System.out.println(days.get(i));
System.out.print("\t" + "\t");
System.out.println(temp.get(i));
}
double sum = 0.0;
for ( int i = 0; i < temp.size(); i++) {
sum += temp.get(i);
}
double average = (double) sum / temp.size();
System.out.println("\n" + "Average temperature = " + average);
//Ask user if he'd like to see the information again.
System.out.print("Enter 'week' here to see temperature details again: ");
userChoice = scnr.next();
}