用户交互scanner

scanner-next-nextLine

 1. next![在这里插入“这里next返回 的结果是hello,因为它以空格结束并且输出的结果不带有空格![这里输出的结果为hello world那是因为nextLine返回的结果带空格且以Enter为结束符](https://img-blog.csdnimg.cn/20210127184612735.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FkeW9uZXM=,size_16,color_FFFFFF,t_70#pic_center)
”图片描述](https://img-blog.csdnimg.cn/20210127184![在这里插入图片描述](https://img-blog.csdnimg.cn/20210127184904442.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FkeW9uZXM=,size_16,color_FFFFFF,t_70#pic_center)

package scanner;
import java.util.Scanner;
public class Demo01 {
    public static void main(String[] args) {
        System.out.println("使用next方式接收");
        //从键盘接收数据
        //判断用户有没有输入字符串
        if(scanner.hasNext()){//这里面默认==true
            //判断是否还有输入
            //使用next方式接收
            String str= scanner.next();//程序用等待用户输入
            完毕
            System.out.println("输入的内容为:"+str);
            //凡是输入IO流的类如果不关闭会一直占用资源,要
            养成良好的习惯关闭
            scanner.close();

        }
    }
}

2.nextline
package scanner;
import java.util.Scanner;
public class Demo02 {
    public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方式接收");

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值