日期:2014-05-20  浏览次数:20702 次

请教一下方法保存进数组的方法?
创建方法:有一个0~100的int型输入,然后保存进成绩数组,返回值void。
每运行一次考试方法应该保存进数组的下一个元素,若考试超过5次或输入不正确println相应信息。
(可以用一个private成员变量保存当前考试数)

请问怎么用什么方法放进数组里去

------解决方案--------------------
是这样吧:
public class Test{
private int count;
public void exame(int record,int[] arr){
count++;
if(count<=5&&record<=100&&record>0){
arr=Arrays.copyOf(arr, arr.length+1);
arr[arr.length-1]=record;
System.out.println(Arrays.toString(arr));
}else{
System.out.println("错误");
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int[] arr={};
Test t=new Test();
while(true){
int record=sc.nextInt();
t.exame(record, arr);
}
}
}