• Java基础--三大结构


    顺序

    {
    
    }
    

    选择

    if-else
    if-else if-else
    switch:
          case:
                break
    

    循环

    do{
    
    }while();
    while(){
    
    }
    for(;;)
    // for each (增强for循环) JDK5
    

    例子

    输出数组元素

    package struct;
    public class Demo05 {
        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]);
            }
            // for each (增强for循环) JDK5
            for (int x: numbers){
                System.out.println(x);
            }
        }
    }
    

    终止

    breakcontinue

    package struct;
    
    /**
     * break continue
     */
    public class Demo06 {
        public static void main(String[] args) {
            int i = 0;
            while(i<100){
                i++; //此处如果把 i++放在continue后面的话 
                	 //使用continue,程序会一直卡在这儿,continue后面的语句不会执行
                if(i==30)
                    // break;      //终止整个循环
                    continue;   //终止某次循环
                System.out.println(i);
            }
        }
    }
    
    

    练习

    题目: 打印三角形

    package struct;
    
    public class TestDemo01 {
        public static void main(String[] args) {
            //打印三角形
            for (int i = 0; i < 5 ; i++) {
                for (int j = 5; j > i ; j--) {
                    System.out.print(" ");
                }
                for (int j = 0; j <= i ; j++) {
                    System.out.print("*");
                }
                for (int j = 0; j < i ; j++) {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }
    
    
  • 相关阅读:
    MySQL-索引
    MySQL-存储引擎
    MySQL-基本概念
    Elasticsearch-分片原理2
    Elasticsearch-分片原理1
    [NOIP模拟33]反思+题解
    [NOIP模拟测试32]反思+题解
    [NOIP模拟测试31]题解
    [jzoj5840]Miner 题解(欧拉路)
    [NOIP模拟测试30]题解
  • 原文地址:https://www.cnblogs.com/sinlearn/p/13362859.html
Copyright © 2020-2023  润新知