Java--流程控制

Java–流程控制

用户交互Scanner

Scanner对象

  1. Java通过Scanner类获取用户的输入

  2. 基本语法:

Scanner scanner = new ScannerSystem.in);
  1. 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般使用hasNext()与hasNextLine()判断是否还有输入的数据
  • next()方法:next()在读到第一个有效字符后,遇到空白就会停止,此处的空白包括回车和空格。

示例:

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()){
            //使用next()方法接收
            String str1 = scanner.next();

            System.out.println("输出:"+str1);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,习惯性关闭。
        scanner.close();
    }
}

结果:
image

  • nextLine()方法:以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
    示例:
import java.util.Scanner;
public class Demo01 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方法接收:");
        //判断用户有没有输入字符串
        if(scanner.hasNextLine()){
            //使用nextLine()方法接收
            String str1 = scanner.nextLine();

            System.out.println("输出:"+str1);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,习惯性关闭。
        scanner.close();
    }
}

结果:
image

顺序结构

  1. java的基本结构就是顺序结构,是最简单的算法结构,他是有若干个处理步骤组成的,是任何一个算法都离不开的一种基本算法结构。
  2. 示例
public class Demo02 {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
        System.out.println("hello6");//从上到下依次输出
    }
}

选择结构

if单选择结构

  1. 我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程程序中用if语句来表示。
  2. 语法
if(布尔表达式){
   //如果布尔表达式为true将执行的语句
}
  1. 示例:
public class Demo02 {
    public static void main(String[] args) {
        System.out.println("请输入:");
        Scanner scanner = new Scanner(System.in);
        //Scanner类的nextLine方法
        String s = scanner.nextLine();
        //判断字符串是否相等
        if(s.equals("lic")){
            System.out.println(s);
        }
            System.out.println("Wan");
        scanner.close();
    }
}

if双选择结构

  1. 通常我们的需求一个if搞不定,需要有两个判断,就需要一个双选择结构,所以有了if-else结构。
  2. 语法
if(布尔表达式){
   //如果布尔表达式为true将执行的语句
}else{
   //如果布尔表达式为false将执行的语句
}
  1. 示例
public class Demo02 {
    public static void main(String[] args) {
        System.out.println("请输入成绩:");
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();

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

        scanner.close();
    }
}

if多选择结构

  1. 补充完善多种选择的需求,所以有多选择结构来处理这类问题。
  2. 语法
if(布尔表达式1){
   //如果布尔表达式为true将执行的代码
}else if(布尔表达式2){
   //如果布尔表达式为true将执行的代码
}else if(布尔表达式3){
   //如果布尔表达式为true将执行的代码
}else{
   //如果以上布尔表达式都不为true将执行的代码
}
  1. 示例
public class Demo02 {
    public static void main(String[] args) {
        System.out.println("请输入成绩:");
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();

        if(score<=100&&score>=90){
            System.out.println("A");
        }
        else if(score<90&&score>=80)
            System.out.println("B");
        else if(score<80&&score>=70)
            System.out.println("C");
        else if(score<70&&score>=60)
            System.out.println("D");
        else if(score<60&&score>=0)
            System.out.println("E");
        else{
            System.out.println("输入成绩不合法!");
        }
        scanner.close();
    }
}
  1. 一旦其中一个else if语句检测为true 其他的else if语句都将跳过执行!

嵌套的if结构

  1. 语法
if(布尔表达式1){
   //如果布尔表达式1为true将执行的语句
   if(布尔表达式2){
   //如果布尔表达式2为true将执行的语句
   }
 } //(套娃哈哈哈哈哈)

switch多选择结构

  1. switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

  2. switch 语句中的变量类型可以是:
    1、byte、short、int、char、jdk7之后支持String
    2、从java SE7开始
    3、同时case标签必须为字符串常量或字面量

  3. 语法

   switch (expression){
            case value;
               //语句
            break;
            case value;
               //语句
            break;
            default: //可选
                //语句
        }

示例:

public class Demo03 {
    public static void main(String[] args) {
        //case穿透
        //switch 匹配一个具体的值
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要匹配的字符串:");
        String s = scanner.nextLine();
        switch (s){
            case"Lic":
                System.out.println("李大聪明");
                break;
            case "Wan":
                System.out.println("王大聪明");
                break;
            case"Liu":
                System.out.println("刘大聪明");
                break;
            default:
                System.out.println("不太聪明");
        }
    }
}

循环结构

while循环

  1. while是最基本的循环,它的结构为:
while(布尔表达式){
 //循环内容
 }
  1. 只要布尔表达式为true,循环就会一直执行下去
  2. 大多数情况让循环停止下来的,需要一个让表达式失效的方式来结束循环。
  3. 少数情况循环一直执行,比如服务器的请求响应监听等。
  4. 循环条件一直为true会造成死循环,避免死循环,会影响程序性能或卡死。
    示例:
public class Demo04 {
    public static void main(String[] args) {
        //输出从1加到100
        int i = 0;
        int sum = 0;
        while(i<=100){
            sum= sum +i;
            i++;
        }
        System.out.println(sum);//输出5050
        /* 死循环 停止不了 应避免 形象性能
        while(true){
        //等待客户端连接
        //定时检查
        }
         */
    }

do while循环

  1. while和do while循环相似,不同的是,do while循环至少会执行一次,即使不满足条件。
do{
    //代码语句
 }while(布尔表达式);
  1. while和do while的区别:
    1、 while先判断后执行,do while先执行后判断
    2、do while总是保证循环体至少会被执行一次,这是主要差别。
    示例:
public class Demo05 {
    public static void main(String[] args) {
        //输出从1加到100
        int i = 0;
        int sum = 0;
       do{
           sum = sum+i;
           i++;
       }while(i<=100);
        System.out.println(sum);
    }
}

for循环

  1. for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构
  2. for循环执行的次数是在执行前就确定的,语法格式如下:
for(初始化;布尔表达式;更新){
    //代码语句
 }
  /*
        for循环的几点说明:
        最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量。可以是空语句。
        然后检测布尔表达式的值。如果为TRUE,循环体执行。否则终止循环,开始执行循环后面的语句。
        执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
        再次检测布尔表达式,循环执行上面的过程。
        for (;;){
            //死循环
        }
        */

示例1:计算0~100之间奇数和偶数的和

public class Demo05 {
    public static void main(String[] args) {
        //计算0到 100之间奇数和偶数的和
        int oddSum = 0;
        int evenSum = 0;
        for (int i = 0; i < 100; i++) {
            if(i%2!=0){
                oddSum = oddSum +i;
            }else{
                evenSum = evenSum+i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);
    }
}

示例2:输出1~1000之间能被5整除的数,并且每行输出3个

public class Demo06 {
    public static void main(String[] args) {
        //输出1~1000之间能被5整除的数,并且每行输出3个
        for (int i = 1; i < 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%(5*3)==0){
                System.out.println();
            }
        }
        //println 输出完会换行
        //print  输出完不会换行
    }
}

示例3:打印九九乘法表

public class Demo07 {
    //打印九九乘法表
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++) {//外循环控制行

            for (int i = 1; i <= j ; i++) {//内循环控制列    i<=j是为了去掉重复项
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }

    }
}

增强for循环

  1. Java5引入了一种用于数组或集合的增强型for循环
  2. 格式如下:
for(声明语句:表达式){
    //代码语句
 }
  1. 声明语句:声明新的变量,该变量的类型必须与数组元素的类型匹配。其作用域限定在循环语句块,此值与此时数组元素的值相等。
  2. 表达式:就是要访问的数组名,或者是返回值为数组的方法
    示例:
public class Demo08 {
    public static void main(String[] args) {
        int [] numbers = {10,20,30,40,50}; //定义了一个数组
        //遍历数组的元素
        //增强for循环
        for(int x:numbers){
            System.out.println(x);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值