62,616
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int num;
if((num=sc.nextInt())==0)
return;
int sum = 0;
for(int i =0 ;i<num;i++){
sum += sc.nextInt();
}
System.out.printf("%.2f",1.0*sum/num);
System.out.println();
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 结束标识设为-1,不能是0,因为有的分数可能为0,输入0就结束了
* Sample Input
2 100 0
3 90 80 70
-1
Sample Output
50.00
80.00
* @author
*
* 2017-9-21
*/
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Sample Input:");
List<Integer> list = new ArrayList<Integer>();
while(sc.hasNextInt()){
int d = sc.nextInt();
if(d!=-1){
list.add(d);
}else{
break;
}
}
System.out.println("Sample Output:");
int n = list.get(0);//list里面第一个数就是第一个班级的人数
printScore(list, n);
}
public static void printScore(List<Integer> list, int n){
int sum = 0;
list.remove(0);//移除第一个班级的人数,接下来前n个数就是第一个班级的分数
for (int i = 0; i < n; i++) {
sum += list.get(0);
list.remove(0);//每次取出一个数,就把这个数删掉
}
System.out.printf("%.2f",1.0*sum/n);//循环一次结束后,接着list的第一个元素就是第二个班级的人数,递归调用
System.out.println();
while(list.size()>0){
printScore(list, list.get(0));
}
}
}
运行结果:
Sample Input:
2 100 0
3 90 80 70
-1
Sample Output:
50.00
80.00
public static void main(String[] args) {
System.out.println("input :");
boolean flag = true;
while(flag){
Scanner input = new Scanner(System.in);//初始化
int num = input.nextInt();
}
}