美文网首页
Scanner的操作

Scanner的操作

作者: shoulda | 来源:发表于2018-07-24 09:02 被阅读0次

Scanner的操作

next():读取以空格为结束符
nextDouble(),nextFloat(),nextInt()

nextLine():读取以换行为结束符,读取的是字符串

hasNext():判断是否有输入
hasNextLine():判断是否有下一行

知道固定长度

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] a = new int[N];
for(int i = 0;i < N;i++){
  a[i] = sc.nextInt();
}

不知道固定长度

Scanner sc = new Scanner(System.in);
String[] nums = null;
nums = sc.nextLine().split(" ");
int[] num = new int[nums.length];
for(int i = 0;i < num.length;i++){
  num[i] = Integer.valueOf(nums[i]);
}

相关文章

网友评论

      本文标题:Scanner的操作

      本文链接:https://www.haomeiwen.com/subject/xyzsmftx.html