

public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i -1; j++) {
System.out.print(" ");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int c = 0;
for (int i = 0; i < T ; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
System.out.println(c + " " + c);
}
}
오류
찾아보니 횟수를 입력받는게 아니라
입력은 여러 개의 테스트 케이스로 이루어져 있다.라고 되어있다. 좀 말이 애매한거같지만..
그래서 여기서는 for문이 아닌 while을 사용해서 풀어야 한다.
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while (true) {
int a = sc.nextInt();
int b = sc.nextInt();
if (a == 0 && b == 0) {
break;
}
System.out.println(a + b);
}
}
}

public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
sc.close();
}
}
hasNextInt()를 사용
입력값이 정수일 경우 true 를 반환하며, 정수가 아닐 경우 바로 예외를 던져더 이상의 입력을 받지 않고 (hasNextInt()에서) false를 반환하여 반복문이 종료되게 했다.

public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// 첫 번째 입력: 정수의 개수 N
int N = sc.nextInt();
// 두 번째 입력: N개의 정수 배열
int[] v = new int[N];
// 세 번째 입력: 찾으려는 정수
for (int i = 0; i < N; i++) {
v[i] = sc.nextInt();
}
int findNum = sc.nextInt();
//개수 카운트
int count = 0;
for (int i = 0; i < N; i++) {
if (v[i] == findNum) {
count ++;
}
}
System.out.println(count);
}
}

public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int X = sc.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = sc.nextInt();
}
for (int i = 0; i < N; i++) {
if (A[i] < X) {
System.out.print(A[i] + " ");
}
}
}
}