런타임 에러 질문 있습니다.
324
작성한 질문수 1
배열 11. 임시반장 정하기 에서 문제를 풀고 있는데해당 코드에서 런타임 에러가 발생하는데 이유를 알 수 있을까요? import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main t = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] arr = new int[n][n];
// int[] arr2 = new int[n];
for (int i = 0 ; i < n; i++){
for (int j =0; j < n; j++)
arr[i][j] = in.nextInt();
}
// for (int i = 0 ; i < n; i++){
// arr2[i] = in.nextInt();
// }
int test = t.solution11(n,arr);
System.out.print(test);
}
// 11번 임시반장
private int solution11(int n, int[][] arr){
int answer = 0;
int MAX = 0;
for (int i =0; i < 5; i ++){
int cnt = 0;
for (int j=0; j < n;j++){
int temp = arr[j][i];
for (int k =0; k <n; k++){
if (k == j) continue;
else if (temp == arr[k][i]){
cnt++;
}
}
if (cnt > MAX){
MAX = cnt;
answer = j;
}
}
}
return answer;
}
}
답변 2
0
안녕하세요^^
학년이 5학년까지로 정해져 있습니다. 입력크기를 5로 하고 학년을 읽는 반복문도 5로 하면 런타임에러는 잡을 수 있습니다. 아래와 같이 하면 에러는 안나옵니다.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main t = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] arr = new int[n][5];
// int[] arr2 = new int[n];
for (int i = 0 ; i < n; i++){
for (int j =0; j < 5; j++)
arr[i][j] = in.nextInt();
}
// for (int i = 0 ; i < n; i++){
// arr2[i] = in.nextInt();
// }
int test = t.solution11(n,arr);
System.out.print(test);
}
// 11번 임시반장
private int solution11(int n, int[][] arr){
int answer = 0;
int MAX = 0;
for (int i =0; i < 5; i ++){
int cnt = 0;
for (int j=0; j < n;j++){
int temp = arr[j][i];
for (int k =0; k <n; k++){
if (k == j) continue;
else if (temp == arr[k][i]){
cnt++;
}
}
if (cnt > MAX){
MAX = cnt;
answer = j;
}
}
}
return answer;
}
}
0
안녕하세요^^
코드블럭 버튼을 이용해서 풀코드를 올려주시면 좋겠습니다. 위에 올린 것은 보기가 불편합니다.
0
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main t = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] arr = new int[n][n];
// int[] arr2 = new int[n];
for (int i = 0 ; i < n; i++){
for (int j =0; j < n; j++)
arr[i][j] = in.nextInt();
}
// for (int i = 0 ; i < n; i++){
// arr2[i] = in.nextInt();
// }
int test = t.solution11(n,arr);
System.out.print(test);
}
// 11번 임시반장
private int solution11(int n, int[][] arr){
int answer = 0;
int MAX = 0;
for (int i =0; i < 5; i ++){
int cnt = 0;
for (int j=0; j < n;j++){
int temp = arr[j][i];
for (int k =0; k <n; k++){
if (k == j) continue;
else if (temp == arr[k][i]){
cnt++;
}
}
if (cnt > MAX){
MAX = cnt;
answer = j;
}
}
}
return answer;
}
}아 죄송합니다 ; 다시 올려드렸어요!
안녕하세요. 바뀐 채점사이트 관련해서 문의드립니다.
0
28
1
갑자기 채점 사이트가 바뀌었어요
0
32
1
문제 리스트 페이지
0
29
1
채점 사이트 관련 질문드립니다
0
23
1
봉우리 문제 질문입니다
0
81
2
씨름 선수 문제에서 각 선수의 몸무게나 키가 같을 수도 있다면?
0
64
0
이 코드랑 영상 코드중에 뭐가 더 좋은 코드인가요?
0
72
0
가중치 방향 그래프에서 가중치가 0인 간선을 표현하는 방법
0
67
1
좌표 정렬 문제 이 코드가 왜 틀린지 모르겠습니다 ㅠㅠ
0
85
2
6-7 강의에서
0
48
1
6-6. 장난꾸러기 질문 있습니다.
0
45
1
강의 수강후 코딩테스트
0
110
1
answer 변수 사용 여부
0
44
1
2중 for문
1
85
2
2-11. 임시반장정하기 (Runtime Error)
0
63
1
혹시 LinkedList 같은 자료 구조들은 따로 배우지 않나요?
0
70
1
이런 풀이는 어떨까요
0
43
1
자바 스트림 방식의 효율성 질문 드립니다.
0
57
1
알고리즘 자료 구조들..
0
62
1
StringBuilder vs BufferdWriter
0
48
1
원더랜드(프림)
0
50
1
이런 코드는 어떤가요?
0
61
1
bfs 풀이
0
57
1
병합정렬
0
56
1





