scanner类的一些使用方法

import java.util.Arrays;
import java.util.Scanner;

/**
 * description:
 * Created by gaoyw on 2018/4/30.
 */
public class ScannerTest {

    public static void testScanner(){
        Scanner reader=new Scanner(System.in);
        double sum=0;
        int m=0;
        while(reader.hasNextDouble()) {
            double x=reader.nextDouble();
            m=m+1;
            sum=sum+x;
        }
        System.out.printf("%d个数的和为%f\n",m,sum);
        System.out.printf("%d个数的平均值是%f\n",m,sum/m);
    }

    public static void testNextLine(){
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.next();//next()之后cursor会依然指向本行,读取空格前的字符
        System.out.print("请输入第二个字符串:");
        //由于cursor没有换行,所以仍然有一个换行符存在,nextLine()会依据这个换行符自动读取当前行
        // 需要读取实际意义的下一行,可以在之前先调用sc.nextLine()
        s2=sc.nextLine();
        System.out.println("输入的字符串是:"+s1+" "+s2);
    }

    public static void testNextLine2(){
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.nextLine();
        System.out.print("请输入第二个字符串:");
        s2=sc.next();
        System.out.println("输入的字符串是:"+s1+" "+s2);
    }

    public static void testNextLine3(){
        String s1,s2;
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入第一个字符串:");
        s1=sc.next();
        sc.nextLine();//这里是读取了之前没有被读取的换行符
        System.out.print("请输入第二个字符串:");
        s2=sc.nextLine();
        System.out.println("输入的字符串是:"+s1+" "+s2);
    }

    public static void testWhile(){
        Scanner in = new Scanner(System.in);
        // 一个while就是一个测试用例
        while(in.hasNext()){
            int n = in.nextInt(); // 该测试用例后续接收的参数个数
            long[] array = new long[n];
            String[] arrayStr = new String[n];
            for(int i=0; i<n; i++){
                arrayStr[i] = in.next();
            }
            for(int i=0; i<n; i++){
                array[i] = in.nextLong();// 取下一个元素转换成long类型
            }
            System.out.println(Arrays.toString(array)+" "+ Arrays.toString(arrayStr));
        }
    }

    public static void testBreak(){
        Scanner sc = new Scanner(System.in);
        while (!sc.hasNext("break")) {
            String input = sc.next();
            if (input.equals("finish")) break;
            System.out.println(input);
        }
        System.out.println("loop has finished");
    }

    public static void main(String[] args){
//        testScanner();
        testNextLine();
//        testNextLine2();
//        testNextLine3();
//        testWhile();
//        testBreak();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值