https://www.hackerrank.com/challenges/java-stdin-stdout/problem
这题看似简单的Java标准终端输入输出,但是应该注意到nextInt(), nextDouble()只读取int,double而将换行符留于流中,因此应在读取下一整行前通过不处理的nextLine()将换行符从输入流中读取不作处理;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
https://www.hackerrank.com/challenges/java-output-formatting/problem
关于格式化输出流的相关问题;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
System.out.printf("%-15s",s1);
System.out.printf("%03d\n",x);
}
System.out.println("================================");
}
}