【问题标题】:I am trying to create a program that averages numbers, but cannot figure out how to add the user inputted numbers together我正在尝试创建一个平均数字的程序,但无法弄清楚如何将用户输入的数字加在一起
【发布时间】:2018-09-19 14:58:43
【问题描述】:

我是一名学习 Java 的 AP 计算机科学专业的学生。在参加本课程之前,我学习了 JavaScript,但我无法弄清楚如何完成这段代码。我的老师在休假,我们的孩子不是程序员。我很感激任何帮助。我的老师希望我们让 cmets 解释一切。 这是我的代码:

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


}

}

【问题讨论】:

  • 你能解释一下什么是变化吗?
  • 还有什么股息是它没有宣布
  • 对不起,我的一些代码没有复制,这里是完整的代码:

标签: java


【解决方案1】:

我修复了你的,所以它添加了所有数字现在仍然不确定我添加了一半 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");
}

【讨论】:

  • 我回顾了我的代码并使用您的参考作为指导找出了问题,每次循环运行时我都在初始化扫描仪,并且每次都将变量 N​​um 重置为 0时间。我不知道为什么我包含 nums 但无论如何,非常感谢您的帮助。
  • 没有问题了,请告诉我,但如果您可以通过单击问题旁边的检查来接受答案,以便其他人不认为这个问题仍然存在
  • 另外看看java命名约定这里是一个参考geeksforgeeks.org/java-naming-conventions我注意到一些大写的变量
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2016-09-09
    • 2020-09-18
    • 2023-02-14
    • 2017-05-17
    相关资源
    最近更新 更多