到目前为止,这是我的代码,它在大多数情况下都有效,但是,它只显示零。我无法弄清楚如何让它显示星号,因为这是我知道如何让它显示内部有东西的二维数组的唯一方法。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int length = 0;
int width = 0;
Scanner input = new Scanner(System.in);
//ask user input of array numbers
while (length <= 20 || width <= 20) {
System.out.print("Enter the length: ");
length = input.nextInt();
System.out.print("Enter the Width: ");
width = input.nextInt();
int[][] myarray = new int[width][length]; //To print all elements in this array of ints,
//loops is used to make it shorter and efficient
for (int w = 0; w < length; w++) {
for (int l = 0; l < width; l++) {
System.out.print(" " + myarray[l][w]);//prints it in grid fashion
}
System.out.println("");
}
}
}
}