对于类项目,我必须编写一个代码,用于打印由用户指定长度的星号框。我们必须制作两种类型的盒子;基本和对角线。基本的盒子只是一个由星号组成的普通盒子,我已经做过。对角线箱子必须有它的内部对角线,看起来像这样: 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.");
}
}
}
如果你需要我更加具体或需要更多细节,请问。提前致谢。
你会想问一个具体问题,尽可能详细说明你的困惑点。越详细越好。现在,我们所看到的只是一些需求和一些代码,就是这样。 –