[英]How can I stop my program from executing the next statement if the user inputs a specific number?
This program prompts the user to enter an odd integer that will decide the height of a specific diamond pattern. 该程序提示用户输入一个奇数整数,该整数将决定特定菱形图案的高度。 My program executes the other statements even though the user inputs one of the other number listed.
即使用户输入列出的另一个数字之一,我的程序也会执行其他语句。
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);}
Be careful with your coding style. 请注意您的编码风格。 You are not using { after your if conditions, therefore only the next statement is related to the if condition.
您没有在if条件之后使用{ ,因此只有下一条语句与if条件有关。 I suppose the user only can input a single number, therefore you should use else if conditions instead just other if .
我想用户只能输入一个数字,因此您应该使用else if条件,而只能使用other if 。
It would be better if you post the entire method, instead of just a part of it. 如果发布整个方法而不是其中的一部分,那会更好。
You could add "break;" 您可以添加“ break;” or "continue;"
或“继续;” at the bottom of each option depending on if you want to break out of or continue through the options.
在每个选项的底部,具体取决于您是要突破还是继续执行这些选项。
Also consider using a Switch case statement (Code supplied below is untested and just an example). 还可以考虑使用Switch case语句(下面提供的代码未经测试,仅是示例)。
I don't know if this is suitable for your needs but it seems you have a simple list of numbers which get selected - switches work great for those situations. 我不知道这是否适合您的需求,但似乎您有一个简单的数字列表可供选择-开关在这些情况下非常有用。 ie:
即:
pickANumber = sc.nextInt(); 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.