Java 我试图创建一个程序,平均数字,但无法计算出如何添加用户输入的数字在一起
我是AP计算机科学专业的学生,学习Java。在参加本课程之前,我学习了JavaScript,但我在理解如何完成这段代码时遇到了困难。我的老师在休假,我们的下属不是程序员。谢谢你的帮助。我的老师要我们发表评论来解释一切。 这是我的密码:Java 我试图创建一个程序,平均数字,但无法计算出如何添加用户输入的数字在一起,java,Java,我是AP计算机科学专业的学生,学习Java。在参加本课程之前,我学习了JavaScript,但我在理解如何完成这段代码时遇到了困难。我的老师在休假,我们的下属不是程序员。谢谢你的帮助。我的老师要我们发表评论来解释一切。 这是我的密码: package com.company; //导入所需的scanner类和任何其他可导入类 导入java.util.* 公共班机{ public static void main(String[] args) { //Asks a question an
package com.company;
//导入所需的scanner类和任何其他可导入类
导入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);
}
}
publicstaticvoidmain(字符串[]args){
//询问一个问题并允许用户输入一个答案,另存为整数
System.out.println(“您希望平均多少个数字?”);
扫描仪红利=新扫描仪(System.in);
int divident=divident.nextInt();
//这是一个变量集,用于使while语句适用于任何数字
int change=0;
//询问应该平均的数字
System.out.println(“输入数字以找到它们的平均值:”);
/*这意味着,虽然变化小于平均数,
继续为数字提供输入*/
虽然(更改您可以使用ArrayList
存储所有用户输入,然后对其进行迭代以求平均值。我修复了您的问题,因此它现在添加了所有数字,但仍然不确定我添加了多少部分nums=nums+num.nextInt();
因此它将合计所有num
如果您希望在下方的底部有一个工作参考,我也创建了我的解决方案
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);
}
}
你能解释一下什么是变化吗?还有什么红利是它没有公布的对不起,我的一些代码没有复制,下面是完整的代码:我回顾了我的代码,并以你的引用为指导解决了问题,我每次循环运行时都像你说的那样初始化扫描仪,每次都将变量Num重置为0。我不知道为什么我会包含NUM,但是无论如何,非常感谢您的帮助。没有问题了问题请告诉我,但是如果您可以通过单击问题旁边的复选框接受答案,这样其他人就不会认为这个问题仍然是开放的。还可以查看java命名约定这里是一个参考,我注意到一些大写的变量s
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");
}