繁体   English   中英

如果用户输入特定数字,如何停止我的程序执行下一条语句?

[英]How can I stop my program from executing the next statement if the user inputs a specific number?

该程序提示用户输入一个奇数整数,该整数将决定特定菱形图案的高度。 即使用户输入列出的另一个数字之一,我的程序也会执行其他语句。

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条件之后使用{ ,因此只有下一条语句与if条件有关。 我想用户只能输入一个数字,因此您应该使用else if条件,而只能使用other if

如果发布整个方法而不是其中的一部分,那会更好。

您可以添加“ 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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM