为了账号安全,请及时绑定邮箱和手机立即绑定

特定用户输入的数组列表和条件语句

特定用户输入的数组列表和条件语句

猛跑小猪 2023-03-23 16:55:15
我是 Java 的新手,我目前正在做一项作业,我在其中创建一个数组列表来存储用户输入的星期几和每日温度。我在创建一个条件语句时遇到困难,该条件语句将打印输入的天数+温度表,以及用户输入单词“周”时的平均温度。我已经能够打印所有这些信息,但这是我正在努力处理的条件语句。我已经尝试使用 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"));  }    }我希望程序在用户输入单词“周”时输出以下内容。-----------------------Day         Temperature-----------------------Monday      90.0Tuesday     94.0Wednesday   89.0Thursday    84.0Friday      82.0Saturday    85.0Sunday      88.0Average temperature = 87.42857142857143
查看完整描述

1 回答

?
慕姐8265434

TA贡献1813条经验 获得超2个赞

如果我正确理解您的问题,那么您需要做的是在获得几天的温度后等待用户输入,检查该值是否为“周”,然后才打印您的值。以下内容应该有效。


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();

}  


查看完整回答
反对 回复 2023-03-23
  • 1 回答
  • 0 关注
  • 98 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信