Java流程控制

用户交互Scanner

Scanner对象
  • 之前我们学的基本语法中我们并没有实现程序和人的交互,但是java给我们提供了这样一个工具类,我们可以获取用户的输。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入

  • 基本语法

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

  • 在使用Scanner语法的时候必须用close语法结束流程控制。

    public static void main(String[] args) {
            System.out.print("请输入:");
            Scanner scanner = new Scanner(System.in);
            String str = scanner.next();
            System.out.println("输入的东西:" + str);
            scanner.close();
        }
    
next()&nextLine()
  • next()

    1. 一定要读取到有效字符后才可以结束输入。
    2. 对有效字符之前输入的空白,next()方法会自动将其去掉。
    3. 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
    4. next()不能得到带有空格的字符串。

    例如:

    public static void main(String[] args) {
            System.out.print("请输入:");
            Scanner scanner = new Scanner(System.in);
            String str = scanner.next();
            System.out.println("输入的东西:" + str);
            scanner.close();
        }
    

  • nextLine()

    1. 以Enter即回车作为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符串。
    2. 可以获得空白。

    例如:

    public static void main(String[] args) {
            System.out.print("请输入:");
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            System.out.println("输入的东西:" + str);
            scanner.close();
        }
    

Scanner进阶

在这里插入图片描述

  • nextInt()输入整数,nextByte()输入字节,nextDouble()输入双精度数,nextFloat()输入单精度数…

结构

顺序结构
  • Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。

  • 顺序是最简单的算法结构。

  • 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。

代码

public static void main(String[] args) {
        System.out.println("01");
        System.out.println("02");
        System.out.println("03");
        System.out.println("04");
        System.out.println("05");
    }
选择结构
  • if结构

    • 单选择结构:我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示。

      • 语法

        	if(布尔表达式){
                	//如果布尔表达式值为true
                }  
        
    • 双选择结构

      • 语法

        	if(布尔表达式){
        		//如果布尔表达式的值为true
        	}else{
        		//如果布尔表达式的值为false
        	}
        
    • 多选择结构

      • 语法

        	if(布尔表达式){
        		//如果布尔表达式的值为true
        	}else if (){
        		//如果布尔表达式的值为true
        	}else if (){
                	//如果布尔表达式的值为true
                }else{
                	//上面表达式都不符合执行此语句
                }
        
    • 嵌套的if结构

      • 使用嵌套的if…else语句是合法的。也就是说你可以在另一个if或者else if语句中使用if或者else if语句。你可以像if语句一样嵌套else if…else。

      • 语法

        if(布尔表达式1){
            //如果布尔表达式1的值为true执行代码
            if(布尔表达式2){
                 //如果布尔表达式2执行的值为true执行代码
            }
        }
        
    • switch多选择结构

      • 多选择结构还有一个实现方式就是switch case语句。

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

      • switch语句中的变量类型可以是:

        • byt、short、int,以及char。
        • 从Java Se 7开始,switch支持字符串String类型了。
        • 同时case标签必须为字符串常量或者字面量。
      • 语法

        switch(expression){
                case value;
                //语句
                	break;
                case value;
                //语句
                	break;
                default :   //意思为当以上语句都没有匹配到时,默认选择此语句
        }
        
      • 代码

        //判断输入的罗马数字是否符合
        public class Suanfa04 {
            public static void main(String[] args) {
                Solutions solution = new Solutions();
                String n = new Scanner(System.in).nextLine();
                System.out.println(solution.romanToInt(n));
            }
        }
        
        class Solutions {
            public int romanToInt(String s) {
                int sum = 0;
                int first = romanMath(s.charAt(0));
                for (int i = 1; i < s.length(); i++) {
                    int hou = romanMath(s.charAt(i));
                    if (first < hou){
                        sum -= first;
                    }else {
                        sum += first;
                    }
                    first = hou;
                }
                sum += first;
                return sum;
            }
            public int romanMath(char z){
                switch(z){
                    case 'I':return 1;
                    case 'V':return 5;
                    case 'X':return 10;
                    case 'L':return 50;
                    case 'C':return 100;
                    case 'D':return 500;
                    case 'M':return 1000;
                    default:return 0;
                }
            }
        }
        
循环结构
  • while循环

    • while是最基本的循环,其结构为

      while (){
      	//循环内容
      }
      
    • 只要布尔表达式为true,循环就会一直执行下去。

    • 我们大多数情况下是会让循环停止下来,我们需要一个让表达式失效的方式来结束循环。

    • 少部分情况下需要一直执行,比如服务器的响应监听等。

    • 循环一直为true会使循环陷入死循环。

    代码:

    //整数倒序输出
    public class Suanfa03 {
        public static void main(String[] args) {
            Solution s = new Solution();
            int n = new Scanner(System.in).nextInt();
            System.out.println(s.twoSum(n));
        }
    }
    class Solution {
        public boolean twoSum(int x) {
            if (x == Daoxu(x)){
                return true;
            }else {
                return false;
            }
        }
        public int Daoxu(int y){
            int dao = 0;
            while (y != 0 && y >0) {
                if ((dao * 10) / 10 != dao) {
                    dao = 0;
                    break;
                }
                dao = dao * 10 + y % 10;
                y = y / 10;
            }
            return dao;
        }
    }
    
  • do…while循环

    • do…while循环和while循环相似,不同的是,do…while至少会执行一次,即在do语句中会执行一次。

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

      • while先判断后执行,do…while先执行后判断。
      • do…while至少会执行一次,这是最主要的区别。

    代码:

    public class java18 {
        public static void main(String[] args) {
            int num = 0;
            int sum = 0;
            do {
                sum += num;
                num++;
            }while (num <= 100);
            System.out.println(sum);
        }
    }
    
  • for循环

    • for循环语句是支持迭代的一种通用结构,是最有效,最灵活的虚幻结构。

    • for循环执行的次数是在执行前就确定的。

    • 语法

      for(初始化;控制语句;更新语句){
          //代码语句
      }
      

    练习:

    打印九九乘法表

    public class java08 {
        public static void main(String[] arg){
            //九九乘法表
            for (int i=1;i<=9;i++){
                    for (int j = 1; j <= i; j++) {
                        System.out.print(j + "*" + i + "=" + i * j + " ");
                    }
                    System.out.println();
            }
        }
    }
    

    图示

    判断100以内的奇数

    public class java06 {
        public static void main(String[] args) {
            //if语句和for语句的嵌套
            //判断1~100以内的奇数
            for (int i = 1; i <= 100; i++) {
                if (i % 2 != 0) {
                    System.out.println(i);
                }
            }
        }
    }
    

    打印三角形

    public class java09 {
        public static void main(String[] arg){
            Scanner scanner = new Scanner(System.in);
            System.out.print("输入控制的三角形行数:");
            int n = scanner.nextInt();
            //打印三角形1
            for (int i=1 ; i<=n ; i++){
                for (int j=n ; 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();
            }
            System.out.println("----------------------------------------");
            //打印三角形2
            for (int i=1 ; i<=n ; i++){
                for (int j=n ; j>=i ; j--){
                    System.out.print(" ");
                }
                for (int j=1 ; j<=2*i-1 ; j++){
                    System.out.print("*");
                }
                System.out.println();
            }
            scanner.close();
        }
    }
    

    图示

  • 增强型for循环

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

    • 语法

      for(声明语句:表达式){
      	//代码语句
      }
      
    • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。起作用域在循环语句块,其值与此时数组的元素的值相等。

    • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

    实例:

    现在有一个数组需要寻找数组里面的的一个数,同时排序,并且说明其位置。

    public class java02 {
            static void number(){
                System.out.print("输入要查询的数:");
                Scanner num=new Scanner(System.in);
                int N=num.nextInt();
                int[] n= {6,23,8,42,9,3,98,10,34,76};
                Arrays.sort(n);
                System.out.print("依次排序:");
                for (int i : n) {
                    System.out.print(i+" ");    //排序
                }
                int f = Arrays.binarySearch(n, N);    //二分查找,9表示从n中排完序要找的数
                System.out.print('\n'+"在第"+(f+1)+"个数");
            }
            public static void main(String[] args) {
                number();
            }
        }
    

    图示

break & continue

  • break在任何循环语句的主体部分,均可用break控制循环的流程,break用于强行退出循环,不执行循环中剩余的语句。(break语句经常在switch中使用)
  • continue语句用在循环语句体中,用于终止某次循环过程,但不会跳出循环,会跳出循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杉shan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值