Java语法基础四

本文详细介绍了结构化编程的基本思想和主要结构,包括条件分支(if、switch语句)和循环结构(while、for、do/while循环)。通过实例展示了如何使用这些结构处理学生性别判断、成绩评级和计算月份天数等问题,强调了程序设计的模块化和逐步求精原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结构化编程

结构化程序设计(Structured programming1960年代开始发展起来的一种编程典范。它采用子程序、程序码区块、for循环以及while循环等结构来取代传统的goto。

指导思想

自顶向下、逐步求精、模块化

结构化编程的推论

任何复杂的问题都可以三种基本算法结构来描述:顺序、选择、循环。因此用计算机语句描述的程序也包含三种基本结构。

条件分支语句

Java

语言提供了两种判断语句:if语句和switch语句。这两种语句用于实现判断,当符合某些条件时执行某段代码,否则将不执行

语法

1、if(条件判断语句){代码A;} 当条件成立时执行代码A,如果条件不成立则不执行代码A,而是直接执行if的下一句

2、if(条件){代码块1;}else{代码块2;} 当条件成立时执行代码块1,如果条件不成立则执行代码块2。没有一种情况是同时执行代码块12

3、if(条件1){代码块1;}else  if(条件2) {代码块2;}... else{代码块n;}    当条件1成立时执行代码块1,如果条件1不成立继续判断条件2,如果条件2成立则执行代码块2,依次类推。没有一种情况是同时执行代码块12

4、说明:允许多个if之间进行相互嵌套

需求:要求输入学生性别,如果false则显示女的

package com.yan2;

import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		// 要求输入学生性别,如果false则显示女的
		Scanner sc = new Scanner(System.in);
		boolean sex = sc.nextBoolean();
		if (sex == false) {
			System.out.println("性别为女");
		} else {
			System.out.println("性别为男");
        }
	}
}

需求:输入学生成绩,并进行评级

package com.yan2;

import java.util.Scanner;

public class Test3 {

	public static void main(String[] args) {
		// 要求输入学生成绩(假设成绩都是整数),
//		如果>=60显示及格,否则显示不及格
//		int score = 0;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生成绩:整数");
//		score = sc.nextInt();

		int score = sc.nextInt();

//		if (score >= 0 && score <= 100) {
//			if (score >= 60) {
//				System.out.println("及格");
//			} else {
//				System.out.println("不及格");
//			}
//		} else {
//			System.out.println("您输入的成绩不合法!");
//		}

//		if (score < 0 || score > 100) {
//			System.out.println("您输入的成绩不合法!");
//		} else {
//			if (score >= 60) {
//				System.out.println("合格");
//			} else {
//				System.out.println("不及格");
//			}
//		}

		if (score < 0 || score > 100) {
			System.out.println("您输入的成绩不合法");
		} else {
			if (score >= 95) {
				System.out.println("你是最棒的!!!");
			} else if (score >= 85) {
				System.out.println("优秀");
			} else if (score >= 70) {
				System.out.println("良好");
			} else if (score >= 60) {
				System.out.println("合格");
			} else {
				System.out.println("不合格");
			}
		}

	}

}

开关分支语言switch

语法:

switch(表达式){
        case 值1 判断表达式的值是否在值1,如果相等则执行代码块1
                代码块1; break; 执行完成后,如果没有break,则自动执行后续语句,但是不进行判定。如果有break则立即中断switch的执行,跳到switch块的下一局继续执行
        多个case语句:
                ... ...
        default:如果所有的case都不能成立,则执行代码块n
                代码块n;
                 break;
}

需求:输入月份,显示对应的本月的天数

package com.yan2;

import java.util.Scanner;

public class Test4 {

	public static void main(String[] args) {
		// 需求:输入年月份,显示对应的本月的天数
		System.out.println("请输入年份:");
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();

//		if (year <= 0 || year >= 10000) {
//			System.out.println("您输入的年份错误");
//			return;
//		}

		System.out.println("请输入月份:");
		int month = sc.nextInt();
		if (month > 12 || month < 1) {
			System.out.println("您输入的月份错误!!!");
		} else {
			if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
				System.out.println("天数为31");
			} else if (month == 4 || month == 6 || month == 9 || month == 11) {
				System.out.println("天数为30");
			} else {
				// 判断闰年 可以被4整除但是不被100整除或者能被400整除
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					System.out.println("天数为29");
				} else {
					System.out.println("天数为28");
				}
			}
		}
		sc.close();

	}

}
package com.yan2;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {
		// 需求:输入年月份,显示对应的本月的天数
		System.out.println("请输入年份:");
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();
		System.out.println("请输入月份:");
		int month = sc.nextInt();
		int days = 0;
		if (month > 12 || month < 1) {
			System.out.println("您输入的月份错误!");
		} else {
			switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				days = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				days = 30;
				break;
			default:
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					days = 29;
				} else {
					days = 28;
				}
				break;
			}
			System.out.println(year + "年" + month + "月有" + days + "天");

		}

	}

}

循环结构

循环就是重复做某一件事情。在Java程序中,循环控制即在某一情况下,控制程序重复执行某一条或多条语句,直到遇到终止条件为止
循环语句也称迭代语句,在Java语言中存在的whilefordo-while循环语句
需求:计算1+2+3+...+100=5050

while循环


whileJava中最基本的循环语句,满足条件才执行循环体,如果不满足条件则不执行。所以有可能循环体一次都不执行。

语法:while(条件){循环体;} 当条件成立时反复执行循环体,每执行一次判断一次条件。

package com.yan2;

public class Test7 {

	public static void main(String[] args) {
//		int k = 100;
//		int m = 99;
//		int res = k > 100 ? m++ : ++m;
//		System.out.println(res);// 100 k>100不成立,则直接执行++m,而和表达式2:m++无关,不会执行
//		System.out.println(m);// 100

		// 计算1到100的和
//		第一种写法
//		int k = 0;
//		int res = 0;
//		while (k++ < 100) {
//			res += k;
//		}
//		System.out.println("1+2+3+..+100=" + res);

//		第二种写法
//		int res = 0;// 累加器,用于存放每次循环体的执行结果
//		int k = 1;// 控制循环次数
//		while (k < 101) {
//			/res=res+k;
//			/k++;
//			res += k++;
//		}
//		System.out.println("1+2+3+...+100=" + res);

//		第三种写法
		int k = 1;
		int res1 = 0;
		while (true) {
//			System.out.println(k);
			res1 += k++;
			if (k > 100)
				break;
		}
		System.out.println("1+2+3+...+100=" + res1);
    }
}

do/while循环


语法:
do{循环体;}while(条件); 先执行循环体,然后进行条件判断,如果条件为true,则执行下次循环;如果条件为false则终止循环。
        不同于while循环的点:do/while循环至少执行循环体一次,而while循环有可能一次都不执行
package com.yan2;

public class Test9 {

	public static void main(String[] args) {
		int k = 1;
		int res = 0;
		do {
			res += k++;
		} while (k <= 100);
		System.out.println(res);
	}
}

for循环


语法:for(表达式1;表达式2;表达式3){循环体;}
        执行表达式1
        执行表达式2,一般表达式2是一个条件,如果条件为true则执行循环体;如果条件为false则终止循环
        执行循环体
        执行表达式3
        执行第2步骤
for循环一般用于可以明确循环次数的场景中;while循环一般用于不能预先确定循环执行次数的场景中;do/while循环一般用于不能预先确定循环执行次数,同时要求循环体至少执行一次的场景中
package com.yan3;

public class Test1 {

	public static void main(String[] args) {
		int res = 0;
		// 第一种写法
//		for (int k = 1; k < 101; k++) {
//			res += k;
//		}
		// 第二种写法
		int k = 1;
		for (;;) {
			res += k++;
			if (k > 100)
				break;
		}
		System.out.println("1+2+3+..+100=" + res);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值