我正在寫一個程序,我需要製作一個空心的asterixs盒子。當我輸入一個正方形的尺寸時,程序工作正常,但當我輸入一個矩形的尺寸時它不起作用。爲什麼程序輸出正確,如果尺寸創建一個正方形而不是矩形?
import java.util.Scanner;
public class asterix {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int stars; // length of side
int column; // width of box
int row = 1;
// prompt user to enter the length and width of box
System.out.print("Enter the length: ");
stars = input.nextInt();
System.out.print("Enter the width: ");
column = input.nextInt();
while (row <= stars) {
column = 1;
// and for as many columns as rows
while (column <= stars) {
if (row == 1)
System.out.print("*");
else if (row == stars)
System.out.print("*");
else if (column == 1)
System.out.print("*");
else if (column == stars)
System.out.print("*");
else
System.out.print(" ");
column++; } // end inner while loop
System.out.println();
row++;
} // end output
} // end of main
} // end of Stars
理想的輸出我想如下:
說,它的3×3:
*** * * ***
,並說,它的4 3:
**** * * ****
這是一個家庭作業嗎? – ewok
感謝編輯 –
沒有它的個人改進 –