英文:
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 < 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]);
}
}
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("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);
}
}
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("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]);
}
More specifically, this part:
asterisk[i] += "*";
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) + "*"
will output `"null*"`
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论