Java 如果用户输入一个特定的数字,我如何停止程序执行下一个语句?
此程序提示用户输入奇数整数,该整数将决定特定菱形图案的高度。我的程序执行其他语句,即使用户输入列出的另一个数字之一Java 如果用户输入一个特定的数字,我如何停止程序执行下一个语句?,java,Java,此程序提示用户输入奇数整数,该整数将决定特定菱形图案的高度。我的程序执行其他语句,即使用户输入列出的另一个数字之一 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 <
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);}
if((数字%2==0)| |数字0;行-=2){
for(int col=0;col<9-行/2;col++)
系统输出打印(“”);
for(int col=0;col0;行-=2){
对于(int col=0;col<7-行/2;col++)
系统输出打印(“”);
for(int col=0;col0;行-=2){
for(int col=0;col<5-行/2;col++)
系统输出打印(“”);
for(int col=0;col0;行-=2){
for(int col=0;col<3-行/2;col++)
系统输出打印(“”);
for(int col=0;col|
小心你的编码风格。在您的if条件之后没有使用{,因此只有下一个语句与if条件相关。我假设用户只能输入一个数字,因此您应该使用else if条件,而不是其他if条件
最好是发布整个方法,而不仅仅是其中的一部分。您可以在每个选项的底部添加“break;”或“continue;”,具体取决于您是否要中断或继续执行这些选项
也考虑使用一个开关case语句(下面提供的代码是未经测试的,只是一个例子)。 我不知道这是否适合你的需要,但似乎你有一个简单的数字列表,可以选择-开关在这些情况下工作的伟大。 i、 e:
pickANumber=sc.nextInt()开关(pickANumber){
案例3:
对于(int行=1;行<4;行+=2){
for(int col=0;col<4-1-row/2;col++)
系统输出打印(“”);
for(int col=0;col0;行-=2){
for(int col=0;col<3-行/2;col++)
系统输出打印(“”);
for(int col=0;col0;行-=2){
for(int col=0;col<5-行/2;col++)
系统输出打印(“”);
for(int col=0;col 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");
|