![](/img/trans.png)
[英]This is my code.In this after checking values for matching direct else is printing. IF is not executing. why?
[英]Some junk values is printing. Why?
我编写了以下代码。
public class ArrayLessons {
public static void main(String[] args) {
int values[] = { 1, 2, 3, 4, 5, 6 };
for (int i = 1; i < values.length; i++) {
System.out.println(values);
}
}
}
但是输出会放入一些垃圾值,例如"[I@5df9aeda"
。
为什么不显示1, 2, 3, 4, 5, 6
?
我对编程和Java非常陌生。 所以请指导我。 谢谢。
0
开始。 在for-loop
使用int i = 0
。 values[i]
打印这些values
。 如果您只想打印:请尝试以下代码:
System.out.println(Arrays.toString(array));
您不能不给出任何索引就直接打印数组值,如果要打印它的值,您必须使用循环和这样的索引进行处理,请参见以下示例
public class ArrayLessonsDem {
public static void main(String[] args) {
int values[] = { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
}
}
数组索引的起始点是0,因此您始终以0开始,请检查my的外观和System.out.println语句是否完全受关注。
您需要将i变量与数组一起使用。 就像在values[i]
而不是仅在values
。 同样,数组索引是从零开始的。
是的,我同意詹先生。 另外,如果要单行打印结果,则不能使用System.out.println()
。 只需使用System.out.print(values[i])
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.