Java–流程控制
package com.zy.base;
import java.util.Scanner;
/**
*description: 流程结构
*@program: 基础语法
*@author: zy
*@create: 2023-02-12 21:31
*/
public class Struct {
public static void main(String[] args) {
// 创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
ifElse(scanner);
switchUse();
whileLoop();
doWhileLoop();
forLoop();
test(scanner);
// 关闭IO流对象,避免消耗内存
scanner.close();
}
/**
* @Description ifElse的使用
* @author zy
* [scanner]
* void
* @date 2023-2-12 20:55
*/
public static void ifElse(Scanner scanner){
/*
if 语句至多有一个 else 语句,else 语句在所有的 else if 语句之后;
if 语句可以有若干个 else if 语句,但它们必须在 else 语句之前;
一旦其中一个 else if 语句检测为 true,其它的 else if 以及 else 语句都将跳过执行
*/
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("满分!");
}else if(score<100 && score>=90){
System.out.println("A级");
}else if(score<90 && score>=80){
System.out.println("B级");
}else if(score<80 && score>=70){
System.out.println("C级");
}else if(score<70 && score>=60){
System.out.println("D级");
}else if(score<60 && score>=0){
System.out.println("不及格");
}else{
System.out.println("成绩不合法!");
}
}
/**
* @Description switch用法
* @author zy
* []
* void
* @date 2023-2-12 21:55
*/
public static void switchUse(){
String name = "张三";
// jdk7 新特性:表达式结果可以是字符串
switch (name){
case "张三":
System.out.println("张三");
break;
case "李四":
System.out.println("李四");
break;
default:
System.out.println("未知");
}
/* 反编译结果分析:
String name = "张三";
byte var2 = -1;
switch(name.hashCode()) {
case 774889:
if (name.equals("张三")) {
var2 = 0;
}
break;
case 842061:
if (name.equals("李四")) {
var2 = 1;
}
}
switch(var2) {
case 0:
System.out.println("张三");
break;
case 1:
System.out.println("李四");
break;
default:
System.out.println("未知");
}
*/
}
/**
* @Description while用法
* @author zy
* []
* void
* @date 2023-2-12 22:15
*/
public static void whileLoop(){
// 计算1+2+3+...+100=?
int i=0;
int sum = 0;
while (i<100){
i++;
sum+=i;
}
System.out.println("1+2+3+...+100="+sum);
}
/**
* @Description doWhile用法
* @author zy
* []
* void
* @date 2023-2-12 22:20
*/
public static void doWhileLoop(){
// 计算1+2+3+...+100=?
int i=0;
int sum = 0;
do {
i++;
sum+=i;
}while (i<100);
System.out.println("1+2+3+...+100="+sum);
/*
while:
先判断后执行,不满足则不进入循环
doWhile:
先执行后判断,总是会执行至少一次
*/
int a = 0;
while (a<0){
System.out.println(a);// 不输出
}
System.out.println("=======================");
do {
System.out.println(a);// 输出一次
}while (a<0);
}
/**
* @Description for循环用法
* @author zy
* []
* void
* @date 2023-2-13 21:48
*/
public static void forLoop(){
/*
关于for循环的几点说明:
最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后检测布尔表达式的值,为true,循环体被执行。为false,循环终止。
执行一次循环后,更新循环控制变量;再次检测布尔表达式,循环执行上面的过程。
死循环写法如下:
for(;;){
}
*/
// 练习1:计算0~100之间奇数的和、偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if(i%2==0){
evenSum+=i;
}else{
oddSum+=i;
}
}
System.out.println("0~100之间奇数的和:"+oddSum);
System.out.println("0~100之间偶数的和:"+evenSum);
// 练习2:输出1~1000之间能被5整除的数,并且每行输出3个
for (int i = 0; i < 1000; i++) {
if(i%5 == 0){
System.out.print(i+"\t");
}
if(i%5*3 == 0){
System.out.println();
}
}
// 练习3:打印九九乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
// 增强for循环
int[] numbers = {1,2,3,4,5,6,7,8,9};
// 遍历数组元素
for(int number:numbers){
System.out.println(number);
}
// break/continue 用法
for (int i = 0; i < 100; i++) {
if(i == 50){
// 终止循环,之后未执行的语句将不再执行且不再进入下一次循环
break;
}
if(i%2 == 0){
// 跳过本次循环,之后未执行的语句将不再执行进入下一次循环
continue;
}
System.out.println(i);
}
// 带标签的break和continue,可终止循环至标签的地方;不建议使用。
// 打印101~150之间所有的质数
out:for (int i=101;i<150;i++){
for(int j=2;j<i/2;j++){
if(i%j == 0){
continue out;
}
}
System.out.print(i+"\t");
}
}
/**
* @Description 流程控制练习:打印三角形
* @author zy
* [scanner]
* void
* @date 2023-2-13 22:17
*/
public static void test(Scanner scanner){
// 打印三角形
System.out.println("\n请输入需要打印的三角形行数,至少5行:");
int row = 0;
if(scanner.hasNextInt()){
row = scanner.nextInt();
}else{
System.out.println("输入的不是数字,无法打印!");
}
if(row>=5){
for(int i=1;i<=row;i++){
for (int j=row;j>=i;j--){
System.out.print(" ");
}
for(int j=1;j<=i;j++){
System.out.print("*");
}
for(int j=1;j<i;j++){
System.out.print("*");
}
for (int j=row;j>=i;j--){
System.out.print(" ");
}
System.out.println();
}
}
}
}