Java流程控制

Java流程控制

用户交互Scanner

Scanner 对象

  1. Java提供了一个工具类用于获取用户的输入,java.util.Scanner 是Java5的新特征,可以通过Scanner类来获取用户输入
  2. 基本语法:
	Scanner s = new Scanner(System.in);
  1. 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()和hasNextLine()判断是否还有输入的数据。
  2. next():
  • 一定要读取到有效字符后菜可以结束输入
  • 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
  • 只有输入有效字符后才会将其后面的空白作为结束符
    -** next()不能得到带有空格的字符串**
import java.util.Scanner;

public class Demon01 {
    public static void main(String[] args) {
        //创建一个对象用于接收数据
        Scanner scanner = new Scanner(System.in);
        //判断是否有下一行
        System.out.println("判断是否还有下一行");
        if(scanner.hasNext()){
            //输入下一行
            String str = scanner.next();
            //输出下一行
            System.out.println("下一行输出为:" + str);
        }
        scanner.close();
        //用完就关闭
    }
}
  1. nextLine():
  • 以Enter作为结束符
  • 可以获得空白
import java.util.Scanner;

public class Demon03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input:");
        String str = scanner.nextLine();
        System.out.println("Output:" + str);
        scanner.close();
    }
}

Scanner 进阶用法

import java.util.Scanner;

public class Demon05 {
    public static void main(String[] args) {
        //输入多个数字,求平均数,计算输入个数,不是数字结束
        Scanner scanner = new Scanner(System.in);
        //输入数据个数
        int m = 0;
        int sum = 0;

        //循环语句,一直判断输入是否为数字
        while(scanner.hasNextDouble()){
            sum += scanner.nextDouble();
            m++;
            System.out.println("Now is No." + m + " digit");
        }
        System.out.println(sum/m);
        System.out.println(m);
        scanner.close();
    }
}

顺序结构

  1. Java的基本结构就是顺序结构,除非特殊情况,否则就一句一句执行。
  2. 顺序结构是最基本的结构

选择结构

if 结构

  1. if选择结构
    • 很多时候需要去判断一个东西是否可行,然后执行
import java.util.*;
public class ifDemon01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        //判断字符串是否相等
        if(s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();
    }
}
  1. if双选择结构
import java.util.*;

public class IfDemon02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double s = scanner.nextDouble();
        if(s >= 60){
            System.out.println("Pass!");
        }else{
            System.out.println("Not Pass!");
        }
        scanner.close();
    }
}
  1. if多选择结构
import java.util.Scanner;

public class IfDemon03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double s = scanner.nextDouble();
        if(s == 100){
            System.out.println("Excellent");
        }else if(s >= 90 && s < 100){
            System.out.println("Rank A");
        }else if(s >= 80 && s < 90){
            System.out.println("Rank B");
        }else if(s >= 70 && s < 80){
            System.out.println("Rank C");
        }else if(s >= 60 && s < 70){
            System.out.println("Rank D");
        }else if(s >= 0 && s < 60){
            System.out.println("Not Pass");
        }else{
            System.out.println("Invalid Value!");
        }

        //一个if里面必须有else语句,保证完备性
        scanner.close();
    }
}

switch 结构

  1. 多选择结构通过switch case语句实现
  2. switch case语句判断一个变量与一系列值中的某个值是否相等,每个值为一个分支。
  3. switch语句中的类型可以是:int,byte,short,char,string
import java.util.*;
public class SwitchDemon01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char grade = 'A';
        //case有穿透特性,需要加break
        //switch匹配一个具体的值
        switch(grade){
            case 'A':
                System.out.println("Great!");;
                break;
            case 'B':
                System.out.println("Good");
                break;
            case 'C':
                System.out.println("Okay");
                break;
            case 'D':
                System.out.println("Pass");
                break;
            default:
                System.out.println("Invalid vaule");
                break;
        }
        scanner.close();
    }
}
  • 字符串case的比较是通过哈希值实现的。

循环结构

while循环

  1. while是最基本的循环
    基本语法:
while(布尔表达式){
	//内容
}
  • 大多数情况都要使得程序无限循环,有时则需要一直循环,就需要设置死循环
  • 循环条件一直为true就会造成无限循环,正常情景应该避免,会占用大量资源。
  1. 示例
public class whileDemon03 {
    public static void main(String[] args) {
        int sum = 0;
        int i = 1;
        while(i<=100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
}

do...while循环

  1. 对于while而言,不满足条件就不会执行
  2. 但是do...while至少会执行一次
public class dowhileDemon01 {
    public static void main(String[] args) {
        int a = 0;
        while(a<0){
            System.out.println(a++);
        }
        System.out.println("******************************");
        do{
            System.out.println(a++);
        }while(a<0);
    }
}

for循环

  1. 虽然循环都能使用while或者do...while表示,但是for循环会让结构更简单。
  2. for循环具有通用、灵活性
public class forDemon01 {
    public static void main(String[] args) {
        for(int i = 1;i <= 100; i++){
            //同C语言,初始化;条件;迭代
            System.out.println(i);
        }
    }
}
  1. 九九乘法表
public class multiplication {
    public static void main(String[] args) {
        for(int i = 1; i < 10;i++){
            for(int j = 1; j <= i; j++){
                System.out.print(i + "*" + j + " ");
            }
            System.out.println();
        }
    }
}
  1. 增强for循环
  • 主要是数组和集合对象使用,用于简化
  • 语法格式:
public class forDemon05 {
    public static void main(String[] args) {
        int[] a = {10, 20, 30, 40, 50};
        for(int x : a){
            System.out.println(x);
        }
    }
}

break & continue

  1. break
  • break主要用于跳出语句块,只是结束循环或者case
  1. continue
  • continue主要用于终止某次循环,但会继续执行下一步的循环
public class breakDemon01 {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i++){
            System.out.println(i);
            if(i==5)
                break;
        }
        for(int i= 0; i < 10; i++){
            System.out.println(i);
            if(i==5)
                continue;
        }
    }
}

练习:打印三角形

import java.util.*;
public class test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input the line of triangle");
        int l = scanner.nextInt();
        for(int i=0;i<l; i++){
            for(int j=0;j<l-i;j++)
                System.out.print(" ");
            for(int j=0;j<2*i-1;j++)
                System.out.print("*");
            System.out.println();
        }
        scanner.close();
    }
}
posted @ 2021-10-20 23:42  黑衣の甘铃儿  阅读(41)  评论(0)    收藏  举报