Java 流程控制

scanner

package com.jun.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收");
        if(scanner.hasNext()){
            String str = scanner.next();
            System.out.println(str);
        }
        scanner.close();
    }
}

循环

while(true){
    //do something
}

do{
  //这里先执行一次  
}while(ex)

for(int i =1;i<=100;i++ ){
    //循环体
}

99乘法表

    public static void main(String[] args) {
        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();
        }
    }

增强for循环

循环数组

public static void main(String[] args) {
        int [] numbers = {10,20,30,40,50};
        for(int i:numbers){
            System.out.println(i);
        }
    }

break 和 continue

  1. break不执行循环剩余的语句
  2. continue跳出当前循环后,下一次循环会继续,用来终止某一次的循环

打印三角形


        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();
        }
posted @ 2021-02-08 12:20  callAjax  阅读(85)  评论(0编辑  收藏  举报