java流程控制之Scanner(1)

2021/7/29 11:05:56

本文主要是介绍java流程控制之Scanner(1),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        scanner();

        scanner2();

        scanner3();
    }

    public  static  void  scanner(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("next方式接收:");

        if (scanner.hasNext()){
            String str= scanner.next() ;
            System.out.println("输入内容为:"+str);
        }
//        scanner.close();
    }

    public  static  void  scanner2(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("nextLine方式接收:");

        if (scanner.hasNextLine()){
            String srt=scanner.nextLine();
            System.out.println("输入内容为:"+srt);

        }
//        scanner.close();
    }


    public  static  void  scanner3(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据 :");

        String str = scanner.next();
        System.out.println("next===>输入内容为:"+str);
        String res = scanner.nextLine();
        System.out.println("nextLine==>输入内容为:"+res);

        scanner.close();

    }

}

运行结果



这篇关于java流程控制之Scanner(1)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程