Java中的各类结构
顺序结构
- Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
- 顺序结构是最简单的算法结构。
- 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法。
package struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
}
}
选择结构
- if单选择结构
package struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60分就是及格,小于60就是不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score < 60){
System.out.println("你的成绩不及格");
} else {
System.out.println("你的及格了");
}
scanner.close();
}
}
- if双选择结构
package struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60分就是及格,小于60就是不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score < 60){
System.out.println("你的成绩不及格");
} else {
System.out.println("你的及格了");
}
scanner.close();
}
}
- if多选择结构
- 嵌套的if结构
package struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
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("成绩不合法");
}
scanner.close();
}
}
- switch多选择结构
- switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
- switch 语句中的变量类型可以是:
- byte、short、int或者char
- 从Java SE 7开始
- switch 支持字符串String类型了
- 同时 case 标签必须为字符串常量或者自变量
package struct;
public class switchDemo {
public static void main(String[] args) {
//case穿透 switch 匹配一个具体的值
char grade = 'F';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("不及格");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知等级");
}
}
}
package struct;
public class switchDemo02 {
public static void main(String[] args) {
String name = "嘉兴薛之谦";
//JDK7的新特性,表达式结果可以是字符串!!!
//字符的本质还是数字
//反编译 Java----class(字节码文件)----反编译(IDEA)
switch (name){
case "sss":
System.out.println("sss");
break;
case "嘉兴薛之谦":
System.out.println("嘉兴薛之谦");
break;
default:
System.out.println("弄啥嘞?");
}
}
}
循环结构
while循环
- while循环是最基本的循环,它的结构为:
while(布尔值表达式){
//循环内容
}
- 只要表达式的值为true,循环就会一直执行下去。
- 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
- 少部分情况需要循环一直执行,比如服务器的请求响应监听等。
- 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!
package struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100
int i = 1;
while (i<=100){
System.out.println(i++);
}
}
}
死循环:
package struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true){
System.out.println("嘉兴薛之谦");
}
}
}
习题:1加到100:
package struct;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+...+100 = ?
int i = 100;
int sum = 0;
while (i>0){
sum += i;
i--;
}
System.out.println(sum);
}
}
do…while循环
- 对于while语句而言,如果不满足条件,则不能进入循环。但有时我们即使不满足条件,也至少可以执行一次。
- do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。
do{
//代码语句
}while(布尔值表达式);
- While和do…while的区别:
- while先判断后执行。do…while是先执行后判断。
- Do…while总是保证循环体会被至少执行一次!这是他们的主要差别。
用do…while来实现1加到100:
package struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
//用do....while循环实现1加到100
int i = 0;
int sum = 0;
do {
sum += i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
观察while循环和do…while循环的区别:
package struct;
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
} //无输出
System.out.println("============");
do {
System.out.println(a);
a++;
}while (a<0); //输出0
}
}
For循环结构
- 虽然所有循环结构都可以用while或者do…while表示,但是Java提供了另一种语句——for循环,使一些循环结构变得更加简单。
- for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
- for循环执行的次数是在执行前后就确定的。语法格式如下:
for(初始化;布尔值表达式;更新){
//代码语句
}
- 练习一:计算0到100之间的奇数和偶数和。
- 练习二:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个。
- 练习三:打印九九乘法表。
package struct;
public class ForDemo01 {
public static void main(String[] args) {
//练习一:计算0到100之间的奇数和偶数和。
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if (i%2!=0){ //奇数
oddSum += i;
}else { //偶数
evenSum += i;
}
}
System.out.println("奇数的和为:"+oddSum);
System.out.println("偶数的和为:"+evenSum);
}
}
package struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习二:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个。
int count = 0;
for (int i = 0; i <= 1000; i++) {
if (i%5 == 0){
System.out.print(i+"\t");
count++;
if (count%3==0){
System.out.println();
}
}
}
}
}
package struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习三:打印九九乘法表。
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+"*"+j+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
增强for循环
package struct;
public class ForDemo04 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50}; //定义了一个数组
for (int x : numbers){
System.out.println(x);
}
}
}