创建一个集合,保存内容为学生成绩(数字),创建2个线程,一个线程循环等待输入成绩并保存到集合,直到输入分数为0则停止输入并完成,
另一线程每10秒用System.out输出一次目前所有学生的平均分

解决方案 »

  1.   

       LZ 都描述的这么清楚了  说明你思路是清晰的
        线程1  获取控制台输入的数字,如输入的数字不为0 则保存到集合中  若为0 则线程结束
               用Scanner scan=new Scanner(System.in);
        线程2  就是把集合中的数字平均下并输出  此方法10s执行一次 (类似定时器) 
       思路就是这样的,具体代码LZ 自己写吧  
           
      

  2.   

    思路 我也明白 就是写的时候不会
    main建个集合 线程1不知道怎么对它操作了
    求解
      

  3.   

    你就把你的线程run方法里面当做另外一个main程序来写,功能就是你描述的那个
      

  4.   

    public class Test implements Runnable {
    public List<Integer> l;
    public Boolean notFinish = true;
    public Test(){
    l = new ArrayList<Integer>();
    }

    @Override
    public void run() {
    while (true){
    try {
    Thread.sleep(10000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    synchronized(this){
    if (l.size()>0){
    int sum = 0;
    for (Integer i:l){
    sum = sum + i;
    }
    System.out.println(sum / l.size());
    }
    if (!notFinish) break;
    }
    }
    }

    public static void main(String[] args){
    Test test = new Test();
    Thread output = new Thread(test);
    output.start();
    Scanner s=new Scanner(System.in);
    int i;
    while ( ( i = Integer.parseInt(s.next())) != 0){
    synchronized(test){
    test.l.add(i);
    }
    }
    synchronized(test){
    test.notFinish = false;
    }
    }
    }
      

  5.   

    public class Test {

    public static void main(String[] args) {

    List<Integer> scoreList=new ArrayList<Integer>();
    new Thread (new T1(scoreList)).start();
    int j=80;
    while(scoreList.size()==0||scoreList.get(scoreList.size()-1)!=0){
    scoreList.add(j);
    j=j-5;
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }


    }}
    public class T1 implements Runnable {

    private List<Integer> sorceList;

    public T1(List<Integer> l){
    this.sorceList=l;
    } @Override
    public void run() {

    while(true){
    System.out.println("平均分:"+getAvgScore());
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

    public int  getAvgScore(){
    int total=0;
    if(this.sorceList.size()!=0){
    for(Integer score:this.sorceList){
    total+=score;
    }
    return (total/this.sorceList.size());
    }
    return 0;

    }大概代码是这样的 自己再稍微修改下!