Java 阵列打印非常奇怪
我有一个非常奇怪的数组打印。代码和输出如下。Java 阵列打印非常奇怪,java,arrays,Java,Arrays,我有一个非常奇怪的数组打印。代码和输出如下。 public class AdvDotComLauncher { public static void main(String[] args) { AdvDotComTable table = new AdvDotComTable(); table.createTable(5,5, 5); } } 下面使用有问题的数组编写代码 import java.util.ArrayList; public
public class AdvDotComLauncher {
public static void main(String[] args) {
AdvDotComTable table = new AdvDotComTable();
table.createTable(5,5, 5);
}
}
下面使用有问题的数组编写代码
import java.util.ArrayList;
public class AdvDotComTable {
public void createTable(int size, int dotComAmount, int dotComSize) {
//Holds the DotComs that will be randomly put onto the map
String[] dotComs = {"Pets.com", "Amazon.com", "Target.com", "Apple.com", "Microsoft.com", "Steampowered.com"};
//Holds the rows and columns
ArrayList<Character> row = new ArrayList<Character>();
ArrayList<Integer> column = new ArrayList<Integer>();
column.add(0);
char[] theAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
//Makes sure the Dot Coms will fit
if (dotComAmount >= dotComs.length || dotComAmount / dotComSize > size) {
System.out.println("There are too many dot coms! Please enter different amounts.");
}
//Used in the while loop to make sure that the correct amount of columns are created
int done = size * size;
//Used to get an index value in theAlphabet
int theAlphabetIndex = 0;
//Used to add columns
int number = 0;
//Used to tell when to create a column
int size2 = size;
//Starts creating rows and columns
while (done > 0) {
row.add(theAlphabet[theAlphabetIndex]);
if (size2 == 0) {
size2 = size;
column.add(number);
number++;
theAlphabetIndex++;
}
System.out.println(row.get(number) + "" + column.get(number));
size2--;
done--;
}
System.out.println("The table has been created with " + dotComAmount + " Dot Coms, a table size of " + size + ", and a Dot Com size of " + dotComSize + ".");
}
}
预期输出如下:
A0
B0
C0
D0
E0
A1
B1
C1
D1
E1
A2
B2
C2
D2
E2
A3
B3
C3
D3
E3
A1
B4
C4
D4
E4
我不知道是什么原因导致了这个问题,如果您能提供任何帮助,我们将不胜感激。再次查看您的程序
AlphabetIndex
从不增加,您将其设置为0,但它不会随着size2
的值为5而增加。由于theAlphabetIndex=0
您只得到A(…),索引为0。——到目前为止,我们能为您提供的最好帮助是建议您学习如何调试代码,这样您就可以自己发现问题,而不必等待他人为您调试。你也可以通过这种方式了解更多。我可以通过在列中交换行来修复数字。添加(数字)代码>和number++代码>。我完全搞不懂你想用这些信做什么。你错了。我放了一个System.out.println(“测试”);if语句中的语句,并将其打印出来。而且,每次循环通过时,size2都会减小……那么为什么会得到a0(10倍)、a1(5倍)等等。只有while循环可以正常运行25次。
A0
B0
C0
D0
E0
A1
B1
C1
D1
E1
A2
B2
C2
D2
E2
A3
B3
C3
D3
E3
A1
B4
C4
D4
E4