Java流程控制

流程控制


用户交互Scanner

package 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);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,养成好习惯用完就关掉
        scanner.close();
    }
}

输入:hello world 输出:hello

public class Demo02 {
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
       
        System.out.println("请输入数据:");
        
        if (scanner.hasNextLine()) {
            String str = scanner.nextLine();
            System.out.println("输入的内容为:" + str);
        }
        scanner.close();
    }
}

输入:hello world 输出:hello world

  • next()
    1. 一定要读取到有效字符才可以结束输入。
    2. 对输入有效字符之前遇到的空白,next()方法会自动去掉
    3. 只有输入有效字符后才将后面的输入的空白作为分隔符或者结束符
    4. next()不能得到带空格的字符串
  • nextLine():
    1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前所有的字符。
    2. 可以获得空白字符

nextFloat(); nextFloat(); .......

package scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //从键盘接收数据
        int i = 0;
        float f = 0.0f;

        System.out.println("请输入整数:");

        if(scanner.hasNextInt()){
            i = scanner.nextInt();//nextInt()
            System.out.println("整数数据:" + i);
        }
        else{
            System.out.println("你输入的不是整数数据!");
        }

        System.out.println("请输入小数:");

        if(scanner.hasNextFloat()){
            f = scanner.nextFloat();//nextFloat()
            System.out.println("小数数据:" + f);
        }
        else{
            System.out.println("你输入的不是小数据!");
        }

        scanner.close();
    }
}

输入求和

public class Demo04 {
    public static void main(String[] args) {
        //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,
        // 通过输入非数字来结束输入并输出执行结果

        Scanner scanner = new Scanner(System.in);

        //和
        double sum = 0;
        //计算输入了多少个数字
        int m = 0;

        //通过循环判断是否还有输入,并在里面对每一次进行求和统计
        while (scanner.hasNextDouble()){
            double x = scanner.nextDouble();
            m++;
            sum = sum + x;
        }

        System.out.println(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值为" + sum/m);

        scanner.close();

    }
}

顺序结构

  

选择结构

if选择

//if选择
if(){
	
}else if{

}else{

}
public class Demo01 {
    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();
    }
}
public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        double score = scanner.nextDouble();

        if (score>=60)
            System.out.println("及格");
        else
            System.out.println("不及格");

        scanner.close();
    }
}

switch选择

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade = 'c';
        switch (grade){
            case 'a'://表达式可以是字符串
                System.out.println("优秀");
                break;//可选,否则穿透现象,底下全部输出
            case 'B':
                System.out.println("良好");
                break;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");

        }
    }
}

tip:反编译 java --class(字节码文件)----反编译(IDEA)

将class文件拷贝到java文件目录下,然后再idea中双击打开

循环结构

while循环

while(布尔表达式){
	//循环内容
}

一般情况下,需要一个表达式让循环失效
public static void main(String[] args) {
    //输出1~100
    int i = 0;
    while (i < 100) {
        i++;
        System.out.println(i);
    }
}

do...while循环

相比while,循环至少会执行一次

for 循环

for(初始化;布尔表达式;更新){

	//循环体

}

最有效最灵活的循环结构

tip: 快捷输入:100.for 从0到100

最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个控制变量,也可以是空语句

然后,检测布尔表达式的值,如果为true,循环体被执行。如果为false,循环终止。执行一次循环后,更新循环的控制变量。

for( ; ; ){

}//死循环
//100以内的奇数和、偶数和
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);
}
public class ForDemo02 {
    //输出1~1000被5整除的数,每行输出3个
    public static void main(String[] args) {
        for (int i = 1; i < 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
            }
            if (i % (5 * 3) == 0)
                System.out.print("\n");
        }
    }
}
public static void main(String[] args) {
    //九九乘法表
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j<= i; j++)
            System.out.print(i + "*" +j + "=" +(i*j) + "\t");
        System.out.println();
    }
}

break continue

  • break在任何循环语句的主体部分,均可用break控制循环流程。break用于强行退出循环,不执行循环中剩余语句
  public static void main(String[] args) {
      //输出0到30跳出循环
      int i = 0;
      while (i<100){
          i++;
          System.out.println(i);
          if (i==30){
              break;
          }
      }
  }
  • continue用于终止某次循环,即跳过循环中尚未执行的语句,接着进行下一次循环判定。
public static void main(String[] args) {
    //跳过10的倍数
    int i = 0;
    while (i<100){
        i++;
        if(i%10==0){
            System.out.println();
            continue;
        }
        System.out.print(i);
    }
}

label标签 对java来说唯一用到的地方在循环语句之前

public static void main(String[] args) {
    //打印101~150之间的质数
    //质数是大于1的自然数中,除了1和它本身不再有其他因数的自然数

    int count = 0;

    outer:for (int i = 101;i<150;i++){
        for (int j = 2;j<i/2 ;j++){
            if(i%j==0){
                continue outer;
            }
        }
        System.out.print(i+" ");
    }
}

练习

打印三角形

public static void main(String[] args) {
    //打印三角形
    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-24 18:54  SagiriV  阅读(53)  评论(0)    收藏  举报