對於類項目,我必須編寫一個代碼,用於打印由用戶指定長度的星號框。我們必須製作兩種類型的盒子;基本和對角線。基本的盒子只是一個由星號組成的普通盒子,我已經做過。對角線箱子必須有它的內部對角線,看起來像這樣: http://prntscr.com/3fbbot在Java框中打印對角線
這裏是我的代碼至今:
public static void main(String[] args) {
Scanner type = new Scanner(System.in);
Scanner number = new Scanner(System.in);
Boolean f = true;
while (f) {
System.out.print("Enter a box type, basic or diagonal: ");
String g = type.nextLine();
if (g.equals("basic") || g.equals("diagonal")) {
}
else {
continue;
}
System.out.print("Enter a number between 2 - 16: ");
try {
int boxSize = number.nextInt();
if (g.equals("basic")) {
if (boxSize >= 2 && boxSize <= 16) {
for (int i = 0; i < boxSize * boxSize;i++) {
int row = i/boxSize;
int col = i % boxSize;
if (row == 0 && col < boxSize-1) {
System.out.print("*");
}
else if (col == 0) {
System.out.print("*");
}
else if (col == (boxSize -1)) {
System.out.println("*");
}
else if (row == (boxSize - 1)) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
}
else {
System.out.println("Please use a proper integer.");
}
System.out.print("Make another square? Type yes or no: ");
Scanner answer = new Scanner(System.in);
if (answer.nextLine().equals("no")) {
System.out.print("Thanks for playing!");
System.exit(0);
}
}
else {
}
}
catch(Exception e){
System.out.println("RESETTING. Please type an integer this time.");
}
}
}
如果你需要我更加具體或需要更多細節,請問。提前致謝。
你會想問一個具體問題,儘可能詳細說明你的困惑點。越詳細越好。現在,我們所看到的只是一些需求和一些代碼,就是這樣。 –