Почему моя числовая последовательность не печатается должным образом из 2d arraylist?
I cannot get the loop to work in the buildDimArray method to store the number combinations "11+11", "11+12", "11+21", "11+22", "12+11", "12+12", "12+21", "12+22", "21+11", "21+12", "21+21", "21+22", "22+11", "22+12", "22+21", and "22+22" into the 2d arraylist with each expression going into one column of the index dimBase-1 row. The loop may work for other people, but for some reason mine isn't functioning correctly. The JVM sees the if dimBase==1 condition, but refuses to check the other conditions. The "WTF" not being printed as a result from the buildDimArray method. If dimBase=1, it prints successfully, but doesn't for the other integers. The dimBase==3 condition needs a loop eventually. The "WTF" is for illustrative purposes. I could get away with a 1d arraylist, but in the future I will likely need the 2d arraylist once the program is completed.
package jordanNumberApp; import java.util.Scanner; import java.util.ArrayList; /* * Dev Wills * Purpose: This code contains some methods that aren't developed. This program is supposed to * store all possible number combinations from numbers 1-dimBase for the math expression * "##+##" into a 2d arraylist at index row dimBase-1 and the columns storing the * individual combinations. After storing the values in the arraylist, the print method * pours the contents in order from the arraylist as string values. */ public class JordanNumberSystem { // a-d are digits, assembled as a math expression, stored in outcomeOutput, outcomeAnswer public static int dimBase, outcomeAnswer, a, b, c, d; public static String inputOutcome, outcomeOutput; public static final int NUM_OF_DIMENSIONS = 9; //Eventually # combinations go up to 9 public static ArrayList<ArrayList<String>> dimBaseArray; public static Scanner keyboard; /* * Constructor for JordanNumber System * accepts no parameters */ public JordanNumberSystem() // Defunct constructor { // Declare and Initialize public variables this.dimBase = dimBase; this.outcomeOutput = outcomeOutput; this.outcomeAnswer = outcomeAnswer; } // Set all values of variable values public static void setAllValues() { // Initialize dimBase = 1; outcomeAnswer = 22; // variables not used for now outcomeOutput = "1"; // variables not used for now //a = 1; //b = 1; //c = 1; //d = 1; dimBaseArray = new ArrayList<ArrayList<String>>(); keyboard = new Scanner(System.in); } public static void buildDimArray(int dim) { dimBase = dim; try { //create first row dimBaseArray.add(dimBase-1, new ArrayList<String>()); if( dimBase == 1) { a = b = c = d = dimBase ; dimBaseArray.get(0).add(a+""+b+"+"+c+""+d); System.out.println("WTF"); // SHOWS } else if (dimBase == 2) { // dim = 2 a = b = c = d = 1 ; System.out.println("WTF"); // doesn't show // dimBaseArray.get(dimBase-1).add(a+""+b+"+"+c+""+d); for( int i = 1 ; i <= dim ; i++) a=i; for( int j = 1 ; j <= dim ; j++) b=j; for( int k = 1 ; k <= dim ; k++) c=k; for( int l = 1 ; l <= dim ; l++) { d=l; dimBaseArray.get(dim-1).add(a+""+b+"+"+c+""+d); } } else if (dimBase == 3) { a = b = c = d = dimBase; dimBaseArray.get(2).add(a+""+b+"+"+c+""+d); System.out.println("WTF"); } }catch (IndexOutOfBoundsException e) { System.out.println(e.getMessage()); } } public static void printArray(int num) // Prints the contents of the array { // Fixing the printing method try { int i = num-1; for( String string : dimBaseArray.get(i)) { System.out.println(string); System.out.println(""); } } catch (IndexOutOfBoundsException e) { System.out.println(e.getMessage()); } } public static void main(String[] args) throws java.lang.IndexOutOfBoundsException { setAllValues(); // sets the initial a,b,c,d values and dimBase, initializes 2d arraylist // Get the Dimension Base number System.out.println("Enter Dimension Base Number. Input an integer: "); int dimBaseInput = keyboard.nextInt(); // Receives integer dimBase = dimBaseInput; if( dimBase != 1 && dimBase != 2 && dimBase != 3) {// Error checking System.out.println("invalid Dimension Base Number should be 1 or 2 "); System.exit(1); } // Build the arraylist, print, clear, exit buildDimArray(dimBase); printArray(dimBase); dimBaseArray.clear(); System.exit(1); } }// End of class
Что я уже пробовал:
Я попытался отладить программу. Это было не очень полезно. Я обратился за помощью к онлайн-репетитору. Они взяли мои деньги, исправили пару вещей, но на самом деле не решили проблему. Я хотел бы знать, работает ли этот код для кого-то еще.