【发布时间】:2020-02-14 20:52:03
【问题描述】:
我想创建一个返回用户输入值的双精度数组的方法。我已经想出了如何创建一个方法来要求用户选择一个数组应该包含多少个元素,然后将大小传递给下一个方法,即输出一个用户输入值的双精度数组。
我的目标是练习学习如何使用基本方法(只是公共静态方法)来分治手头的问题。
...java 包数组练习; 导入 java.util.Scanner;
公共类 Array_Exercises {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Get number of elements to store values from the user
int number = numOfElements();
System.out.println(valueOfElements(number));
}
public static int numOfElements () {
// Create a Scanner object to get the user's input
Scanner input = new Scanner (System.in);
// Get the input from the user
System.out.print("Enter how many number of the type double do you want"
+ " to store in your array? ");
return input.nextInt();
}
public static double[] valueOfElements (int num) {
// Create a Scanner object to get the user's value for each element
Scanner input = new Scanner (System.in);
// Declare an array of doubles
double[] double_array = new double[num];
// Loop through the elements of double array
for (int i = 0; i < double_array.length; i++) {
System.out.print("Enter a value #" + (i + 1) + ": ");
double_array[i] = input.nextDouble();
}
return double_array;
}
}
预期的输出应该打印出 main 方法中双精度数组的所有值。
我得到的只是这个:
运行: 输入要在数组中存储多少个 double 类型? 2 输入值 #1:1.234567 输入值 #2:2.345678 [D@55f96302
这是为什么?我在这里做错了什么?我只是一个初学者,这学期我要上 Java 的课程,所以一切对我来说都是新的。
【问题讨论】:
标签: java arrays static-methods