December 22, 2020 Continuing from yesterday, we will deal with standard input in Java. Shows how to receive the entered numerical values separated by blanks as data.
import java.util.*;
public class Sample {
public static void main(String[] args) {
int[] points = new int[5];
Scanner scan = new Scanner(System.in);
for(int i = 0; i < points.length; i++) {
points[i] = scan.nextInt();
System.out.println(points[i]);
}
}
}
import java.util.*;
public class Sample {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] pointsStr = scan.nextLine().split(" ");
int[] points = new int[pointsStr.length];
for(int i = 0; i < points.length; ++i) {
points[i] = Integer.parseInt(pointsStr[i]);
System.out.println(points[i]);
}
}
}
[Java] How to get and output standard input
Recommended Posts