为什么null出现在我的输出中?-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: " + 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("Maximum Number: " + max);
System.out.println("Range: " + range);
}
public void Median(int[] c) {
Arrays.sort(c);
double median = 0;
System.out.println("Median: " + 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("Median: " + 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("Mode: " + maxValue);

}
public void Histogram(int[] f) {
String[] asterisk = new String[6];
System.out.println("Histogram: ");
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("Enter a number (1-50): ");
nums[i] = reader.readInt();
while (nums[i] >= 1 && nums[i] <= 50 && i < 19) {
i++;
System.out.print("Enter a number (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);
}
}

有什么建议吗?感谢

这是这里的代码:

public void Histogram(int[] f) {
String[] asterisk = new String[6];
System.out.println("Histogram: ");
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*"

相关内容

  • 没有找到相关文章

最新更新