是否有任何方法可以简化不同行的x列数量的打印,使其加起来总共为50个项目?



我解决了这个任务,其中用户输入X行。然后,程序确定需要多少列才能使项目总数达到50。如果它添加了另一个列,那么这个列最终会有太多的项,所以我需要弄清楚如何不打印这些额外的字符。我确实想出了一个解决办法,但我想知道是否有更简单的方法。

public class J1_Lab08_3 {
public static void main(String[] args) {
userInput();
}
public static void userInput(){
Scanner input = new Scanner(System.in); // scanner object
int rowAmount;
char[][] array;
while(true) {
System.out.println("Please enter the amount of rows 1-50. Enter 0 to quit program"); // asking the user to enter row amount
rowAmount = input.nextInt();
if(rowAmount == 0){
System.out.println("Thank you.");
System.exit(0);
} else if(rowAmount >= 51){
System.out.println("Please enter a value less than 50");
System.exit(0);
}
array = createArray(rowAmount); // created array
print2dCharArray(array); // prints array
}
}
public static char[][] createArray(int sizeX){
char[][] array = new char[sizeX][(50+sizeX-1)/sizeX];
int remainder = 50%sizeX;
int counter = 0;
for(int i = 0; i < sizeX; i++){ // goes through each row in the array
for(int j = 0; j < 50/sizeX; j++){ // goes through each column in the array
array[i][j] = (char)('a' + (Math.random() * ('z' - 'a'))); // creates a random letter from a-z
}
}
while(remainder > 0){
array[counter][(50/sizeX)] = (char)('a' + (Math.random() * ('z' - 'a')));
counter++; // goes to next row in array
remainder--; // subtracts by one to know when to stop going down rows
}
return array; // returns the array that was created with random characters
}
public static void print2dCharArray(char[][] array){
for(int i = 0; i < array.length; i++){ // goes through row of array
for(int j = 0; j < array[0].length; j++){ // goes through column of array
if(array[i][j] != 0) { // gets rid of remaining zeros
System.out.print("(" + array[i][j] + ") "); // prints row and column
}
}
System.out.println(); // prints a new line to start printing new row
}
System.out.println();
}
}

使用编号为7行的示例,我为此所做的是,我创建了非余数的列或数组。因此,对于7行,它首先创建7列,总共需要8列才能达到50列。然后,它将检查是否有余数,如果有余数,则添加另一列,每次添加一列,它都会降低余数,直到余数为0。

编辑:我添加了一个变量来控制需要打印多少项

public static char[][] createArray(int sizeX){
int pairAmount = 50; // total amount of items
char[][] array = new char[sizeX][(pairAmount+sizeX-1)/sizeX];
int remainder = pairAmount%sizeX;
int counter = 0;
for(int i = 0; i < sizeX; i++){ // goes through each row in the array
for(int j = 0; j < pairAmount/sizeX; j++){ // goes through each column in the array
array[i][j] = (char)('a' + (Math.random() * ('z' - 'a'))); // creates a random letter from a-z
}
}
while(remainder > 0){
array[counter][(pairAmount/sizeX)] = (char)('a' + (Math.random() * ('z' - 'a')));
counter++; // goes to next row in array
remainder--; // subtracts by one to know when to stop going down rows
}
return array; // returns the array that was created with random characters
}

将其保持为50,并输入一个数字,例如5。将打印到控制台

使用5作为行数,50作为项目总数

使用6作为行数,50作为项目总数

如果totalItems更改为24,并且我们使用相同的数字(5和6),它也会生成

使用5作为行数,24作为项目总数

使用6作为行数,24作为项目总数

您可以通过在循环中添加剩余字符来稍微简化它。你可以简单地计算你已经加了多少。例如

public static char[][] createArray(int sizeX){
int pairAmount = 50; // total amount of items
int columnAmount = (pairAmount+sizeX-1)/sizeX;
char[][] array = new char[sizeX][columnAmount];
for(int i = 0; i < sizeX; i++){ // goes through each row in the array
for(int j = 0; j < columnAmount && pairAmount > 0; j++){ // goes through each column in the array
array[i][j] = (char)('a' + (Math.random() * ('z' - 'a'))); // creates a random letter from a-z
pairAmount--;
}
}
return array; // returns the array that was created with random characters
}

您可以通过执行pairAmount % sizeX获得具有额外列/元素的前n行,例如对于pairAmount = 50 and sizeX = 6: pairAmount % sizeX = 2,这意味着前两行将有9个元素。您可以在内部循环中添加此条件,并完全摆脱while循环:

public static char[][] createArray(int sizeX){
int pairAmount = 50; 
char[][] array = new char[sizeX][(int) Math.ceil((double) pairAmount / sizeX)];  // I think this way it is clearer, but you can keep your approach if you don't like it
for(int i = 0; i < sizeX; i++){ 
for(int j = 0; j < ((i < pairAmount % sizeX) ? array[i].length : pairAmount/sizeX); j++){ 
array[i][j] = (char)('a' + (Math.random() * ('z' - 'a'))); 
}
}
return array;
}

或引入一个变量,如果你不觉得上面的内容是不可读的:

public static char[][] createArray(int sizeX){
int pairAmount = 50;
char[][] array = new char[sizeX][(int)Math.ceil((double) pairAmount / sizeX)];
for(int i = 0; i < sizeX; i++){
int maxColumn = (i < pairAmount % sizeX) ? array[i].length : pairAmount / sizeX;
for(int j = 0; j < maxColumn; j++){
array[i][j] = (char)('a' + (Math.random() * ('z' - 'a')));
}
}
return array;
}

相关内容

最新更新