基本的三种基本算法结构
一、顺序结构
即程序按照顺序执行,是任何一个算法都离不开的一种算法结构
二、选择结构
1. if单选择结构
我们判断一件事是否可行,然后我们才去执行,这个过程用if语句表示
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
package com.link.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();
}
}
2.if双选择结构
语法:
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
示例:
package com.link.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60分的为及格,小于六十分的为不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
if (score <60){
System.out.println("不及格");
}else {
System.out.println("及格");
}
scanner.close();
}
}
3. if多选择结构
多次判断
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}else if(布尔表达式3){
//如果布尔表达式3的值为true执行代码
}else{
//如果以上布尔表达式的值都不为true执行代码
}
示例:
package com.link.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
/*
if语句至多有一个 else 语句,它必须在所有 else if 语句之后;
if语句可以有多个 else if 语句;
一旦其中一个 else if 语句检测为true,其他的 else if 以及 else 语句都将跳过执行;
*/
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
if (score==100){
System.out.println("恭喜你为满分");
}else if (score<100 && score>=85){
System.out.println("恭喜你为优秀");
}else if (score<85 && score>=70){
System.out.println("恭喜你为良好");
}else if (score<70 && score>=60){
System.out.println("成绩为合格");
}else if (score<60 && score>=0){
System.out.println("成绩不合格");
}else if (score<=150 && score>100){
System.out.println("高中语数英是吧");
}else {
System.out.println("”贵物“");
}
scanner.close();
}
}
4.嵌套的if结构
使用嵌套的 if…else 语句是合法的。也就是说可以在一个 if 或者 else if 语句中使用 if 或者 else if 语句。
语法
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
if(布尔表达式 2){
//布尔表达式 2的值为true执行代码
}
}
5. switch 多选择结构
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支
从jdk7开始switch开始支持String类型(字符串)
语法:
switch(expression){
case value:
//语句
break;//可选
case value:
//语句
break;//可选
//你可以有任意数量的case语句
default://可选
//语句
}
示例:(无任何指向意义,单纯图一乐)
package com.link.struct;
import java.util.Scanner;
public class SwitchDemo01 {
public static void main(String[] args) {
//case 穿透 //switch 匹配一个具体的值
Scanner scanner = new Scanner(System.in);
System.out.println("请选择你的英雄:");
String fund_manager= scanner.next();
switch (fund_manager){
case "王圆圆" :
System.out.println("作文大赛一等奖");
break;//可选 没有break的话程序会继续往下执行输出
case "张坤" :
System.out.println("哈哈哈,鸡汤来咯");
break;
case "葛兰" :
System.out.println("已经在谷底了,怎么走都是向上");
break;
case "蔡嵩松" :
System.out.println("菜狗");
break;
case "刘彦春" :
System.out.println("开摆");
break;
default:
System.out.println("梭哈是种智慧");
}
scanner.close();
}
}
三、循环结构
- while 循环
- do…while循环
- for循环
1.while循环
语法:
while(布尔表达式){
//循环内容
}
- 只要布尔表达式为true,循环就会一直执行下去。
- 大多数情况下会让循环停止下来,我们需要一个让表达式失效的方式来结束循环
- 少部分需要循环一直执行,比如服务器的请求响应监听
- 循环条件一直为true就会造成无限循环【死循环】,在正常的编程中应尽量避免死循环,会影响程序性能或者造成程序卡死崩溃
示例1
package com.link.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1到100
int i=0;
while (i<100){
i++;
System.out.println(i);
}
}
}
示例2
package com.link.struct;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+....+100
int i=0;
int sum=0;
while (i<=100){
/*
sum=sum+i;
i++ ;
*/
//sum+=++i;
sum+=i++;
}
System.out.println(i);
System.out.println(sum);
}
}
2.do…while循环
- 对于while语句而言,如果不满足条件,则不能进入循环。但有时候需要即使不满足条件,也至少执行一次
- do…while循环和while循环相似,不同的是,do…while循环至少会执行一次
语法:
do{
//代码语句
}while(布尔表达式);
- while和do-while的区别:
- while先判断后执行,do-while先执行后判断
- do-while总是保证循环体会被至少执行一次,这是他们的主要差别
示例1 与while循环类似
package com.link.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i=0;
int sum=0;
do {
/*
sum=sum+i;
i++ ;
*/
//sum+=++i;
sum+=i++;
}while (i<=100);
System.out.println(i);
System.out.println(sum);
}
}
示例2 与while循环区别
package com.link.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);//先输出的分割线,才输出a=0
}
}
3.For循环(重要)
语法:
for(初始化;布尔表达式;迭代){
//代码语句
}
示例
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){ // 也可键入1"100.for"通过idea的快捷键来构建此行代码
System.out.println(i);
}
System.out.println("For循环结束");
//经典死循环代码; for( ; ; ){ }
}
}
4.增强for循环
package com.link.struct;
public class ForDemo05 {
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);
}
}
}
5.break
用于强行退出循环
package com.link.struct;
public class BreakDemo {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
break;
}
}
System.out.println(123);//此处依然输出,说明break只是跳出循环但程序依然继续往下执行
}
}
6.continue
用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
package com.link.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.println(i);//在i为10的倍数时输出了空行
}
}
}
7.练习
练习1:计算0到100的奇数和偶数的和
package com.link.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习1:计算0到100的奇数和偶数的和
int oddSum=0;
int evenSum=0;
//循环执行直到i=100时退出循环
for (int i = 0; i <=100; i++) {
if (i%2!=0){
oddSum+=i;//i除2的余数不等于0时,判断为true,即认定为偶数oddSum oddSum=oddSum+i;
}else {
evenSum+=i; //如上面条件不满足,if语句判断为false,则认定为奇数,执行此行代码 evenSum=evenSum+i;
}
}
System.out.println("偶数的和"+oddSum);
System.out.println("奇数的和"+evenSum);
}
}
练习2:分别用For和while循环输出1~1000间能被5整除的数,并且每行输出3个
- For循环方式
package com.link.struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用For循环输出1~1000间能被5整除的数,并且每行输出3个
for (int i = 0; i <=1000; i++) {
if (i%5==0){
System.out.print(i+"\t");//print 后不跟ln则不会换行 \t相当于按一次Tab
}
if (i%(5*3)==0){ //每输出3个被5整除的数换一次行
System.out.println();
// System.out.println("\n"); 此行代码与上行一样都代表换行
}
}
}
}
- while循环方式
package com.link.struct;
public class WhileDemo04 {
public static void main(String[] args) {
//练习2:用while循环输出1~1000间能被5整除的数,并且每行输出3个
int i=1;
while (i<=1000){
if (i%5==0){
System.out.print(i+"\t");
}
if (i%15==0){
System.out.println();
}
i++;
}
练习3:打印九九乘法表
package com.link.struct;
public class ForDemo04 {
public static void main(String[] args) {
//1.先打印第1列
//2.我们把固定的1再用一个循环包起来
//3.去掉重复项,m<=i
//4.调整样式
for (int i = 1; i <= 9; i++) {
for (int m = 1; m <= i ; m++) {
System.out.print(m+"X"+i+"="+(i*m)+"\t");
}
System.out.println();
}
}
}
练习4:打印正三角形
package com.link.struct;
public class TestDemo {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 1; i <= 5; i++) {
for (int j=5;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("*");
}
System.out.println();
}
}
}