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

我是 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.0


Tuesday     94.0


Wednesday   89.0


Thursday    84.0


Friday      82.0


Saturday    85.0


Sunday      88.0


Average temperature = 87.42857142857143


猛跑小猪
浏览 99回答 1
1回答

慕姐8265434

如果我正确理解您的问题,那么您需要做的是在获得几天的温度后等待用户输入,检查该值是否为“周”,然后才打印您的值。以下内容应该有效。ArrayList<String> days = new ArrayList<String>();&nbsp;ArrayList<Double> temp = new ArrayList<Double>();&nbsp;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&nbsp;&nbsp; Sunday below.\n" );for (int i = 0;i < 7; i++) { // Loop 7 times for each day of the week&nbsp; &nbsp;System.out.print("Enter day of week: ");&nbsp; &nbsp;days.add(scnr.next());&nbsp; &nbsp;System.out.print("Enter temperature: ");&nbsp; &nbsp;temp.add(scnr.nextDouble());}// You ask for user input hereSystem.out.print("Enter 'week' here to see temperature details: ");String userChoice = scnr.next();while ("week".equalsIgnoreCase(userChoice)) {&nbsp; System.out.println("-----------------------------");&nbsp; System.out.println("Day\t" + "\tTemperature");&nbsp; System.out.println("-----------------------------");&nbsp; for(int i = 0; i < 7; i++) {&nbsp; &nbsp; &nbsp;System.out.println(days.get(i));&nbsp; &nbsp; &nbsp;System.out.print("\t" + "\t");&nbsp; &nbsp; &nbsp;System.out.println(temp.get(i));&nbsp; }&nbsp; double sum = 0.0;&nbsp; for ( int i = 0; i < temp.size(); i++) {&nbsp; &nbsp; &nbsp; sum += temp.get(i);&nbsp; }&nbsp; double average = (double) sum / temp.size();&nbsp; System.out.println("\n" + "Average temperature = " + average);&nbsp; //Ask user if he'd like to see the information again.&nbsp; System.out.print("Enter 'week' here to see temperature details again: ");&nbsp; userChoice = scnr.next();}&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java