Java数组:查找比平均值小的数字数量

huangapple 未分类评论53阅读模式
英文:

Java Array: Finding how many numbers are less then the Mean

问题

  1. public static void main(String[] args) {
  2. Scanner in = new Scanner(System.in);
  3. System.out.println("How many integers should we analyze?");
  4. int num;
  5. num = in.nextInt();
  6. while (num <= 2) {
  7. System.out.println("Please reenter, integer must be greater than 1");
  8. num = in.nextInt();
  9. }
  10. int[] arr = new int[num];
  11. System.out.println("Please enter the " + num + " integers:");
  12. for (int i = 0; i < arr.length; i++) {
  13. arr[i] = in.nextInt();
  14. }
  15. System.out.print("Number of integers input: " + num);
  16. System.out.println();
  17. double total = 0;
  18. for (int element : arr) {
  19. total += element;
  20. }
  21. System.out.print("Total: " + (int) total);
  22. System.out.println();
  23. double mean = 0;
  24. if (arr.length > 0) {
  25. mean = total / arr.length;
  26. }
  27. System.out.print("Mean: " + mean);
  28. int big = arr[0];
  29. for (int i = 0; i < arr.length; i++) {
  30. if (arr[i] > big) {
  31. big = arr[i];
  32. }
  33. }
  34. System.out.println();
  35. System.out.print("Largest: " + big);
  36. System.out.println();
  37. ///////////////////////////////////////////////////////////////////////////////////////////////
  38. int less;
  39. for (int i = 0; i < mean; i++) {
  40. int num2 = i;
  41. int[] arr2 = new int[num2];
  42. int count = 0;
  43. while (num2 != 0) {
  44. num2 /= 10;
  45. ++count;
  46. System.out.print("Numbers less than the mean: " + count);
  47. }
  48. }
  49. //////////////////////////////////////////////////////////////////////////////////////////////
  50. }

请注意,上述代码可能有一些逻辑错误,特别是在计算小于平均值的数字的部分。如果您需要更详细的帮助或修复,请随时提问。

英文:
  • Okay so im trying to find the amount of numbers that are less then
    the mean of the first array. Everything but the last part is working and i
    cant figure it out. The code at the bottom is what im having problems with.

for example. if i enter 1 2 3 4 5. the mean is 3 and, 1 and 2 are less then 3. so the answer would be 2 numbers.

  1. public static void main(String[] args) {
  2. Scanner in=new Scanner(System.in);
  3. System.out.println(&quot;How many integers should we analyze?&quot; );
  4. int num;
  5. num=in.nextInt();
  6. while ( num &lt;= 2)
  7. {
  8. System.out.println( &quot;Please reenter, integer must be greater than 1&quot; );
  9. num=in.nextInt();
  10. }
  11. int[] arr = new int[num];
  12. System.out.println( &quot;Please enter the &quot;+ num +&quot; integers:&quot; );
  13. for (int i = 0; i &lt; arr.length; i++)
  14. {
  15. arr[i] = in.nextInt();
  16. }
  17. System.out.print(&quot;Number of integers input: &quot; + num);
  18. System.out.println();
  19. double total = 0;
  20. for( int element : arr) {
  21. total += element;
  22. }
  23. System.out.print(&quot;Total: &quot; + (int) total);
  24. System.out.println();
  25. double mean = 0;
  26. if ( arr.length &gt; 0) {
  27. mean = total / arr.length;
  28. }
  29. System.out.print(&quot;Mean: &quot; + mean );
  30. int big = arr[0];
  31. for (int i = 0 ; i &lt; arr.length; i++) {
  32. if (arr[i] &gt; big) {
  33. big = arr[i];
  34. }
  35. }
  36. System.out.println();
  37. System.out.print(&quot;Largest: &quot; + big);
  38. System.out.println();
  39. ///////////////////////////////////////////////////////////////////////////////////////////////
  40. int less;
  41. for(int i=0;i&lt;mean;i++) {
  42. int num2 = i;
  43. int[] arr2 = new int[num2];
  44. int count = 0;
  45. while ( num2 != 0 )
  46. {
  47. num2/=10;
  48. ++count;
  49. System.out.print(&quot;Numbers less than the mean: &quot; + count);
  50. }
  51. }
  52. //////////////////////////////////////////////////////////////////////////////////////////////
  53. }
  54. }

答案1

得分: 0

你可以使用下面的代码:

  1. int count = 0;
  2. for (int i = 0; i < arr.length; i++) {
  3. if (arr[i] < mean)
  4. count++;
  5. }
  6. System.out.println("小于平均值的数字个数为:" + count);

这段代码的作用是遍历所有原始整数,如果某个整数小于平均值,就将计数变量增加1。

英文:

you could use this code below

  1. int count = 0;
  2. for(int i =0;i&lt; arr.length;i++) {
  3. if(arr[i] &lt; mean)
  4. count++;
  5. }
  6. System.out.println(&quot;numbers less than mean &quot; + count);

what this does is it loops through all of the original integers and if one is less than the mean, the count variable goes up by 1.

答案2

得分: 0

你应该遍历数组并检查每个元素是否小于均值。如果是的话,就增加一个整数。

  1. int less = 0;
  2. for (int i = 0; i < arr.length; i++) {
  3. if (arr[i] < mean) {
  4. less++;
  5. }
  6. }
  7. System.out.print("小于均值的数字:" + less);
英文:

You should iterate over the array and check each element if they are under the mean. If yes, then increment an integer.

  1. int less = 0;
  2. for(int i = 0; i &lt; arr.length; i++) {
  3. if(arr[i] &lt; mean) {
  4. less++;
  5. }
  6. }
  7. System.out.print(&quot;Numbers less than the mean: &quot; + less);

huangapple
  • 本文由 发表于 2020年5月5日 06:57:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/61603012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定