java流程控制——30题练习(16-20题)

16、已知学生成绩以100分为满分,共分5个等级:A,B,C,D,E
        90~100为等级A,
        80~89为等级B,
        70~79为等级C,
        60~69为等级D,
         0~59为等级E。
        要求定义一个成绩变量,当成绩变化时,可直接知道该成绩对应的等级。
        例如:当成绩为100时,该学生的等级是A。

import java.util.Scanner;
public class Exer16 {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入学生的成绩:");
		int score = scan.nextInt();
		
		if(90 <= score && score <= 100){
			System.out.println("该学生成绩等级为:A");
		}else if(80 <= score && score <90){
			System.out.println("该学生成绩等级为:B");
		}else if(70 <= score && score <80){
			System.out.println("该学生成绩等级为:C");
		}else if(60 <= score && score <70){
			System.out.println("该学生成绩等级为:D");
		}else{
			System.out.println("该学生成绩等级为:E");
		}
	}

}

17、打印自然数的个数
        1)打印1~100之间 6的倍数的个数
        2)求出1~100之间,既是3又是7的倍数的自然数出现的次数?

public class Exer17 {
	public static void main(String[] args){
		int count1 = 0;//对6倍数个数计数
		int count2 = 0;//对3和7的倍数个数计数
		
		for(int i = 1;i <= 100;i++){
			
			if(i % 6 ==0){
				count1 +=1;
			}
			if(i % 3 == 0 && i % 7 == 0){
				count2 +=1;
			}
		}
		System.out.println("6的倍数的个数为:"+ count1);
		System.out.println("3和7的倍数的个数为:"+ count2);
	}
}

18、求调和级数中从第多少项开始值大于10
        调和级数的第n项形式为:1+1/2+1/3+…+1/n

public class Exer18 {
	public static void main(String[] args){
		double sum = 0.0;//前n项和
		int i = 1;//分母值
		int count = 0;
//方法一
		do{
			sum += 1.0 / i;
			count += 1;
			i++;
		}while(sum < 10);
		System.out.println("第"+ count +"项开始值大于10");
		
//方法二
//		while(true){
//			sum += 1.0 / i;
//			if(sum > 10){
//				break;
//			}
//			i++;
//		}
//		
//		System.out.println("第"+ i +"项开始值大于10");
	
	}

}

19、打印如下的图形:三角形

   *
   * * *
   * * * * *
   * * * * * * *
   * * * * *
   * * *
   *

public class Exer19 {
	public static void main(String[] args){
		//方法一:分为两部分就行输出
		//上半部(升):注意内外层的关系
		for(int i = 1;i <= 4;i++){
			for(int j = 1;j < 2 * i;j++){
				System.out.print("*");
			}
			System.out.println();
		}
		
		//下半部(降)
		for(int i = 1;i <= 3;i++){
			for(int j = 1;j <= 7 - 2 * i;j++){
				System.out.print("*");
			}
			System.out.println();
		}
	System.out.println();
	
		//方法二:用一个for循环输出
		for(int i = 0;i < 7;i++){
			if(i < 4){
				for(int j = 0;j < 2 * i + 1;j++){
					System.out.print("*");
				}
				System.out.println();
			}else{
				for(int k = 0;k < 13 - 2 * i;k++){
					System.out.print("*");
				}
				System.out.println();
			}
		}
	}

}

20、 打印如下的图形:菱形1
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

public class Exer20 {
	public static void main(String[] args){
		//上部分
		for(int i = 1;i < 6;i++){
			for(int j = 1;j < 6 - i;j++){
				System.out.print(" ");
			}
			for(int k = 1;k < i + 1;k++){
				System.out.print("* ");
			}
			System.out.println();
		}
		
		//下部分
		for(int i = 1;i < 5;i++){
			for(int j = 1;j < i + 1;j++){
				System.out.print(" ");
			}
			for(int k = 1;k <= 5 - i;k++){
				System.out.print("* ");
			}
			System.out.println();
		}
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MoerPerfect

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值