zoukankan      html  css  js  c++  java
  • java

    1. 顺序结构

    2. 分支结构

    if(){

    }

    else{

    }

    switch:

    class test{
        public static void main(String[] args){
            int x = 6;
            switch(x){
                case 1:
                    System.out.println("a");

                    break
                case 6:
                    System.out.println("b");

                    break
                default:
                    System.out.println("c");

                    break
            }
        }
    }

    x只能为:byte, short, int, char, enum, String

    case后需要加break,否则会执行下一个break之前的所有代码。

    利用这个特性可以用switch做学习成绩查询,虽说一般用for- - 

    import java.util.*;
    class test{
        public static void main(String[] args){
            Scanner s = new Scanner(System.in);
            int x = s.nextInt();
            switch(x/10){
                case 0: 
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    System.out.println("不及格");
                    break;
                case 6: 
                    System.out.println("及格");
                    break;
                case 7: 
                    System.out.println("中等");
                    break;
                case 8: 
                    System.out.println("良好");
                    break;
                case 9: 
                    System.out.println("优秀");
                    break;
                case 10: 
    if(x == 100){ // 防止101-109的数字出现,出现后直接跳过并穿透到default System.out.println(
    "满分"); break;
    }
    default: System.out.println("请输入0-100之间的数字"); break; } } }

    3.循环结构

    for(初始条件;循环条件(不满足则循环停止);变化量(每次循环后的变化)){

    }

    while(循环条件){

    变化

    执行代码

    }

    do{

    变化

    执行代码

    }

    while(满足条件);

    区别

    import java.util.*;
    class test{
        public static void main(String[] args){
            int i = 10;
            while(i<5){
                System.out.println(i);
            }
            System.out.println(i);
            i = 10;
            do{
                System.out.println(i);
                i++;
            }
            while(i<5);
            System.out.println(i);
        }
    }

    初始值就不符合循环条件时

    while不会执行

    do while会执行一次

    break

    int i = 1; 
        while(i < 5){
            if(i == 3){
                break;
            }
            i++
        }
    }

    break会终止循环。

    import java.util.*;
    class test{
        public static void main(String[] args){
            for(int i = 0;i < 5; i++){
                for(int j = 0; j < 5; j++){
                    if(j == 3){
                        break;
                    }
                    System.out.println("i=" + i + "|j=" + j);
                }
            }
        }
    }

     break只会中断里层循环,而且break之后不能直接加代码

    import java.util.*;
    class test{
        public static void main(String[] args){
            for(int i = 0;i < 5; i++){
                for(int j = 0; j < 5; j++){
                    if(j == 3){
                        break;
                        System.out.println("循环终止");
                    }
                    System.out.println("i=" + i + "|j=" + j);
                }
            }
    } }

     

     continue

    continue只中断这一次循环,break是中断整个循环。

    import java.util.*;
    class test{
        public static void main(String[] args){
            for(int i = 0;i < 5; i++){
                for(int j = 0; j < 5; j++){
                    if(j == 3){
                        continue;
                    }
                    System.out.println("i=" + i + "|j=" + j);
                }
            }
        }
    }

     j会在等于0,1,2,4的时候输出

     break的时候 j 不会等于4;

    循环标记

    才知道有这玩意。。。

    通过给循环起名字来操作指定循环,不指定的话只能操作break和continue所在的内层循环。

    import java.util.*;
    class test{
        public static void main(String[] args){
            cycle1: for(int i = 0;i < 5; i++){
                cycle2: for(int j = 0; j < 5; j++){
                    if(j == 3){
                        break cycle1;
                    }
                    System.out.println("i=" + i + "|j=" + j);
                }
            }
        }
    }

     cycle2中判定j==3后,直接中断外部cycle1循环。

    continue同理

    import java.util.*;
    class test{
        public static void main(String[] args){
            cycle1: for(int i = 0;i < 5; i++){
                cycle2: for(int j = 0; j < 5; j++){
                    if(j == 3){
                        continue cycle1;
                    }
                    System.out.println("i=" + i + "|j=" + j);
                }
                System.out.println("---------");
            }
        }
    }

    continue中断了外部的循环因为中断了外部循环所以内部循环也不再执行,继续执行外部循环的下一次;

    i=0,j=3时,停止当前循环,继续执行下一次i=1的循环。

    while也可以起名字 name:while(...){}

    Math.pow(a,b);   a^b

  • 相关阅读:
    IDEA新字体出现啦!!!
    Spring学习笔记1---Bean的自动装配
    Spring学习笔记1---Bean的作用域
    Spring学习笔记1---C命名和P命名的注入
    Spring学习笔记1---IoC创建对象的方式
    Spring学习笔记1---IoC容器
    Spring学习笔记1---IOC理论推导
    vfatbGUbLE
    Spring学习笔记1---环境配置(IDEA)
    springboot整合ES
  • 原文地址:https://www.cnblogs.com/clamp7724/p/11570561.html
Copyright © 2011-2022 走看看