博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java: Best Way to read a file
阅读量:5047 次
发布时间:2019-06-12

本文共 1509 字,大约阅读时间需要 5 分钟。

经常在各种平台的online test里面不熟悉STDIN, STOUT,下面举个例子:

Input Format

There are three lines of input:

  1. The first line contains an integer.
  2. The second line contains a double.
  3. The third line contains a String.

Output Format

There are three lines of output:

1. String

2. Double

3. Int

Sample Input

423.1415Welcome to HackerRank's Java tutorials!

Sample Output

String: Welcome to HackerRank's Java tutorials!Double: 3.1415Int: 42 Hi, I don't understand why we have to do a sc.nextLine(), then do it again sc.nextLine()... Sometimes you have to clear the buffer to print the strings by command sc.nextLine(); Answer: After supplying data for int, we would hit the enter key. What nextInt() and nextDouble() does is it assigns integer to appropriate variable and keeps the enter key unread in thekeyboard buffer. so when its time to supply String the nextLine() will read the enter key from the user thinking that the user has entered the enter key. (that's we get empty output) . Unlike C, there is no fflush() to clean buffer, so we have to flush by not taking it in variable.
1 import java.util.*; 2  3 class Solution { 4      5     public static void main(String[] args) { 6         Scanner sc = new Scanner(System.in); 7         int n = sc.nextInt(); 8         double x = sc.nextDouble(); 9         sc.nextLine();10         String str = sc.nextLine();11 12 13         System.out.println(str);14         System.out.println(x);15         System.out.println(n);16     }17 }

 

 

 

转载于:https://www.cnblogs.com/EdwardLiu/p/4200279.html

你可能感兴趣的文章
③面向对象程序设计——封装
查看>>
【19】AngularJS 应用
查看>>
Spring
查看>>
Linux 系统的/var目录
查看>>
Redis学习---Redis操作之其他操作
查看>>
WebService中的DataSet序列化使用
查看>>
BZOJ 1200 木梳
查看>>
【Linux】【C语言】菜鸟学习日志(一) 一步一步学习在Linxu下测试程序的运行时间...
查看>>
hostname
查看>>
SpringBoot使用其他的Servlet容器
查看>>
关于cookie存取中文乱码问题
查看>>
k8s架构
查看>>
select 向上弹起
查看>>
mysql 多表管理修改
查看>>
group by order by
查看>>
bzoj 5252: [2018多省省队联测]林克卡特树
查看>>
https 学习笔记三
查看>>
华为“云-管-端”:未来信息服务新架构
查看>>
基于Sentinel实现redis主从自动切换
查看>>
函数(二)
查看>>