題目描述
在大多數的HackerRank題目挑戰中,您必須從標準輸入串流(stdin, standard input)中讀取資料,並將結果輸出到標準輸出串流(stdout, standard output)。
Java的標準輸入串流物件為「System.in」,可以使用「Scanner」類別來讀取其中的資料。標準輸出串流物件為「System.out」,可以直接使用物件提供的「print」、「println」、「printf」等相關方法將字串輸出到標準輸出串流中。
在這個題目中,您必須要從標準輸入串流讀取3個整數,並且將它們輸出至標準輸出串流。
原題網址
https://www.hackerrank.com/challenges/java-stdin-and-stdout-1
範例輸入
42
100
125
範例輸出
42
100
125
解題概念
使用Scanner物件提供的「nextInt」方法直接讀取輸入串流中的整數,接著再依序使用標準輸出串流物件提供的「println」方法,將整數輸出至標準輸出串流。
參考答案
import java.util.Scanner;
public class Solution {
public static void main(final String[] args) {
final Scanner sc = new Scanner(System.in);
final int a = sc.nextInt();
final int b = sc.nextInt();
final int c = sc.nextInt();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}