选择结构
选择结构
if
if单选择结构
package com.ylmxy.struct;
import java.util.Scanner;
public class ifDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals:判断字符串是否相等
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
if双选择结构
package com.ylmxy.struct;
import java.util.Scanner;
public class ifDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
float score = scanner.nextFloat();
if (score>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
if多选择结构
package com.ylmxy.struct;
import java.util.Scanner;
public class ifDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/*
if 语句至多有1个else语句
*/
System.out.println("请输入成绩:");
float score = scanner.nextFloat();
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语句中的变量类型可以是:
- byte、short、int、char、String
- 同时case标签必须为字符串常量或字面量
package com.ylmxy.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透 //switch 匹配一个具体的值
char grade = 'C';
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 com.ylmxy.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "嘿嘿嘿";
//字符的本质还是数字
//反编译 java---class(字节码文件)---反编译(IDEA)
switch (name){
case "ylmxy":
System.out.println("ylmxy");
break;
case "姚欣堉":
System.out.println("姚欣堉");
break;
default:
System.out.println("干嘛");
}
}
}
循环结构
while循环
- 只要布尔表达式为true,循环就会一直执行下去
- 我们大多数情况会让循环停止下来,需要一个让表达式失效的方式来结束循环
package com.ylmxy.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.ylmxy.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true){
//等待客户端链接
//定时检查
}
}
}
package com.ylmxy.struct;
//计算1-100之和
public class WhileDemo03 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
do...while循环
- 对于while语句而言,如果不满足条件,则不能进入循环。但有时需要即使不满足条件,也至少执行一次
- do...while循环和while循环相似,不同的是,do...while循环至少会执行一次
- while先判断后执行,do...while先执行后判断
package com.ylmxy.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
package com.ylmxy.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);
}
}
for循环
- for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构
- for循环执行的次数是在执行前就确定的
for(初始化;布尔表达式;更新){
//代码语句
}
package com.ylmxy.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1;//初始化条件
while (a<=100){//条件判断
System.out.println(a);//循环体
a += 2;//迭代
}
System.out.println("while循环结束!");
//初始化//条件判断//迭代
for (int i = 1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束!");
}
}
package com.ylmxy.struct;
//计算0到100之间的奇数和偶数的和
public class ForDemo02 {
public static void main(String[] args) {
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i <= 100; i++) {
if (i%2!=0){
sum1+=i;
}else {
sum2+=i;
}
}
System.out.println("奇数的和为;"+sum1);
System.out.println("偶数的和为:"+sum2);
}
}
package com.ylmxy.struct;
//用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
System.out.println();
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
package com.ylmxy.struct;
//九九乘法表
public class ForDemo04 {
public static void main(String[] args) {
/*
1.打印第一列
2.把固定的1再用一个循环包起来
3.去掉重复项,i <= j
4.调整样式
*/
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
package com.ylmxy.struct;
//等边实心三角形
public class ForDemo05 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int i1 = 5; i1 >= i; i1--) {
System.out.print(" ");
}
for (int i2 = 1; i2 <= i; i2++) {
System.out.print("*");
}
for (int i3 = 1; i3 < i; i3++) {
System.out.print("*");
}
System.out.println();
}
}
}
package com.ylmxy.struct;
//空心三角形
public class ForDemo07 {
public static void main(String[] args) {
int i3 = 0;
for (int a = 1; a <= 5; a++) {
for (int b = 5; b > a; b--) {//控制三角形前空格
System.out.print(" ");
}
for (int c = 1;c<=2*a-1;c++){//控制后面字符数
if (a==5&&(c%2!=0)){
System.out.print("*");
} else if (c==1||c==2*a-1) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
增强for循环
package com.ylmxy.struct;
public class ForDemo06 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定义了一个数组
for (int i = 0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("=========================");
//遍历数组的元素
for (int x:numbers){
System.out.println(x);
}
}
}

浙公网安备 33010602011771号