第八章:循环结构进阶

第一题:编写程序,打印如下图案,要求该图案的行数由用户输入。

import java.util.Scanner;


public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
    System.out.print("请输入行数:");  
    int lineNumber  = input.nextInt(); 
    for (int i = 1; i <= lineNumber; i++) {  
        for (int j = 1; j <= i; j++) {   
            System.out.print(j+"  ");  
        }  
        System.out.println("");  

}


}

第二题:买鸡问题

public class Test1 {

public static void main(String[] args) {
for (int i = 0; i <20; i++) {  //循环公鸡的数量  
     for (int j = 0; j < 33; j++) {   //循环母鸡的数量  
      for (int j2 = 0; j2 < 300; j2++) {  //循环小鸡的数量  
        if (((5*i)+(3*j)+(j2/3))==100 && (i+j+j2)==100) {  
        System.out.println("公鸡有:"+(i+1)+"\t"+"母鸡有:"+(j+1)+"\t"+"小鸡有:"+(j2+1));  
                   }  
               }  
           }  
       }  
}

}

第三题:有三个班级各四名学员参赛,从控制台输入每个班级参赛学员的成绩,要求统计出三个班级所有参赛学员中成绩大于85分的学员的平均分,如何编程实现?

import java.util.Scanner;


public class Test2 {
public static void main(String[] args) {
int [][] arr = new int [3][4]; //定义二维数组,分别接受3个班的每个班上的4位同学的成绩  
       int count = 0; //平均分大于85的人数  
       double avg = 0.0;//平均分大于85的同学的平均分  
       int totalScore = 0; //平均分大于85分的同学的总成绩  
       for (int i = 0; i < arr.length; i++) {  
           int sum = 0;//初始化成绩和  
           double avg1 = 0.0;//初始化平均分  
           System.out.println("第"+(i+1)+"个班的"+"成绩");  
           for (int j = 0; j < arr[i].length; j++) {  
            Scanner input = new Scanner(System.in);
               System.out.print("请输入第"+(j+1)+"位同学的成绩为:");
               if (input.hasNextInt() == true) {
               int score = input.nextInt();  
               arr[i][j] = score;  
               sum += arr[i][j];  
               if (score>=85) {  
                   count++;  
                   totalScore += arr[i][j];  
               }  
           }  
           avg = totalScore/(count*1.0);  
             
       }  
       System.out.println("成绩大于85分的学员的平均分的人数为:"+count);  
       System.out.println("成绩大于85分的学员的平均分为:"+avg);
}


}

}

第四题:银行取钱问题

import java.util.Scanner;


public class Test3 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in); 
       for (int i = 0; i < 3; i++) { 
           System.out.print("请输入密码:"); 
           int password =input.nextInt();  
           if (password == 123456) {  
               System.out.print("请输入金额:");  
               int money = input.nextInt();  
               while ((money % 100 == 0 && money > 0 && money <= 1000) == false) { // 循环判断用户输入的金额是否符合银行的出钱条件  
                   System.out.print("您输入的金额不合法,请重新输入:");  
                   money =input.nextInt();  
               }  
               System.out.print("您取出了" + money + "元\n");  
               break;   
           } else {    
               if (i == 2) { 
                   System.out.println("密码错误,请取卡!");  
               }  
               continue;  
           }  
       }  
       System.out.println("交易完成,请取卡!");  
}


}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值