多重循环控制练习
作者:互联网
多重循环控制
练习题1:
//要求: //统计三个班成绩情况,每个班5个人, 求出各个班的平均分 和 所有班级平均分 (成绩从键盘输入)并且统计所有及格的同学
import java.util.Scanner; public class ForDemo10 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double allScore = 0;//定义3个班总分 int student = 0; for (int classRoom = 1;classRoom <= 3;classRoom++) { double sum = 0;//一个班级的总分 for (int persons = 1; persons <= 5; persons++) { System.out.println("请输入第" + classRoom + "个班第" + persons + "个学生的成绩:"); double score = scanner.nextDouble(); sum += score; System.out.println("第" + persons + "个学生的成绩为:" + score); if (score>=60){student++;} } System.out.println("第"+ classRoom + "个班的总分" + sum + "平均分=" + (sum / 5)); allScore +=sum; } System.out.println("所有班级的总分="+allScore+"所有班的平均分="+(allScore/3)); System.out.println("所有班级中,及格的同学有:"+ student +"个"); } }
练习题2:打印9*9乘法表:
for (int verNum = 1;verNum <= 9;verNum++){ for (int tranNum = 1;tranNum <= verNum;tranNum++){ System.out.print(tranNum + "*" + verNum + "=" + (tranNum * verNum)); System.out.print(" "); } System.out.println("\t"); }
以列和排数命名。
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
详解:
-
定义排数verNum=1;定义列数stranNum=1.
-
分析每一排第一排,第一排的乘数不会大于当前排,所以tranNum <= verNum.
-
打印内循环体,使用print不换行加空格,退出循环体
-
println换行使用,然后继续。
标签:classRoom,多重,Scanner,练习,System,循环,println,allScore,平均分 来源: https://blog.csdn.net/m0_55112422/article/details/120250735