取用户输入数量的平均值 [英] Taking average of user input number

查看:109
本文介绍了取用户输入数量的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

  import java.util。*; 
import java.io. *;

公共类eCheck10A
{
public static void main(String [] args)
{
PrintStream out = System.out;
Scanner in = new Scanner(System.in);

out.print(输入你的整数);
out.println(Negative = sentinel);

列表<整数> aList = new ArrayList< Integer>(); (b n1 = in.nextInt(); n1> 0; n1 = in.nextInt())
{
if (n1< 0)
{
break;
}
}


}
}

如果我想获取我为n1输入的所有数字,并将它们平均,我如何参考所有这些数字?我将把它们放在IF语句中,所以如果输入一个负数,程序就会停止并发布它们的平均值。

解决方案

这是您需要执行此任务的伪代码(伪代码,因为它看起来很像家庭作业/课堂作业,如果您自己坚持实施,您将成为更好的开发人员。)



因为您不需要数字本身来计算平均值,所以存储它们没有意义。平均值定义为所有数字除以其计数的总和,因此您需要记住这一切。这样的事情就足够了:

 总计= 0 
count = 0
n1 = get_next_number()
而n1> = 0:
total = total + n1
count = count + 1
n1 = get_next_number()
if count == 0:
打印没有输入数字。
else:
打印平均值是,(总计/计数)






我要提到的其他几点。现在,你的声明将退出第一个非正数(< = 0 ),使如果多余。



此外,您可能想要任何零都包含在平均值中: {1,2,3}的平均值= 2 {1,2,3,0,0,0} = 1 的平均值不同。



您可以在中为语句执行此操作,例如:

  for(int n1 = in.nextInt(); n1> = 0; n1 = in.nextInt())

然后你根本不需要循环内的 if / break 位,类似于我提供的伪代码。 / p>

This is my code

import java.util.*;
import java.io.*;

public class eCheck10A
{
public static void main(String[] args)
{
  PrintStream out = System.out;
  Scanner in = new Scanner(System.in);

  out.print("Enter your integers");
  out.println("Negative = sentinel");

  List<Integer> aList = new ArrayList<Integer>();



  for (int n1 = in.nextInt(); n1 > 0; n1 = in.nextInt())
  {
          if(n1 < 0)
          {
          break;
          }
  }


 }
}

if i want to take all the numbers that I enter for n1, and average them out, how do i refer to all these numbers? I am going to put them in the IF statement, so if a negative number is entered, the program stops and posts their average.

解决方案

This is the pseudocode you need to do this task (pseudo-code since it looks suspiciously like homework/classwork and you'll become a better developer if you nut out the implementation yourself).

Because you don't need the numbers themselves to work out the average, there's no point in storing them. The average is defined as the sum of all numbers divided by their count, so that's all you need to remember. Something like this should suffice:

total = 0
count = 0
n1 = get_next_number()
while n1 >= 0:
    total = total + n1
    count = count + 1
    n1 = get_next_number()
if count == 0:
    print "No numbers were entered.
else:
    print "Average is ", (total / count)


A couple of other points I'll mention. As it stands now, your for statement will exit at the first non-positive number (<= 0), making the if superfluous.

In addition, you probably want any zeros to be included in the average: the average of {1,2,3} = 2 is not the same as the average of {1,2,3,0,0,0} = 1.

You can do this in the for statement itself with something like:

for (int n1 = in.nextInt(); n1 >= 0; n1 = in.nextInt())

and then you don't need the if/break bit inside the loop at all, similar to my provided pseudo-code.

这篇关于取用户输入数量的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆