Multidimensional Arrays - Lab 04
Read a set of numbers and group them by their remainder when dividing to 3 (0, 1 and 2). One first line, you will get numbers separated with coma.
Гърми ми на 4 тест и не мога да разбера защо. Кода ми е това:
import java.util.Scanner; public class groupNums04 { public static void main(String[] args){ Scanner input = new Scanner(System.in); String space = " "; String rem0 = new String(); String rem1 = new String(); String rem2 = new String(); String[] lineNums = input.nextLine().split(", "); int[] lineNumsArr = new int[lineNums.length]; for (int i =0; i < lineNumsArr.length; i ++){ lineNumsArr[i] = Integer.parseInt(lineNums[i]); } for(int i =0; i < lineNumsArr.length; i ++){ int remainder = Math.abs(lineNumsArr[i]) %3; if (remainder == 0){ rem0 = rem0 + " " + Integer.toString(lineNumsArr[i]); } else if (remainder == 1){ rem1 = rem1 + " " + Integer.toString(lineNumsArr[i]); } else if (remainder == 2){ rem2 = rem2 + " " + Integer.toString(lineNumsArr[i]); } } String[] allRemainders = new String[]{rem0.trim(), rem1.trim(), rem2.trim()}; int[][] jagged = new int[3][]; for (int i = 0; i < jagged.length; i ++){ String[] inputNums = allRemainders[i].split(" "); jagged[i] = new int[inputNums.length]; for (int j = 0; j < inputNums.length; j ++){ jagged[i][j] = Integer.parseInt(inputNums[j]); } } for (int i =0; i < jagged.length; i ++){ for (int j = 0; j < jagged[i].length; j++){ System.out.printf("%d ", jagged[i][j]); } System.out.println(); } } }