赞
踩
1. Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
2. 顺序结构是最简单的算法结构。
3.语句与语句之间,框与框架之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法。
案例一:
- public class Demo01 {
- public static void main(String[] args) {
- System.out.println("hello1");
- System.out.println("hello2");
- System.out.println("hello3");
- System.out.println("hello4");
- }
- }
运行结果:
我们很多时候需要去判断一个东西是否可行,然后才去执行,这样一个过程在程序中用if语句来 表示。
语法:
- if(布尔表达式){
- //如果布尔表达式为true将执行语句
- }
案例二:
- import java.util.Scanner;
- public class Demo02 {
- 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就搞不定了了,我们需要两个判断,需要一个双选结构,所以就有了if-else 结构
语法:
- if(布尔表达式){
- //如果布尔表达式的值为true
- }else{
- //如果布尔表达式的值为false
- }
案例三:
- import java.util.Scanner;
- public class Demo03 {
- 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(布尔表达式 1){
- //如果布尔表达式 1的值为true执行代码
- }else if(布尔表达式 2){
- //如果布尔表达式 2的值为true执行代码
- }else if(布尔表达式 3){
- //如果布尔表达式 3的值为true执行代码
- }else{
- //如果以上布尔表达式都不为true执行代码
- }
案例四:(满分100)
- import java.util.Scanner;
- public class Demo04 {
- 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("成绩优秀");
- }else if(score<80 && score>=70){
- System.out.println("成绩良好");
- }else if(score<70 && score>=60){
- System.out.println("成绩良好");
- }else if(score<60 && score>=0) {
- System.out.println("成绩不及格");
- }else{
- System.out.println("成绩不合法");
- }
- scanner.close();
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
运行结果:
嵌套的if结构:使用嵌套的if...else语句是合法的。也就是说你可以在另一个if或else if语句中使用if或者elseif语句。你可以像if语句一样嵌套else if...else
语法:
- if(布尔表达式 1){
- //如果布尔表达式 1的值为true执行代码
- if(布尔表达式 2){
- //如果布尔表达式 2的值为true执行代码
- }
- }
1. switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
2. switch 语句中的变量类型可以是:
(1)byte、short、int、或者char
(2)从Java SE 7 开始Switch支持字符串String类型了
(3)同时case标签必须为字符串常量或字面量
语法:
- switch(expression){
- case value:
- //语句
- break; //可选
- case value:
- //语句
- break; //可选
- //你可以有任意数量的case语句
- default://可选
- //语句
- }
案例五:
- public class Demo05 {
- public static void main(String[] args) {
- //case穿透 switch匹配一个具体值
- char grade = 'B';
- 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("未知等级");
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
运行结果:
案例六:
- public class Demo06 {
- public static void main(String[] args) {
- String name = "小明";
- //从Java SE 7 开始Switch支持字符串String类型了
- //同时case标签必须为字符串常量或字面量
- switch (name){
- case "小明":
- System.out.println("小明");
- break;
- case "小亮":
- System.out.println("小亮");
- break;
- default:
- System.out.println("在干啥?");
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
运行结果:
注意:在Java5中引入了一种主要用于数组的增强型for循环。
While是最基本的循环,结构为:
- While(布尔表达式){
-
- //循环内容
-
- }
注意:
案例七:
- public class Demo07 {
- public static void main(String[] args) {
- //输出1~100
- int i = 0;
- while (i<100){
- i++;
- System.out.println(i);
- }
- }
- }
案例八:经典案例(从一加到100)
- public class Demo08 {
- public static void main(String[] args) {
- int i = 0;
- int sum = 0;
- while (i<=100){
- sum = sum + i;
- i++;
- }
- System.out.println(sum);
- }
- }
运行结果:5050
对于while语句而言,如果不满足条件,则不能进入循环。do...while循环和while循环相似,不同的是do...while循环至少会执行一次。
语法:
- do {
- //代码语句
- }while(布尔表达式);
while与do...while的区别:
1. while先判断后执行。do...while是先执行后判断。
2. do...while保证循环体会被至少执行一次!
案例九:
- public class Demo09 {
- public static void main(String[] args) {
- int i = 0;
- int sum = 0;
- do{
- sum = sum + i;
- i++;
- }while (i<=100);
- System.out.println(sum);
- }
- }
运行结果:5050
案例十:(while和do...while比较)
- public class Demo10 {
- 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);
- }
- }
运行结果:
分析:上面while循环不满足条件,不进入循环。do...while至少循环一次。
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循 环结构。
语法:
- for(初始化;布尔表达式;更新){
- //代码语句
- }
案例十一:(计算1~100之间奇数和偶数的和)
- public class Demo11 {
- public static void main(String[] args) {
- 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);
- }
- }
运行结果:
语法:
- for(声明语句:表达式)
-
- {
- //代码句子
- }
案例十二:
- public class Demo12 {
- public static void main(String[] args) {
- int[] numbers = {10,20,30,40};
- for (int x:numbers){
- System.out.println(x);
- }
- }
- }
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。