}
ave=sum/12;
System.out.println(ave); for(int i=0;i<12;i++) {
rainde[i]=rains[i]-ave; }
for(double ra:rainde) {
System.out.println(ra); } }
------------------------------------------------------------------------------------------
假定5个学生参加了三门课的考试,试统计出全部学生的最高分和最低分,以及每个学生的平均成绩,并输 出。
public class Student {
public static void main(String[] args) {
double[][] score={{95,85,99},{99,87,77},{88,100,96},{77,88,99},{77,77,66}}; double max=score[0][0],min=score[0][0]; double aveScore[]={0,0,0,0,0}; for(int i=0;i double aveSum=0; for(int j=0;j if(score[i][j]>max) max=score[i][j]; if(score[i][j] aveScore[i]=aveSum/3; System.out.println(\第\个学生的平均成绩为:\} System.out.println(\最大的分数是:\System.out.println(\最小的分数是:\} } ------------------------------------------------------------------------------------------ 编写程序,生成100个{0,9}之间的整数,输出每个数的出现次数,比如0出现了10次,1出现课5次,等等 提示:Math.random()返回一个double值,该值大于等于0.0、小于1.0 方法一: public class CalRan { public static void main(String[]args) { int[] ra=new int[100]; int[] count=new int[10]; for(int i=0;i<100;i++) { ra[i]=(int)(Math.random()*10); } for(int i=0;i<100;i++) { for(int j=0;j<10;j++) { if(ra[i]==j) count[j]++; } } for(int i=0;i<10;i++) { System.out.println(i+\的个数是:\} } } 方法二: public class RandomTest { public static void main(String[]args) { int[] tj = new int[9]; for(int i=0;i<100;i++) { int r=(int)(Math.random()*9)+1; switch(r) { case 1:tj[0]++; break; case 2:tj[1]++; break; case 3:tj[2]++; break; case 4:tj[3]++; break; case 5:tj[4]++; break; case 6:tj[5]++; break; case 7:tj[6]++; break; case 8:tj[7]++; break; case 9:tj[8]++; break; } } for(int i=0;i<9;i++) { System.out.println(\} } } ------------------------------------------------------------------------------------------ 7.9 上机实践 第①题: public class Change { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); String s=\char[] c=s.toCharArray(); for(int i=0;i if(c[i]>=97) { sb.append((c[i]+\} else { sb.append((c[i]+\} } System.out.println(sb.toString()); } } 第②题: public class Test99 { public static void main(String[] args) { String[] strs={\int num1=0,num2=0,p=strs.length; for(int i=0;i boolean flag1=strs[i].startsWith(\ boolean flag2=strs[i].endsWith(\ if(flag1) num1++; if(flag2) num2++; } System.out.println(\以st开头的字符串有\个.以ng结尾的字符串有\个\} } ------------------------------------------------------------------------------------------ 编写应用程序,完成两个数相除,并把相除的结果输出。通过JOptionpane.showInputDialog() 输入字符串,把字符串通过Integer.parseInt()转换成整形数给除数变量,如果转换过程中有异常 通过NumberForamtException捕获,如果数据为0,通过ArithmeticException捕获 public class Erci { public Exci { } public static void main(String[] args) { int div1=23; int div2; try { div2=integer.parseInt(JOptionpane.showInputDialog() 这里未完 int div3=div1 div2; System.out.println(div3); } catch(NumberForamtException.e) { System.out.println(\你输入的是一个整形数\} catch(ArithmeticException.e) { System.out.println(\你输入的除数是0\} } } (未完待续) ------------------------------------------------------------------------------------------ 8.6 第一题: import java.util.Scanner; public class NumberRangeExceptionTest { static int n=5; //n为需要录入的学生人数 static Student stu[] = new Student[n]; static { stu[0] = new Student(\ //此处初始化学生信息用,这里的A0?? 是学生名字 stu[1] = new Student(\stu[2] = new Student(\stu[3] = new Student(\stu[4] = new Student(\} public static void main(String[] agrs) //截获stu[i].setScore(method1());的异常 //之所以把所有的都写在try catch里,是由 于如果写在外面,就算截获了异常,程序依 然会顺序执行下去//如果不try catch 而直接 throws 的话,那么异常会直接的抛给JVM处理 { try { for(int i=0;i System.out.print(\请输入\的数学成绩:\stu[i].setScore(method1());