具有从 Scanner 存储的数字的字符串的平均值

Average of a string which has numbers stored from Scanner

我是编码新手,任务是从使用扫描仪的用户那里获取三个月的平均降雨量(11 月、12 月、1 月),然后计算三个月降雨量的平均值。

我显然做错了什么,因为我将平均值初始化为字符串,但不知道如何初始化变量?基本上我不知道如何将用户输入的降雨量结果转换为整数,如果有人可以帮助我使用我的代码以及我如何正确地计算三个月的平均值,那就太好了,谢谢!

import java.util.Scanner;
public class rainfall {

  public static void main (String[] args) {
    Scanner stdin = new Scanner (System.in);

    String November, December, January;

    String average;

    System.out.println("what is the rainfall for November?");
    November = stdin.nextLine();

    System.out.println("what is the rainfall for December?");
    December = stdin.nextLine();

    System.out.println("what is the rainfall for January?");
    January = stdin.nextLine();

    System.out.println("the rainfall for November is: \t "  + November );
    System.out.println("the rainfall for December is: \t "  + December );
    System.out.println("the rainfall for January is: \t "  + January );

    average = November + December + January/3;
    System.out.println("The average is " + average );
  }
}

试试这个可以解决你的问题

 public static void main (String[] args) {
        Scanner stdin = new Scanner (System.in);

        double November, December, January;

        double average;

        System.out.println("what is the rainfall for November?");
        November = stdin.nextDouble();

        System.out.println("what is the rainfall for December?");
        December = stdin.nextDouble();

        System.out.println("what is the rainfall for January?");
        January = stdin.nextDouble();

        System.out.println("the rainfall for November is: \t "  + November );
        System.out.println("the rainfall for December is: \t "  + December );
        System.out.println("the rainfall for January is: \t "  + January );

        average = (November + December + January)/3;
        System.out.println("The average is " + average );
      }