此程序提示用户输入一个奇数,该整数将决定特定菱形图案的高度。即使用户输入了列出的另一个号码,我的程序也会执行其他语句。
if ((number% 2 == 0)|| number <=0 )System.out.println("--- The number you entered is not odd positive!! Please try again!");
else
{if (number == 9)
for (int row = 1 ; row < 10 ; row += 2) {
for (int col = 0 ; col < 10 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 7 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 9 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println(""); }
System.out.println("Here is the diamond shape, whose height is " + number);
//-----------------------------------------------------------------------
if (number==7)
for (int row = 1 ; row < 8 ; row += 2) {
for (int col = 0 ; col < 8 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 5 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 7 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");}
System.out.println("Here is the diamond shape, whose height is " + number);
//---------------------------------------------------------------------------
if (number == 5)
for (int row = 1 ; row < 6 ; row += 2) {
for (int col = 0 ; col < 6 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 3 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 5 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");}
System.out.println("Here is the diamond shape, whose height is " + number);
//--------------------------------------------------------------------------
if (number == 3)
for (int row = 1 ; row < 4 ; row += 2) {
for (int col = 0 ; col < 4 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 1 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 3 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");}
System.out.println("Here is the diamond shape, whose height is " + number);}
答案 0 :(得分:0)
小心你的编码风格。您没有在 {条件之后使用 {,因此只有下一条语句与 if 条件相关。我想用户只能输入一个号码,因此你应该使用 else if 条件而不是其他 if 。
如果您发布整个方法,而不仅仅是其中的一部分,那会更好。
答案 1 :(得分:0)
你可以添加“break;”或“继续”在每个选项的底部,取决于您是想要突破还是继续选择。
还要考虑使用Switch case语句(下面提供的代码是未经测试的,只是一个例子)。
我不知道这是否适合您的需求,但似乎您有一个简单的数字列表可供选择 - 交换机非常适合这些情况。 即:
pickANumber = sc.nextInt();
switch (pickANumber) {
case 3:
for (int row = 1 ; row < 4 ; row += 2) {
for (int col = 0 ; col < 4 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 1 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 3 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");}
System.out.println("Here is the diamond shape, whose height is " + number);}
break;
case 5:
for (int row = 1 ; row < 6 ; row += 2) {
for (int col = 0 ; col < 6 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 3 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 5 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");}
System.out.println("Here is the diamond shape, whose height is " + number);
break;
case 7:
for (int row = 1 ; row < 8 ; row += 2) {
for (int col = 0 ; col < 8 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 5 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 7 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");}
System.out.println("Here is the diamond shape, whose height is " + number);
break;
case 9:
for (int row = 1 ; row < 10 ; row += 2) {
for (int col = 0 ; col < 10 - 1 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println("");
}
for (int row = 7 ; row > 0 ; row -= 2) {
for (int col = 0 ; col < 9 - row / 2 ; col++)
System.out.print(" ");
for (int col = 0 ; col < row ; col++)
System.out.print("*");
System.out.println(""); }
System.out.println("Here is the diamond shape, whose height is " + number);
break;
default: System.out.println("Invalid option entered - please try again");