为什么在我的输出中出现了 “null”? – Java

huangapple go评论123阅读模式
英文:

Why is null appearing in my output? - Java

问题

以下是您提供的代码的翻译部分:

  1. 我正在编写这个程序突然在输出中出现了 null
  2. 1-10 |null**********
  3. 11-20 |null**********
  4. 21-30 |null
  5. 31-40 |null
  6. 41-50 |null
  7. 应该是这样的
  8. 1-10 |**********
  9. 11-20 |**********
  10. 21-30 |
  11. 31-40 |
  12. 41-50 |
  13. 这是我的代码
  14. import java.util.Arrays;
  15. public class Ex4Method {
  16. public void Average(int[] a) {
  17. int total = 0;
  18. for (int i = 0; i < a.length; i++) {
  19. total = total + a[i];
  20. }
  21. int average = total / a.length;
  22. System.out.println(" ");
  23. System.out.println("平均值:" + average);
  24. }
  25. public void MaxAndRange(int[] b) {
  26. int min = b[0];
  27. int max = b[0];
  28. for (int i = 0; i <= 19; i++) {
  29. //System.out.println(i);
  30. if (b[i] < min) {
  31. min = b[i];
  32. }
  33. if (b[i] > max) {
  34. max = b[i];
  35. }
  36. }
  37. int range = max - min;
  38. System.out.println("最大数:" + max);
  39. System.out.println("范围:" + range);
  40. }
  41. public void Median(int[] c) {
  42. Arrays.sort(c);
  43. double median = 0;
  44. System.out.println("中位数:" + median);
  45. }
  46. public void findMedian(int a[]) {
  47. // 首先对数组进行排序
  48. Arrays.sort(a);
  49. double median;
  50. if (a.length % 2 == 0) {
  51. median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
  52. } else {
  53. median = (double) a[a.length / 2];
  54. }
  55. System.out.println("中位数:" + median);
  56. }
  57. public void Mode(int[] d) {
  58. int maxValue = 0, maxCount = 0;
  59. for (int i = 0; i < d.length; i++) {
  60. int count = 0;
  61. for (int j = 0; j < d.length; j++) {
  62. if (d[j] == d[i]) {
  63. count++;
  64. }
  65. }
  66. if (count > maxCount) {
  67. maxCount = count;
  68. maxValue = d[i];
  69. }
  70. }
  71. System.out.println("众数:" + maxValue);
  72. }
  73. public void Histogram(int[] f) {
  74. String[] asterisk = new String[6];
  75. System.out.println("直方图:");
  76. System.out.println(" ");
  77. for (int i = 0; i <= f.length - 1; i++) {
  78. if (f[i] >= 1 && f[i] <= 10) {
  79. asterisk[1] += "*";
  80. }
  81. if (f[i] >= 11 && f[i] <= 20) {
  82. asterisk[2] += "*";
  83. }
  84. if (f[i] >= 21 && f[i] <= 30) {
  85. asterisk[3] += "*";
  86. }
  87. if (f[i] >= 31 && f[i] <= 40) {
  88. asterisk[4] += "*";
  89. }
  90. if (f[i] >= 41 && f[i] <= 50) {
  91. asterisk[5] += "*";
  92. }
  93. }
  94. System.out.println(" 1-10 |" + asterisk[1]);
  95. System.out.println("11-20 |" + asterisk[2]);
  96. System.out.println("21-30 |" + asterisk[3]);
  97. System.out.println("31-40 |" + asterisk[4]);
  98. System.out.println("41-50 |" + asterisk[5]);
  99. }
  100. }
  101. 主类-
  102. import TurtleGraphics.KeyboardReader;
  103. public class ArrayEx4 {
  104. public static void main(String[] args) {
  105. KeyboardReader reader = new KeyboardReader();
  106. Ex4Method object = new Ex4Method();
  107. int[] nums = new int[20];
  108. int i = 0;
  109. System.out.print("输入一个数字 (1-50): ");
  110. nums[i] = reader.readInt();
  111. while (nums[i] >= 1 && nums[i] <= 50 && i < 19) {
  112. i++;
  113. System.out.print("输入一个数字 (1-50): ");
  114. nums[i] = reader.readInt();
  115. //occurences[nums[i]]++;
  116. }
  117. //for(int x=0;x<=4;x++) {
  118. //System.out.println(occurences[x]);
  119. //}
  120. object.Average(nums);
  121. object.MaxAndRange(nums);
  122. object.findMedian(nums);
  123. object.Mode(nums);
  124. object.Histogram(nums);
  125. }
  126. }
  127. 有什么建议吗谢谢
英文:

I am writing this program and all of a sudden null appears in the output:

  1. 1-10 |null**********
  2. 11-20 |null**********
  3. 21-30 |null
  4. 31-40 |null
  5. 41-50 |null

it should be like this:

  1. 1-10 |**********
  2. 11-20 |**********
  3. 21-30 |
  4. 31-40 |
  5. 41-50 |

This is my code:

  1. import java.util.Arrays;
  2. public class Ex4Method {
  3. public void Average(int[] a) {
  4. int total = 0;
  5. for (int i = 0; i &lt; a.length; i++) {
  6. total = total + a[i];
  7. }
  8. int average = total / a.length;
  9. System.out.println(&quot; &quot;);
  10. System.out.println(&quot;Average: &quot; + average);
  11. }
  12. public void MaxAndRange(int[] b) {
  13. int min = b[0];
  14. int max = b[0];
  15. for (int i = 0; i &lt;= 19; i++) {
  16. //System.out.println(i);
  17. if (b[i] &lt; min) {
  18. min = b[i];
  19. }
  20. if (b[i] &gt; max) {
  21. max = b[i];
  22. }
  23. }
  24. int range = max - min;
  25. System.out.println(&quot;Maximum Number: &quot; + max);
  26. System.out.println(&quot;Range: &quot; + range);
  27. }
  28. public void Median(int[] c) {
  29. Arrays.sort(c);
  30. double median = 0;
  31. System.out.println(&quot;Median: &quot; + median);
  32. }
  33. public void findMedian(int a[]) {
  34. // First we sort the array
  35. Arrays.sort(a);
  36. double median;
  37. if (a.length % 2 == 0) {
  38. median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
  39. } else {
  40. median = (double) a[a.length / 2];
  41. }
  42. System.out.println(&quot;Median: &quot; + median);
  43. }
  44. public void Mode(int[] d) {
  45. int maxValue = 0, maxCount = 0;
  46. for (int i = 0; i &lt; d.length; i++) {
  47. int count = 0;
  48. for (int j = 0; j &lt; d.length; j++) {
  49. if (d[j] == d[i]) {
  50. count++;
  51. }
  52. }
  53. if (count &gt; maxCount) {
  54. maxCount = count;
  55. maxValue = d[i];
  56. }
  57. }
  58. System.out.println(&quot;Mode: &quot; + maxValue);
  59. }
  60. public void Histogram(int[] f) {
  61. String[] asterisk = new String[6];
  62. System.out.println(&quot;Histogram: &quot;);
  63. System.out.println(&quot; &quot;);
  64. for (int i = 0; i &lt;= f.length - 1; i++) {
  65. if (f[i] &gt;= 1 &amp;&amp; f[i] &lt;= 10) {
  66. asterisk[1] += &quot;*&quot;;
  67. }
  68. if (f[i] &gt;= 11 &amp;&amp; f[i] &lt;= 20) {
  69. asterisk[2] += &quot;*&quot;;
  70. }
  71. if (f[i] &gt;= 21 &amp;&amp; f[i] &lt;= 30) {
  72. asterisk[3] += &quot;*&quot;;
  73. }
  74. if (f[i] &gt;= 31 &amp;&amp; f[i] &lt;= 40) {
  75. asterisk[4] += &quot;*&quot;;
  76. }
  77. if (f[i] &gt;= 41 &amp;&amp; f[i] &lt;= 50) {
  78. asterisk[5] += &quot;*&quot;;
  79. }
  80. }
  81. System.out.println(&quot; 1-10 |&quot; + asterisk[1]);
  82. System.out.println(&quot;11-20 |&quot; + asterisk[2]);
  83. System.out.println(&quot;21-30 |&quot; + asterisk[3]);
  84. System.out.println(&quot;31-40 |&quot; + asterisk[4]);
  85. System.out.println(&quot;41-50 |&quot; + asterisk[5]);
  86. }
  87. }

MAIN CLASS-

  1. import TurtleGraphics.KeyboardReader;
  2. public class ArrayEx4 {
  3. public static void main(String[] args) {
  4. KeyboardReader reader = new KeyboardReader();
  5. Ex4Method object = new Ex4Method();
  6. int[] nums = new int[20];
  7. int i = 0;
  8. System.out.print(&quot;Enter a number (1-50): &quot;);
  9. nums[i] = reader.readInt();
  10. while (nums[i] &gt;= 1 &amp;&amp; nums[i] &lt;= 50 &amp;&amp; i &lt; 19) {
  11. i++;
  12. System.out.print(&quot;Enter a number (1-50): &quot;);
  13. nums[i] = reader.readInt();
  14. //occurences[nums[i]]++;
  15. }
  16. //for(int x=0;x&lt;=4;x++) {
  17. //System.out.println(occurences[x]);
  18. //}
  19. object.Average(nums);
  20. object.MaxAndRange(nums);
  21. object.findMedian(nums);
  22. object.Mode(nums);
  23. object.Histogram(nums);
  24. }
  25. }

Any suggestions? Thanks

答案1

得分: 2

这就是代码:

  1. public void Histogram(int[] f) {
  2. String[] asterisk = new String[6];
  3. System.out.println("直方图:");
  4. System.out.println(" ");
  5. for (int i = 0; i <= f.length - 1; i++) {
  6. if (f[i] >= 1 && f[i] <= 10) {
  7. asterisk[1] += "*";
  8. }
  9. if (f[i] >= 11 && f[i] <= 20) {
  10. asterisk[2] += "*";
  11. }
  12. if (f[i] >= 21 && f[i] <= 30) {
  13. asterisk[3] += "*";
  14. }
  15. if (f[i] >= 31 && f[i] <= 40) {
  16. asterisk[4] += "*";
  17. }
  18. if (f[i] >= 41 && f[i] <= 50) {
  19. asterisk[5] += "*";
  20. }
  21. }
  22. System.out.println(" 1-10 |" + asterisk[1]);
  23. System.out.println("11-20 |" + asterisk[2]);
  24. System.out.println("21-30 |" + asterisk[3]);
  25. System.out.println("31-40 |" + asterisk[4]);
  26. System.out.println("41-50 |" + asterisk[5]);
  27. }
  28. 更具体地说是这部分
  29. asterisk[i] += "*";
  30. Java字符串的`+`操作符表示连接两个操作数在进行连接之前会被转换为字符串
  31. 在最开始`new String[6]` 填充了 `String` 类型的默认值对于任何从 `java.lang.Object` 继承的类型来说默认值是 `null`当你使用操作符进行连接时数组中的任何一个位置在第一次处理时都会遇到 `null`
  32. 而且
  33. String.valueOf(null) + "*";
  34. 会输出 `"null*"`
  35. <details>
  36. <summary>英文:</summary>
  37. This is the code right here:
  38. public void Histogram(int[] f) {
  39. String[] asterisk = new String[6];
  40. System.out.println(&quot;Histogram: &quot;);
  41. System.out.println(&quot; &quot;);
  42. for (int i = 0; i &lt;= f.length - 1; i++) {
  43. if (f[i] &gt;= 1 &amp;&amp; f[i] &lt;= 10) {
  44. asterisk[1] += &quot;*&quot;;
  45. }
  46. if (f[i] &gt;= 11 &amp;&amp; f[i] &lt;= 20) {
  47. asterisk[2] += &quot;*&quot;;
  48. }
  49. if (f[i] &gt;= 21 &amp;&amp; f[i] &lt;= 30) {
  50. asterisk[3] += &quot;*&quot;;
  51. }
  52. if (f[i] &gt;= 31 &amp;&amp; f[i] &lt;= 40) {
  53. asterisk[4] += &quot;*&quot;;
  54. }
  55. if (f[i] &gt;= 41 &amp;&amp; f[i] &lt;= 50) {
  56. asterisk[5] += &quot;*&quot;;
  57. }
  58. }
  59. System.out.println(&quot; 1-10 |&quot; + asterisk[1]);
  60. System.out.println(&quot;11-20 |&quot; + asterisk[2]);
  61. System.out.println(&quot;21-30 |&quot; + asterisk[3]);
  62. System.out.println(&quot;31-40 |&quot; + asterisk[4]);
  63. System.out.println(&quot;41-50 |&quot; + asterisk[5]);
  64. }
  65. More specifically, this part:
  66. asterisk[i] += &quot;*&quot;;
  67. In Java, `+` operator for strings means concatenation, and both operands are converted to strings beforehand.
  68. At the very beginning, `new String[6]` is filled with default values for `String` type, which, for any type inherited from `java.lang.Object` is a `null` value. When you concatenate using operators, the very first pass over any slot in the array will always encounter a `null` value.
  69. And
  70. String.valueOf(null) + &quot;*&quot;
  71. will output `&quot;null*&quot;`
  72. </details>

huangapple
  • 本文由 发表于 2020年10月6日 01:51:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64213745.html
匿名

发表评论

匿名网友

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

确定