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

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

Why is null appearing in my output? - Java

问题

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

我正在编写这个程序突然在输出中出现了 null

     1-10 |null**********
    
    11-20 |null**********
    
    21-30 |null
    
    31-40 |null
    
    41-50 |null
    
应该是这样的
    
     1-10 |**********
    
    11-20 |**********
    
    21-30 |
    
    31-40 |
    
    41-50 |

这是我的代码

    import java.util.Arrays;
    
    public class Ex4Method {
    
        public void Average(int[] a) {
    
            int total = 0;
    
            for (int i = 0; i < a.length; i++) {
                total = total + a[i];
            }
    
            int average = total / a.length;
    
            System.out.println(" ");
            System.out.println("平均值:" + average);
        }
    
        public void MaxAndRange(int[] b) {
    
            int min = b[0];
            int max = b[0];
    
            for (int i = 0; i <= 19; i++) {
                //System.out.println(i);
                if (b[i] < min) {
                    min = b[i];
                }
                if (b[i] > max) {
                    max = b[i];
                }
            }
            int range = max - min;
            System.out.println("最大数:" + max);
            System.out.println("范围:" + range);
    
        }
    
        public void Median(int[] c) {
            Arrays.sort(c);
            double median = 0;
            System.out.println("中位数:" + median);
        }
    
        public void findMedian(int a[]) {
            // 首先对数组进行排序
            Arrays.sort(a);
            double median;
            if (a.length % 2 == 0) {
                median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
            } else {
                median = (double) a[a.length / 2];
            }
    
            System.out.println("中位数:" + median);
        }
    
        public void Mode(int[] d) {
    
            int maxValue = 0, maxCount = 0;
    
            for (int i = 0; i < d.length; i++) {
                int count = 0;
                for (int j = 0; j < d.length; j++) {
                    if (d[j] == d[i]) {
                        count++;
                    }
                }
                if (count > maxCount) {
                    maxCount = count;
                    maxValue = d[i];
                }
            }
            System.out.println("众数:" + maxValue);
    
    
        }
    
        public void Histogram(int[] f) {
    
            String[] asterisk = new String[6];
    
            System.out.println("直方图:");
            System.out.println(" ");
            for (int i = 0; i <= f.length - 1; i++) {
    
                if (f[i] >= 1 && f[i] <= 10) {
                    asterisk[1] += "*";
                }
                if (f[i] >= 11 && f[i] <= 20) {
                    asterisk[2] += "*";
                }
                if (f[i] >= 21 && f[i] <= 30) {
                    asterisk[3] += "*";
                }
                if (f[i] >= 31 && f[i] <= 40) {
                    asterisk[4] += "*";
                }
                if (f[i] >= 41 && f[i] <= 50) {
                    asterisk[5] += "*";
                }
            }
    
            System.out.println(" 1-10 |" + asterisk[1]);
            System.out.println("11-20 |" + asterisk[2]);
            System.out.println("21-30 |" + asterisk[3]);
            System.out.println("31-40 |" + asterisk[4]);
            System.out.println("41-50 |" + asterisk[5]);
        }
    }



主类-

    
    import TurtleGraphics.KeyboardReader;
    
    public class ArrayEx4 {
    
        public static void main(String[] args) {
    
            KeyboardReader reader = new KeyboardReader();
            Ex4Method object = new Ex4Method();
    
            int[] nums = new int[20];
            int i = 0;
            System.out.print("输入一个数字 (1-50): ");
            nums[i] = reader.readInt();
            while (nums[i] >= 1 && nums[i] <= 50 && i < 19) {
    
                i++;
                System.out.print("输入一个数字 (1-50): ");
                nums[i] = reader.readInt();
                //occurences[nums[i]]++;
    
            }
            //for(int x=0;x<=4;x++) {
            //System.out.println(occurences[x]);
            //}
    
            object.Average(nums);
            object.MaxAndRange(nums);
            object.findMedian(nums);
            object.Mode(nums);
            object.Histogram(nums);
    
        }
    }
    

有什么建议吗谢谢
英文:

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

 1-10 |null**********
11-20 |null**********
21-30 |null
31-40 |null
41-50 |null

it should be like this:

 1-10 |**********
11-20 |**********
21-30 |
31-40 |
41-50 |

This is my code:

import java.util.Arrays;
public class Ex4Method {
public void Average(int[] a) {
int total = 0;
for (int i = 0; i &lt; a.length; i++) {
total = total + a[i];
}
int average = total / a.length;
System.out.println(&quot; &quot;);
System.out.println(&quot;Average: &quot; + average);
}
public void MaxAndRange(int[] b) {
int min = b[0];
int max = b[0];
for (int i = 0; i &lt;= 19; i++) {
//System.out.println(i);
if (b[i] &lt; min) {
min = b[i];
}
if (b[i] &gt; max) {
max = b[i];
}
}
int range = max - min;
System.out.println(&quot;Maximum Number: &quot; + max);
System.out.println(&quot;Range: &quot; + range);
}
public void Median(int[] c) {
Arrays.sort(c);
double median = 0;
System.out.println(&quot;Median: &quot; + median);
}
public void findMedian(int a[]) {
// First we sort the array 
Arrays.sort(a);
double median;
if (a.length % 2 == 0) {
median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
} else {
median = (double) a[a.length / 2];
}
System.out.println(&quot;Median: &quot; + median);
}
public void Mode(int[] d) {
int maxValue = 0, maxCount = 0;
for (int i = 0; i &lt; d.length; i++) {
int count = 0;
for (int j = 0; j &lt; d.length; j++) {
if (d[j] == d[i]) {
count++;
}
}
if (count &gt; maxCount) {
maxCount = count;
maxValue = d[i];
}
}
System.out.println(&quot;Mode: &quot; + maxValue);
}
public void Histogram(int[] f) {
String[] asterisk = new String[6];
System.out.println(&quot;Histogram: &quot;);
System.out.println(&quot; &quot;);
for (int i = 0; i &lt;= f.length - 1; i++) {
if (f[i] &gt;= 1 &amp;&amp; f[i] &lt;= 10) {
asterisk[1] += &quot;*&quot;;
}
if (f[i] &gt;= 11 &amp;&amp; f[i] &lt;= 20) {
asterisk[2] += &quot;*&quot;;
}
if (f[i] &gt;= 21 &amp;&amp; f[i] &lt;= 30) {
asterisk[3] += &quot;*&quot;;
}
if (f[i] &gt;= 31 &amp;&amp; f[i] &lt;= 40) {
asterisk[4] += &quot;*&quot;;
}
if (f[i] &gt;= 41 &amp;&amp; f[i] &lt;= 50) {
asterisk[5] += &quot;*&quot;;
}
}
System.out.println(&quot; 1-10 |&quot; + asterisk[1]);
System.out.println(&quot;11-20 |&quot; + asterisk[2]);
System.out.println(&quot;21-30 |&quot; + asterisk[3]);
System.out.println(&quot;31-40 |&quot; + asterisk[4]);
System.out.println(&quot;41-50 |&quot; + asterisk[5]);
}
}

MAIN CLASS-

import TurtleGraphics.KeyboardReader;
public class ArrayEx4 {
public static void main(String[] args) {
KeyboardReader reader = new KeyboardReader();
Ex4Method object = new Ex4Method();
int[] nums = new int[20];
int i = 0;
System.out.print(&quot;Enter a number (1-50): &quot;);
nums[i] = reader.readInt();
while (nums[i] &gt;= 1 &amp;&amp; nums[i] &lt;= 50 &amp;&amp; i &lt; 19) {
i++;
System.out.print(&quot;Enter a number (1-50): &quot;);
nums[i] = reader.readInt();
//occurences[nums[i]]++;
}
//for(int x=0;x&lt;=4;x++) {
//System.out.println(occurences[x]);
//}
object.Average(nums);
object.MaxAndRange(nums);
object.findMedian(nums);
object.Mode(nums);
object.Histogram(nums);
}
}

Any suggestions? Thanks

答案1

得分: 2

这就是代码:

public void Histogram(int[] f) {

    String[] asterisk = new String[6];

    System.out.println("直方图:");
    System.out.println(" ");
    for (int i = 0; i <= f.length - 1; i++) {

        if (f[i] >= 1 && f[i] <= 10) {
            asterisk[1] += "*";
        }
        if (f[i] >= 11 && f[i] <= 20) {
            asterisk[2] += "*";
        }
        if (f[i] >= 21 && f[i] <= 30) {
            asterisk[3] += "*";
        }
        if (f[i] >= 31 && f[i] <= 40) {
            asterisk[4] += "*";
        }
        if (f[i] >= 41 && f[i] <= 50) {
            asterisk[5] += "*";
        }
    }

    System.out.println(" 1-10 |" + asterisk[1]);
    System.out.println("11-20 |" + asterisk[2]);
    System.out.println("21-30 |" + asterisk[3]);
    System.out.println("31-40 |" + asterisk[4]);
    System.out.println("41-50 |" + asterisk[5]);
}

更具体地说是这部分

    asterisk[i] += "*";

在Java中字符串的`+`操作符表示连接两个操作数在进行连接之前会被转换为字符串

在最开始,`new String[6]` 填充了 `String` 类型的默认值对于任何从 `java.lang.Object` 继承的类型来说默认值是 `null`。当你使用操作符进行连接时数组中的任何一个位置在第一次处理时都会遇到 `null`

而且

    String.valueOf(null) + "*";

会输出 `"null*"`。

<details>
<summary>英文:</summary>

This is the code right here:

    public void Histogram(int[] f) {

        String[] asterisk = new String[6];

        System.out.println(&quot;Histogram: &quot;);
        System.out.println(&quot; &quot;);
        for (int i = 0; i &lt;= f.length - 1; i++) {

            if (f[i] &gt;= 1 &amp;&amp; f[i] &lt;= 10) {
                asterisk[1] += &quot;*&quot;;
            }
            if (f[i] &gt;= 11 &amp;&amp; f[i] &lt;= 20) {
                asterisk[2] += &quot;*&quot;;
            }
            if (f[i] &gt;= 21 &amp;&amp; f[i] &lt;= 30) {
                asterisk[3] += &quot;*&quot;;
            }
            if (f[i] &gt;= 31 &amp;&amp; f[i] &lt;= 40) {
                asterisk[4] += &quot;*&quot;;
            }
            if (f[i] &gt;= 41 &amp;&amp; f[i] &lt;= 50) {
                asterisk[5] += &quot;*&quot;;
            }
        }

        System.out.println(&quot; 1-10 |&quot; + asterisk[1]);
        System.out.println(&quot;11-20 |&quot; + asterisk[2]);
        System.out.println(&quot;21-30 |&quot; + asterisk[3]);
        System.out.println(&quot;31-40 |&quot; + asterisk[4]);
        System.out.println(&quot;41-50 |&quot; + asterisk[5]);
    }

More specifically, this part:

    asterisk[i] += &quot;*&quot;;

In Java, `+` operator for strings means concatenation, and both operands are converted to strings beforehand.

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.

And

    String.valueOf(null) + &quot;*&quot;

will output `&quot;null*&quot;`


</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:

确定