#10712: Java RE


manhong2112 (飄夢)


不懂為甚麼RE, index out of bound??

測資時x > y或y < x的情況都是AC的, 但到送出後就RE了?!這不科學吧

import java.util.*;

public class a015 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String[] str = input.nextLine().split(" ");
        int x = Integer.valueOf(str[0]);
        int y = Integer.valueOf(str[1]);
        int count = 0;
        int[][] arr = new int[y][x];
        while(input.hasNext()) {
          str = input.nextLine().split(" ");
          for(int i = 0;i < str.length;i++) {
              arr[i][count] = Integer.valueOf(str[i]);
          }
          count += 1;
        }
        for(int[] i:arr) {
            for(int j:i) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }

}

#10713: Re:Java RE


manhong2112 (飄夢)


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
	at code_2702472.main(code_2702472.java:14)
因為是寫入陣列時出錯, 是給的陣列不止一個?

 

#10714: Re:Java RE


manhong2112 (飄夢)


好吧, 果然是不止一個測資...

import java.util.*;

public class a015 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String[] str = input.nextLine().split(" ");
        int x = Integer.valueOf(str[0]);
        int y = Integer.valueOf(str[1]);
        int count = 0;
        int[][] arr = new int[y][x];
        while(input.hasNext()) {
          str = input.nextLine().split(" ");
          for(int i = 0;i < str.length;i++) {
              arr[i][count] = Integer.valueOf(str[i]);
          }
          count += 1;
          if(count == x) {
            for(int[] i:arr) {
                for(int j:i) {
                    System.out.print(j + " ");
                }
                System.out.println();
            }
            if(input.hasNext()) {
                str = input.nextLine().split(" ");
                x = Integer.valueOf(str[0]);
                y = Integer.valueOf(str[1]);
                arr = new int[y][x];
                count = 0;
            }
          }
        }
    }

}