简体   繁体   English

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

[英]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.

相关问题 如何使程序停止接受输入并运行程序? - How can I get my program to stop accepting inputs and run the program? 当用户输入小数点时,如何错误处理代码以防止程序崩溃? - How can I error handle my code to prevent my program from crashing when the user inputs a decimal point? 如果用户输入了无效字符,我如何使我的程序向用户输出消息? - If a user inputs an invalid character how can I get my program to output a message to the user? 如果用户输入特定关键字,如何停止从扫描仪读取? - How to stop reading from a scanner if the user inputs a specific keyword? 我做了一个CLI计算器,我想知道,在用户输入停止命令之前,如何让程序不确定地接受数字? - I made a CLI calculator and I am wondering, how can I have the program accept numbers indefinetly until the user inputs a stop command? 如何编写一个程序来反转用户输入的数字 - How to write a program that reverses a number a user inputs 当用户输入无效的输入时,如何终止程序? - How can i terminate the program when the user inputs an invalid input? 如何更改我的程序以接受不同的输入? - how can i change my program to accept different inputs? 如何修复循环以在输入小于 1 且大于 50 的数字时停止并在输入超过 20 个时停止? - How can I fix my loop to stop when i enter number less than 1 and greater than 50 and to stop when more than 20 inputs? 用户输入错误的数据类型时如何停止程序? - how to stop the program when the user inputs the wrong type of data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM