第 8章 循环结构进阶 课后作业:

 
 
1.
 import java.util.Scanner;
public class Forxh {
	public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("请输入你的数字:");
       int rows = input.nextInt();
       for (int i = 1; i <=rows; i++) {
		for (int j = 1; j <=i; j++) {
			System.out.print(j+" ");
		}
		System.out.println(" ");
	}
	}
}
 
  
2.
  import java.util.Scanner;
public class Forxh2 {
public static void main(String[] args) {
        int [][] score = new int [3][4]; //定义班级数和人数
        int total = 0;//定义85分以上的平均分
        double sum = 0.0;//平均分
         Scanner input = new Scanner(System.in);
         for (int i = 0; i < score.length; i++) {
         System.out.println("请输入第"+(i+1)+"班级");
         for (int j = 0; j < score[i].length; j++) {
         System.out.print("第"+(j+1)+"个学员成绩:");
         score[i][j] = input.nextInt();
         if(score[i][j]>=85) {
         total++;
         sum+=score[i][j];
         continue;
       }
   }
         }
         System.out.println("大于85分的人数为:"+total);
         System.out.println("85分成绩平均分是:"+sum/(total*1.0));
   }

}

3.百钱买百鸡:
   public class Forxh1 {
public static void main(String[] args) {
int i,j,k;
for ( i= 0; i <= 20; i++) {  //公鸡数
for ( j = 0; j <= 30; j++) { //母鸡数
for ( k = 0; k <=100; k++) {  //雏鸡数
if(5*i+3*j+k/3==100 && i+j+k==100) {
System.out.println("公鸡有:"+i+"\n"+"母鸡有:"+j+"\n"+"雏鸡有:"+k);
System.out.println(" ");
}
    }
       }
    }
        }
}
4.Atm机取款:
import java.util.Scanner;

public class Atm {

	public static void main(String[] args) {
		int password;
		int money;
		Scanner input = new Scanner(System.in);
		for (int i = 0; i < 3; i++) {
		System.out.print("请输入你的密码:");
		password = input.nextInt();
		if (password == 111111) {
		System.out.print("请输入金额:");
		money = input.nextInt();
		while( (money > 0 && money < 1000 && money % 100 == 0)==false)  {
		System.out.print("您输入金额的不合法,请重新输入:");
		money = input.nextInt();
		}
		System.out.println("您取出了" + money + "元\n");
		break;
		} else {
		if (i == 2) {
		System.out.println("您已输错3次,请输入正确密码!");
		}
		continue;
			}
		}
		System.out.println("交易完成,请取卡!");
	}
}

5.输入行数,打印菱形:
public class Lx {
 public static void main(String[ ] args ) {
     Scanner input = new Scanner(System.in);
     System.out.print("请输入菱形行数:");
     int rows = input.nextInt();
     while(rows%2==0) {
     System.out.print("请输入奇数:");
          rows= input.nextInt();
     }
     int num = (rows+1)/2;
     for (int i = 1; i <= num; i++) {
     for (int j = 0; j < num-i; j++) {
      System.out.print(" ");
   }
    for (int k =0; k < 2*i-1; k++) {
    System.out.print("*");
   }
    System.out.println("");
  }
     int num1 = rows-num;
     for (int i = 1; i <= num1; i++) {
     for (int j =1; j <= i; j++) {
     System.out.print(" ");
   }
     for (int j = 0; j < rows-2*i; j++) {
     System.out.print("*");
   }
     System.out.println("");
     }
  }
}
}




猜你喜欢

转载自blog.csdn.net/gz98411/article/details/79857334
今日推荐