Java流程控制

Java流程控制

1.Scanner对象

通过Scanner类来获取用户的输入。

基本语法:

Scanners = new Scanner(System.in);
//通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

import java.util.Scanner;

public class demo2 {
    public static void main(String[] args) {
        //从键盘接收数据-声明一个scanner变量,并用new运算符实例化Scanner,实例化Scanner时,需要传入System.in对象,Scanner通过传入的System.in获取用户输入,并对用户输入的字符进行处理,屏蔽了获取用户输入的复杂操作。
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine方式接收:");
        //判断是否还有输入
        if (scanner.hasNextLine()){
            String str=scanner.nextLine();
            System.out.println("输入的内容为:"+str);
        }

        scanner.close();
    }
}

//简单的键入输出无需加入判断过程
import java.util.Scanner;

public class demo2 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine方式接收:");
        
        String str=scanner.nextLine();
        
        System.out.println("输入的内容为:"+str);

        scanner.close();
    }
}

在这里插入图片描述

2.Scanner进阶使用
import java.util.Scanner;

public class demo4 {
    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 += x;
            System.out.println("你输入了第"+m+"个数据,然后当前的结果sum="+sum);
        }
        System.out.println(m+"个数的和为:"+sum);
        System.out.println(m+"个数的平均值为:"+(sum/m));
        scanner.close();
    }
}

3.顺序结构

在这里插入图片描述

4.选择结构

if单选择结构:

语法:

if(布尔表达式){
    //如果布尔表达式为true将执行的语句
}

示例:

import java.util.Scanner;

public class xuanze {
    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();
    }
}

if 双选择结构:

语法:

if (布尔表达式){
    //如果布尔表达式为true
}else{
    //如果布尔表达式为false
}

示例:

import java.util.Scanner;

public class xuanze1 {
    public static void main(String[] args) {
        //考试分数大于60就是及格,小于60分就是不及格。
        Scanner scanner = new Scanner(System.in);

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

        if (score>60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }
        scanner.close();
    }
}

if多选择结构:

语法:

if (布尔表达式1){
    //如果布尔表达式1为true
}else if(布尔表达式2){
    //如果布尔表达式2为true
}else if(布尔表达式3){
    //如果布尔表达式2为true
}else{
    //如果以上布尔表达式都不为true
}

if嵌套:

语法:

if(布尔表达式1){
    //如果布尔表达式1为true
    if(布尔表达式2){
       //如果布尔表达式2为true
    }
}

switch多选择结构:

  • 多选择结构还有一个实现方法是switch case语句
  • switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

语法:

switch(expression){
    case value:
        //语句
        break;//可选
    case value:
        //语句
        break;//可选
	//可以有任意数量的case语句
    default://可选
        //语句     
}

在这里插入图片描述

示例:

public class switch1 {
    public static void main(String[] args) {
        //case穿透   switch 匹配一个具体的值
        char grade = 'A';

        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("再接再厉");
                break;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");
                break;
        }
    }
}
5.循环结构

while循环:尽量避免死循环

语法:

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

示例:

public class whiledemo {
    public static void main(String[] args) {
        //计算1+2+3+..+100=?

        int i = 0;
        int sum =0;
        while(i<=100){
            sum+=i;
            i++;
        }
        System.out.println(sum);
    }
}

do…while循环:

对于while语句而言,如果不满足条件,则不能进入循环。但是有时我们需要即使不满足条件,也至少执行一次。

do…while循环和while循环相似,不同的是,do…while循坏至少会执行一次。

语法:

do{
    //代码语句
}while(布尔表达式)

示例:

public class dowhiledemo {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do{
            sum += i;
            i++;
        }while(i<=100);
        System.out.println(sum);
    }
}

在这里插入图片描述

for循环:

语法:

for(初始化;布尔表达式;更新){
    //代码语句
}

示例1:

public class fordemo {
    public static void main(String[] args) {
        //练习:循环输出1-1000之间能被5整除的数,并且每行输出3个
        for (int i=0;i<=1000;i++){
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%15==0){
                System.out.println();
                //System.out.print("\n");
            }
        }
    }
}

示例2:

public class fordemo1 {
    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();
     	}
    }
}
6.增强for循环

Java5引入的一种主要用于数组或集合的增强型for循环。

语法:

for(声明语句:表达式)
{
    //代码句子
}

在这里插入图片描述

示例:

public class fordemo2 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};

        for (int x:numbers){
            System.out.println(x);
        }
    }
}
7.break,continue和goto

break:强制终止循环,不再进行循环语句

示例:

public class breakdemo {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
    }
}

continue:用于终止某次循环,仍会继续循环(跳到循环开始处继续循环)

示例:

public class continuedemo {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            if (i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
    }
}

goto:

在这里插入图片描述

8.实例作业
public class textdemo {
    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 = 1 ;j <=i ; j++){
                System.out.print("*");
            }
            for (int j = 1 ; j < i ; j++){
                System.out.print("*");
            }

            System.out.println();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值