问答题

在程序中,随机产生100个学生的成绩,并计算出他们的平均成绩。学生的成绩按照五级打分制,“A”表示4分、“B”表示3分、“C”表示2分、“D”表示1分、“E”表示0分。平均成绩用浮点数表示。请填写横线处的内容。
注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。
import java.math.*;
public class basic

public static void main(String[] args)

float aver=0;
int a=0, b=0, c=0, d=0, e=0, f=0;
for(______; i < 100; i++)

double sd= Math.random()*5+’A’;
char score=______;
______(score)

case‘A’: aver+=4;a++;break;
case‘B’: aver+=3;b++;break;
case‘C’: aver+=2;c++;break;
case‘D’: aver+=1;d++;break;
case‘E’: aver+=0;e++;break;
default: break;


aver/=100;
System.out.println("平均分数为"+aver+",学生人数100");
System.out.println("得A的学生有"+a+"人");
System.out.println("得B的学生有"+b+"人");
System.out.println("得C的学生有"+c+"人");
System.out.println("得D的学生有"+d+"人");
System.out.println("得E的学生有"+e+"人");

【参考答案】

int i=0
(char)sd
switch
热门 试题

问答题
本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入DataPool类中,消费者从中取出数据。DataPool类一次只能存放一个数据。请更正题中带下划线的部分。 注意:不改变程序的结构,不得增行或删行。 class DataPool private int data; private boolean isFull; public DataPool() isFull=false; public synchronized void putData(int d) if(isFull= =true) try this.notify(); catch(InterruptedException e) data=d; isFull=true; System.out.println( 生产了一个数据: +data); this.notify(); public synchronized int getData() if(isFull= =false) try this.wait(); catch(InterruptedException e) isFull=false; System.out.println( 消费了一个数据 +data); this.wait(); return this.data; boolean getIsFull() return isFull; class Producer extends Thread DataPool pool; public Producer(DataPool pool) this.pool=pool; public void run() for(int i=0; i<10; i++) int data=(int) (Math.random()*1000); try 用于生产数据 sleep(data); catch(InterruptedException e) pool.putData(data); class Consumer implements Runnable DataPool pool; public Consumer(DataPool pool) this.pool=pool; public void run() for(int i=0; i<10; i++) int data=pool.getData(); try 用于处理数据 sleep((int) (Math.random()*1000)); catch(InterruptedException e) public class advance public static void main(String[] args) Data Pool pool=new Data Pool(); Producer pro=new Producer(pool); Runnable con=new Consumer(pool); Thread conTh=new Thread(con); pro.start(); conTh.start();