我正在尝试创建一个对数字求平均的程序,但无法弄清楚如何将用户输入的数字加在一起

时间:2018-09-19 14:58:44

标签: java

我是一名学习Java的AP计算机科学专业的学生。在上这门课程之前,我学习过JavaScript,但是在弄清楚如何完成此代码时遇到了麻烦。我的老师正在休假,我们的分校不是程序员。感谢您的帮助。我的老师希望我们发表评论以解释一切。 这是我的代码:

package com.company;

//导入扫描器类和所需的任何其他可导入类

导入java.util。*;

公共类主要{

public static void main(String[] args) {
    //Asks a question and lets the user input an answer, saved as the int Dividend
    System.out.println("How many numbers do you want to average?");
    Scanner dividend = new Scanner(System.in);
    int Dividend = dividend.nextInt();

    //this is a variable set in order to make the while statement work for any number
    int change = 0;

    //Asks for the numbers that should be averaged
    System.out.println("Enter the numbers to find their average: ");

    /*This is saying that while change is less than the amount of numbers being averaged,
      continue to provide an input for numbers*/
    while (change <= Dividend) {
        int Num = 0;
        int nums = 0;
        Scanner num = new Scanner(System.in);
        //I am trying to add the input to num so I can average them
        nums = num.nextInt();
        Num += nums;
        /*this is making sure that change is letting the code run through the exact amount of necessary times
          in order to input the correct amount of numbers*/
        change++;

        //once all of the numbers have been put in, average them
        if (change == Dividend) {
            System.out.println("Average: " + Num / Dividend);
        }
        System.out.println(Num);
    }


}

}

2 个答案:

答案 0 :(得分:0)

我修复了您的问题,因此它现在将所有数字相加,但仍不确定我添加了nums = nums + num.nextInt();的那一半,因此它将总计所有数字

如果您想在自己的下方找到工作参考,我也创建了解决方案

public static void main(String[] args){
    int change = 0;//Total Numbers It think
    int nums = 0;//Sum

    System.out.println("Enter the numbers to find their average: ");
    Scanner num = new Scanner(System.in);//Should be moved out of loop so its not initialized every time

    int Dividend=1;//I had to declare to run it
    while (change <= Dividend) {//not sure why your comparing these

        nums = nums + num.nextInt();//Total Sum
        change++;//Total nums

        if (change == Dividend) {
            System.out.println("Average: " + nums / Dividend);
        }
        System.out.println(nums);
    }
}

这就是我要做的

public static void main(String[] args){
    int sum = 0;
    int countOfNumbers = 0;//Tthe same as your change im guessing

    System.out.println("Enter the numbers to find their average type stop to quit: ");//Added stop statement
    Scanner scanner = new Scanner(System.in);//Should be moved out of loop so its not initialized everytime

    while (scanner.hasNext()){//Ensures there is something to check
        String next = scanner.next(); //Pull next Value
        if(next.equalsIgnoreCase("Stop"))//This will prevent looping forever
            break;
        sum = sum + Integer.valueOf(next); //Get total sum
        countOfNumbers++; //Increment total numbers
        System.out.println("The Sum is:"+sum
                +"\nThe count of Numbers averaged is:"+countOfNumbers
                +"\nThe Average is:"+sum/countOfNumbers);//Print info 
    }
    System.out.println("Done Averaging Goodbye");
}

答案 1 :(得分:-1)

您可以使用ArrayList来存储所有用户输入,然后对其进行迭代以求平均。