我正在尝试根据用户输入初始化一个数组,以便它可以是任何自定义大小。默认情况下,我已将所有 0.0 值的数组初始化为 10,但是当我尝试调用初始化数组的方法时,什么也没有发生,并且数组中的大小或值都不会改变。
班级编号:
public class Numbers {
private Float [] numbers;
private int default_size = 10;
public Numbers() {
/// write code here to intialize a "default" array since this is the default constructor
numbers = new Float[default_size];
for(int i = 0; i < default_size; i++)
numbers [i] = (float) 0.0;
}
public Numbers (int size) {
/// write code here to initialize an "initial" array given the parameter size as this is an initial constructor
numbers = new Float[size];
}
public void initValuesInArray() {
/// write code here to intialize the values in the array
Scanner scan = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.println("Enter Value : ");
numbers[i] = scan.nextFloat();
}
}
public String toString() {
// write code to return the values in the array in a list format - one value per line
for (int i = 0; i < numbers.length; i++)
System.out.println(numbers[i]);
return " ";
}
public float calcAverage() {
// write code to return the average of the values in the array
double sum = 0.0;
for (int i = 0; i < numbers.length; i++)
sum += numbers[i];
double average = sum / numbers.length;
System.out.println(average);
return (float) average;
}
}
主类:
public class Main {
public static void main (String [] args) {
// write the code here to implement the menu as specified in Lab 1
boolean menuLoop = true;
while(true) {
System.out.println("Enter 1 to initialize a default array");
}
}
}
}
它需要能够根据用户输入更改数组的大小,然后用户可以将值输入到数组中。
www说
拉莫斯之舞
慕斯王
随时随地看视频慕课网APP
相关分类