首页 > 编程语言 > 详细

JavaSE流程控制02

时间:2021-08-29 20:50:53      阅读:11      评论:0      收藏:0      [点我收藏+]

JavaSE流程控制

一.Scanner输入

	//创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);

        //判断用户有没有输入 字符串
        if(scanner.hasNext())
        {
            //使用next方法接收
            String str1 = scanner.next();
            System.out.println(str1);
        }

        //吃掉多余的一个 回车
        //使用nextLine方法接收
        String str2 = scanner.nextLine();


        //判断用户有没有输入 字符串(读取一行)
        if(scanner.hasNextLine())
        {
            //使用nextLine方法接收
            String str3 = scanner.nextLine();
            System.out.println(str3);
        }

        //判断用户有没有输入 Int
        if(scanner.hasNextInt())
        {
            //使用nextInt方法接收
            int num1 = scanner.nextInt();
            System.out.println(num1);
        }

        //判断用户有没有输入 Float
        if(scanner.hasNextFloat())
        {
            //使用nextFloat方法接收
            float num2 = scanner.nextFloat();
            System.out.println(num2);
        }

        //IO流如果不关闭会一直占用资源,用完后就要关闭
        scanner.close();

二.顺序结构


三.选择结构

if

switch

四.循环结构

while

do while

for

break,continue,goto

JavaSE流程控制02

原文:https://www.cnblogs.com/FlyingFishStudio/p/15203067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!