我正在从事编程项目,而且我对Java有一点了解。我似乎无法弄清楚这位教授正在寻找什么。
您将创建一个名为ArrayWorks
的Java类。构造函数将询问用户他们输入了多少个数字,然后从用户那里检索这些数字。这些数字将放入构造函数创建的ArrayList
中。 ArrayList
是唯一的属性
ArrayWorks
课程。
然后您将编写以下方法:
我得到了什么:
import java.util.*;
public class ArrayWorks{
ArrayWorks (int inputs, ArrayList<Integer> nums){
this.inputs = inputs;
this.nums = nums;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> nums = new ArrayList<Integer>();
System.out.println("please enter how many numbers you have to enter!");
int numOfInputs = sc.nextInt();
System.out.println("Now enter the numbers!");
for(int i = numOfInputs; i>0; i--){
nums.add(sc.nextInt());
}
}
int inputs;
ArrayList<Integer> nums;
int size;
int max;
int min;
double ave;
public int getSize(){
size = nums.size();
return size;
}
public double ave(){
int sum = 0;
for (Integer i : nums) {
sum += i;
}
double average = sum / nums.size();
return average;
}
public int max(){
for(Integer i: nums) {
if(i > max) max = i;
}
return max;
}
public int min(){
for(Integer i: nums) {
if(i < min) min = i;
}
return min;
}
}
问题是我不知道如何满足他对构造者成为输入者的需求。
非常感谢Thrasher的帮助!如果有人有更多的意见,请告诉我!
答案 0 :(得分:1)
这是您教授要求的答案,我建议在主类上询问用户输入并使用构造函数来设置参数,因为您可以在任何类型的用户界面上使用您的类。我还研究了将来可能需要的一些方法(大小,平均值,最大值,最小值)。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrayWorks
{
private List < Integer > numberList;
public ArrayWorks ()
{
Scanner sc = new Scanner(System.in);
List<Integer> mylist = new ArrayList<Integer>();
System.out.print("Enter number of iterations: ");
int numberOfIterations = sc.nextInt ( );
//Iterate user input number
for(int i = 0; i < numberOfIterations; i++)
{
System.out.print("Enter Integer number " + (i+1) +": ");
int num = sc.nextInt();
mylist.add(num);
}
System.out.println ( "Finished" );
this.numberList = mylist;
}
public List < Integer > getNumberList ( )
{
return numberList;
}
public void setNumberList ( List < Integer > numberList )
{
this.numberList = numberList;
}
public int size()
{
return this.numberList.size ( );
}
public float ave()
{
int size = 0;
int sum = 0;
for(int i = 0; i < this.numberList.size ( ); i++)
{
sum += numberList.get ( i );
size++;
}
return (float) sum/size;
}
public int max()
{
int max = 0;
int current = 0;
for(int i = 0; i < this.numberList.size ( ); i++)
{
current = numberList.get ( i );
if ( current > max )
{
max = current;
}
}
return max;
}
public int min()
{
int min = numberList.get ( 0 );
int current = 0;
for(int i = 0; i < this.numberList.size ( ); i++)
{
current = numberList.get ( i );
if ( current < min )
{
min = current;
}
}
return min;
}
@Override
public String toString ( )
{
return "ArrayWorks [numberList=" + numberList + "]";
}
}
public class mainProgram
{
public static void main ( String [ ] args )
{
//Create your ArrayWorks
ArrayWorks myArrayWork = new ArrayWorks ();
System.out.println ( "toString: " + myArrayWork.toString ( ) );
System.out.println ( "size: " + myArrayWork.size() );
System.out.println ( "avg: " + myArrayWork.ave ( ) );
System.out.println ( "max: " + myArrayWork.max ( ) );
System.out.println ( "min: " + myArrayWork.min ( ) );
}
}
输入迭代次数:3
输入整数1:10
输入整数2:5
输入整数3:1
成品
toString:ArrayWorks [numberList = [10,5,1]]
尺寸:3
平均值:5.3333335
最大:10
min:1