How many elements do you want in the array? 3 Enter an integer to store in the array 66 enter an integer to store in the array 33 enter an integer to store in the array 99 Here are the sorted values: [33,66,99]
这应该是结果。
这就是我所做的:
import java.util.Scanner;
public class PracTe3Te {
/**
* @param args
*/
public static void main(String[] args) {
int [] a;
int size;
int value;
Scanner sc;
sc = new Scanner(System.in);
System.out.println("How many elements do you want in the array? ");
size = sc.nextInt();
a = new int [size];
System.out.println("the size of array is " + a.length);
for (int i = 0; i < a.length; i++)
{
System.out.println("Enter an integer to store in the array");
value = sc.nextInt();
a[i] = value;
int newNum = value;
if (value < a.length - 1) { //otherwise array is already full
a[value] = value;
newNum = newNum + 1;
}
}
System.out.print(a);
}
}
得到这个结果:
How many elements do you want in the array? 3 the size of array is 3 Enter an integer to store in the array 4 Enter an integer to store in the array 3 Enter an integer to store in the array 5 [I@60072ffb
如何打印输入项目的数组列表,而不是[I@60072ffb
?
答案 0 :(得分:1)
使用Arrays.toString()
(不要忘记import java.util.Arrays
):
System.out.print(Arrays.toString(a));
数组不会覆盖toString()
(当您尝试打印对象时调用的方法),因此实现默认为that of Object
,其中包含类名后跟哈希代码。
答案 1 :(得分:0)
您正在尝试打印数组:
System.out.print(a);
但Java并不够聪明,不知道你的意思。如果它是一个对象数组怎么办?如果它是一个JButton数组怎么办?应该打印出什么?
API中有一些函数以更智能的方式打印数组,或者您可以定义自己的算法来打印数组,可能是通过循环并一次打印一个数组的单个元素。我认为这是作业的重点。