05、顺序、选择、循环结构练习题

1. 展示0 ~ 100以内的所有偶数
//1. 展示0 ~ 100以内的所有偶数
public class Test1 {
	public static void main(String[] args) {
		
		int count = 0;//计数器
		
		System.out.println("0 ~ 100以内的所有偶数:");
		
		/*
		循环101次
		for (int i = 0; i <= 100; i++) {
			if (i % 2 ==0) {
				
				count++;
				
				System.out.print(i+"\t");				
				
				if (count % 6 == 0) {
					System.out.println();
				}
				
			}
		}
		*/
		
		//等差数列计算 差值为2 循环51次
		for (int i = 0; i <= 100; i += 2) {
			System.out.print(i+"\t");
			
			count++;
			
			//输出6个数之后换行
			if (count % 6 == 0) {
				System.out.println();
			}			
		}
	}	
}

运行结果如下图
0~100所有的偶数

2. 展示0 ~ 100以内的所有奇数
//2. 展示0 ~ 100以内的所有奇数
public class Test2 {
	public static void main(String[] args) {
		
		int count = 0;//计数器
		
		System.out.println("0 ~ 100以内的所有奇数:");
		
		/*
		循环101次
		for (int i = 0; i <= 100; i++) {
			if (i % 3 ==0) {
				
				count++;
				
				System.out.print(i+"\t");				
				
				if (count % 6 == 0) {
					System.out.println();
				}
				
			}
		}
		*/
		
		//等差数列计算 循环50次
		for (int i = 1; i < 100; i += 2) {
			System.out.print(i+"\t");
			
			count++;
			
			//输出6个数之后换行
			if (count % 6 == 0) {
				System.out.println();
			}
		}
	}
}

运行结果如下图
0~100所有的奇数

3. 计算1 - 150 的和
//3. 计算1 - 150 的和
public class Test3 {
	public static void main(String[] args) {
		int sum = 1;
		
		//数据处理
		for (int i = 2; i <= 150; i++) {
			sum += i;
		}
		
		//数据展示
		System.out.println("1 ~ 150 的和"+sum);
	}
}

运行结果如下图
1~150的和

4. 逢7过!!! 【100以内的】
//4. 逢7过!!! 【100以内的】
public class Test4 {
	public static void main(String[] args) {
				
		System.out.println("100以内逢7过开始:");
		
		for (int i = 1; i <= 100; i++) {
			/*
			逢7过
			1、7的倍数
			2、含有7的数
			*/
			if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {
				System.out.println("过");;
			} else {
				System.out.print(i+" ");				
			}
		}
	}
}

运行结果如下图
逢7过

5. 多种方法打印26个小写字母
//5. 多种方法打印26个小写字母
public class Test5 {
	public static void main(String[] args) {
		
		char ch = 'a';
		
		//第一种方法
		for (; ch <= 'z'; ch++) {
			System.out.printf("%c ",ch);
		}
		
		System.out.println();
		
		//第二种方法
		ch = 'a';
		
		while (ch <= 'z') {
			System.out.printf("%c ",ch);
			ch++;
		}
		
		System.out.println();
		
		//第三种方法
		ch = 'a';
		
		do {
			System.out.printf("%c ",ch);
			ch++;
		} while (ch <= 'z');
	}
}

运行结果如下图
多种方法打印小写字母

6. 例如:
	输入 5  8;
	计算 5 + 6 + 7 + 8;
	输出 26.
/*例如:
输入 5 8;
计算 5 + 6 + 7 + 8;
输出 26.
*/
import java.util.Scanner;

public class Test6 {
	
	public static void main(String[] args) {
		int sum = 0;
	
		Scanner sc = new Scanner(System.in);
	
		System.out.println("请输入第一个数:");
	
		int num1 = sc.nextInt();
	
		System.out.println("请输入第二个数:");
	
		int num2 = sc.nextInt();
			
		//第一种方法
		//对键入的两个整数判断大小
		if (num1 > num2) {
			/*
			定义一个中间变量
			int temp = num1;
			num1 = num2;
			num2 = temp;
			*/
			
			//省空间
			num1 = num1 + num2;
			num2 = num1 - num2;
			num1 = num1 - num2;
		}
		for (int i = num1; i <= num2; i++) {
			sum += i;
		}
	
		System.out.println(sum);
		
		
		//第二种方法
		if (num1 > num2) {
			System.out.println((num2 + num1) * (num1 - num2 + 1) / 2);
		} else if (num2 > num1) {
			System.out.println((num1 + num2) * (num2 - num1 + 1) / 2);
		} else {
			System.out.println(num1);
		}
	}

}

运行结果如下图
求和

7. 整数逆序输出, 例如输入一个整数12345,输出5 4 3 2 1
//7. 整数逆序输出, 例如输入一个整数12345,输出5 4 3 2 1
import java.util.Scanner;

public class Test7 {
	public static void main(String[] args) {
		
		int remainder = 0;
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入一个整数:");
		
		int num = sc.nextInt();
		
		System.out.printf("%d逆序输出结果为:",num);
		
		//考虑到整数中可能存在数字0的情况 所以num = 0不能作为判断条件
		while (num > 0) {
			
			remainder = num % 10;//对10取余,得到最后一位数 例:12345 % 10 == 5
			
			System.out.print(remainder+" ");//输出余数
			
			num = num / 10;//整除10 去掉最后一位数 例:12345 / 10 == 1234
		}
	}
}

运行结果如下图
整数逆序输出

8. 
   *****
   *****
   *****
   *****
   *****
/*
8.
*****
*****
*****
*****
*****
*/
public class Test8 {
	public static void main(String[] args) {
		
		int line = 5;//定义一个变量表示层数
		
		char ch = '*';
		
		//外层循环控制层数
		for (int i = 0; i < line; i++) {
			//内层循环控制每一层*的个数
			for (int j = 0; j < line; j++) {
				System.out.print(ch);
			}
			System.out.println();
		}
	}
}

运行结果如下图
矩阵输出

9.
  *
  **
  ***
  ****
  *****
/*
9.
*
**
***
****
*****
*/
public class Test9 {
	public static void main(String[] args) {
		
		int line = 5;//定义一个变量表示行数
		
		char ch = '*';
		
		//外层循环控制层数
		for (int i = 0; i < line; i++) {
			//内层循环控制每一层*的个数 *的个数等于当前的层数
			for (int j = 0; j <= i; j++) {
				System.out.print(ch);
			}
			System.out.println();
		}
	}
}

运行结果如下图
正三角输出

10.   
      *
     ***
    *****
   *******
  *********
/*
10.
    *
   ***
  *****
 *******
*********
*/
public class Test10 {
	public static void main(String[] args) {
		
		char ch1 = ' ';
		char ch2 = '*';
		int row = 5;//总行数
		
		//外层循环控制行数
		for (int i = 1; i <= row; i++) {
			
			//打印左空格 每行的空格数 + 当前行数 = 总行数
			for (int j = 1; j <= row - i; j++) {
				System.out.print(ch1);
			}
			//打印* 
			for (int k = 1; k <= 2 * i - 1; k++) {
				System.out.print(ch2);
			}
			System.out.println();
		}
		
	}
}

运行结果如下图
*金字塔

11.【拔高题】
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
/*
11.【拔高题】  row 空格 * 
    *           1   4   1
   ***          2   3   3
  *****         3   2   5
 *******        4   1   7
*********       5   0   9
 *******        6   1   7
  *****         7   2   5
   ***          8   3   3
    *           9   4   1

空格数 = Math.abs((row + 1) / 2 - i)
* = row - 2 * Math.abs((row + 1) / 2 - i)
*/
public class Test11 {
	public static void main(String[] args) {
		char ch1 = ' ';
		char ch2 = '*';
		
		int row = 9;
		
		//第一种方法
		//正金字塔
		for (int i = 1; i <= (row + 1) / 2; i++) {
			//打印空格
			for (int j = 1; j <= (row + 1) / 2 - i; j++) {
				System.out.print(ch1);
			}
			
			//打印*
			for (int k = 1; k <= 2 * i - 1; k++) {
				System.out.print(ch2);
			}
			System.out.println();
		}
		
		//倒金字塔
		for (int i = row / 2; i >= 1; i--) {
			//打印空格
			for (int j = 1; j <= row / 2 - i + 1; j++) {
				System.out.print(ch1);
			}
			
			//打印*
			for (int k = 1; k <= 2 * i - 1; k++) {
				System.out.print(ch2);
			}
			System.out.println();
		}
		System.out.println("--------------------");
		
		for (int i = 1; i <= row; i++) {
			//打印空格
			for (int j = 1; j <= Math.abs((row + 1) / 2 - i); j++) {
				System.out.print(ch1);
			}
			
			//打印*
			for (int k = 1; k <= row - 2 * Math.abs((row + 1) / 2 - i); k++) {
				System.out.print(ch2);
			}
			System.out.println();
		}
		
	}
}

运行结果如下图
雪花菱形

12.【拔高题】
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A
/*
12.【拔高题】
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A
*/
public class Test12 {
	public static void main(String[] args) {
		char ch1 = ' ';
		char ch2 = 'A';
		
		int row1 = 5;
		int row2 = 4;
		
		//正金字塔
		for (int i = 1; i <= row1; i++) {
			//打印空格
			for (int j = 1; j <= row1 - i; j++) {
				System.out.print(ch1);
			}
			
			//正序
			for (int k = 1; k <= i; k++) {
				System.out.print(ch2);
				if (i != 1 && k != i) {
					ch2++;
				}
			}
			
			//逆序
			for (int m = 1; m < i; m++) {
				ch2--;
				System.out.print(ch2);
			}
			System.out.println();
		}
		
		//倒金字塔
		for (int i = row2; i >= 1; i--) {
			//打印空格
			for (int j = 1; j <= row2 - i + 1; j++) {
				System.out.print(ch1);
			}
			
			//正序
			for (int k = 1; k <= i; k++) {
				System.out.print(ch2);
				if (i != 1 && k != i) {
					ch2++;
				}
			}
			
			//逆序
			for (int m = 1; m < i; m++) {
				ch2--;
				System.out.print(ch2);
			}
			System.out.println();
		}
	}
}

运行结果如下图
字母菱形

13.【拔高题】
   ###*###
   ##*#*##
   #*###*#
   *#####*
   #*###*#
   ##*#*##
   ###*###
/*
13.【拔高题】    i 偏移量
   ###*###       1   0
   ##*#*##       2   1
   #*###*#       3   2
   *#####*       4   3
   #*###*#       5   2 
   ##*#*##       6   1
   ###*###       7   0
   
   
   (row + 1) / 2
*/
public class Test13 {
	public static void main(String[] args) {
		char ch1 = '#';
		char ch2 = '*';
		
		int row = 7;
		
		for (int i = 1; i <= (row + 1) / 2; i++) {
			for (int j = 1; j <= row; j++) {
				//判断是否到*的位置 如果是打印*
				if (j == (row + 1) / 2 - i + 1 || j == (row + 1) / 2 + i - 1) {
					System.out.print(ch2);
				} else {
					System.out.print(ch1);
				}
			}
			System.out.println();
		}
		
		for (int i = row / 2; i >= 1; i--) {
			for (int j = 1; j <= row; j++) {
				//判断是否到*的显示位置
				if (j == row / 2 - i + 2 || j == row / 2 + i) {
					System.out.print(ch2);
				} else {
					System.out.print(ch1);
				}
			}
			System.out.println();
		}
		
		System.out.println("-------------------------");
		
		for (int i = 1; i <= row; i++) {
			for (int j = 1; j <= row; j++) {
				//偏移量 Math.abs((row + 1) / 2 - j)
				// 当前行的偏移量 row / 2 - Math.abs((row + 1) / 2 - i)
				if (Math.abs((row + 1) / 2 - j) == row / 2 - Math.abs((row + 1) / 2 - i)) {
					System.out.print(ch2);
				} else {
					System.out.print(ch1);
				}
			}
			
			System.out.println();
		}
		
	}
}

运行结果如下图
矩形菱形

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值