1.计算三个班的平均分
import java.util.Scanner;
public class AvgScore{
public static void main (Sting[] args){
int[] score = new int[]; //成绩数组
int classNum = 3; //班级数量
double sun = 0.0; //成绩总和
double[] average = new double[classNum];//平均成绩数组
//循环输入学员成绩
Scanner input = new Scanner(System.in);
for(int i =0; i<classNum; i++){ //外层循环控制班级
sum = 0.0 //成绩总和归零
System.out.println("请输入"+(i+1)+"个学员的成绩:");
for(int j=0; j<score.length; j++){ //内层循环控制每个班的参赛人数
System.out.print("第"+(j+1)+"个学员的成绩:");
score[j] = input.nextInt();
sum = sum+score[j];//成绩累加
}
average[i] = sum/score.length;//平均分计算
System.out.println("第"+(i+1)+"个班级参赛学员的平均分是:"+average[i]+" ");
}
}
}
2.打印矩形代码样例
public class RTriAngle{
public satic void main(String[] args){
System.out.println("打印矩形");
for(int i = 1; i<= 5; i++){//打印第i行
for(int j =1; j<=5;j++){//打印五个*号
System.out.print("*");
}
System.out.println(" "); //换行
}
}
}
3.冒泡排序
Package cn.jbit.ifdemo;
import java.util.Scanner;
/*
*冒泡排序5名学生成绩
**/
public class SortScore{
public static void main (String[] args){
int[] scores=new int[5];
Scanner input=new Scanner(System.in)
System.out.println("请输入5名学生的成绩:");
//获取学生成绩
for(int i = 0; i<scores.length;i++){
System.out.pint("请输入第"+(i+1)+"个学生成绩:") ;
scores[i]=input.nextInt();
}
//冒泡排序学员成绩
for (int i=0; i<scores.length-1; i++){
for(int j=0; j<score.length-1-i; j++){
if(scores[j]<scores[j+1]){
int temp=scores[j];
scores[j]=scores[j+1];
scores[j+i] =temp;
}
}
}
System.out.println("排序后的成绩为:");
for(int score:scores){
System.out.print(score+" ");
}
}
}