String str=scan.nextLine();与String str=scan.next();的区别

用Java来表示

package lianxi;
        import java.util.Scanner;
public class Geshu {
            public static void main(String[] args) {
                Scanner scan=new Scanner(System.in);
        int num=0;
        int c=0;
        int kong=0;
        int others=0;
        //输入字符串
        System.out.println("输入字符串");
        String str=scan.nextLine();
        /*String str=scan.next();
         * String str=scan.nextline();
         * 区别:
         * next遇到空白制表回车字符结束(就是遇到空格结束)
         * nextline遇到换行符结束(运行程序的最后一下回车)
        */
        for(int i=0;i<str.length();i++) {
            //字符串一个一个判断字符
            char ch=str.charAt(i);
            if(ch>='0'&&ch<='9') {
                num++;
            }
            else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') {
                c++;
            }
            else if(ch==' ') {
                kong++;        
            }
            else {
                others++;
            }
        }
        System.out.println("数字个数:"+num);
        System.out.println("字符个数:"+c);
        System.out.println("空格个数:"+kong);
        System.out.println("其他字符:"+others);
    }

}


1,正常输入是三个数字,三个字符,三个空格,还有第三个其他符号,(代码在上面)

 现在做一个改动把String str=scan.nextline();改成String str=scan.next();

之后只要遇到字符串中有空格就会终止,因此无法用它来扫描空格的字数,会直接中断

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值