虽然Loop不会遍历所有7个案例

0 c# loops while-loop

我得到案例1-3工作,但我不能工作4-7或不知道如何键入或接近它.

正如您在第4号中所看到的那样,我编程,所以当我运行程序并按4时它应该运行案例4倒数到10但跳过7为什么它不工作?也无法弄清楚怎么做5-7.

我还没有学到很多,如果可以帮助我,并解释你的代码是如何为我解决问题的,那将是非常合适的.

namespace WhileLoopExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int selection = 0;
            while (selection != 9)
            {
                Console.Clear();
                Console.WriteLine("\n\n Menu:\n");

                Console.WriteLine("\t 1. Display 10 stars( one per line)");
                Console.WriteLine("\t 2. Request a value 1 to 50 from user, then display");
                Console.WriteLine("\t 3. Display 10 rows of 3 stars");
                Console.WriteLine("\t 4. Add all values between 1 to 10 except 7");
                Console.WriteLine("\t 5. Display all the even numbers between 10 and 100");
                Console.WriteLine("\t 6. Add all the odd numbers bewteen 10 and 100");
                Console.WriteLine("\t 7. Generate 6 random numbers ranging from 1 to 49");
                Console.WriteLine("\t 8. Exit Apllication");

                Console.Write("Enter your selection: ");
                selection = int.Parse(Console.ReadLine());

                switch (selection)
                {
                    case 1:
                        {
                            int counter = 1;
                            while (counter <= 10)
                            {
                                Console.WriteLine("*");
                                counter++;
                            }
                        }
                        break;
                    case 2:
                        {
                            Console.Write("Enter integer 0-50 : ");
                            int N = int.Parse(Console.ReadLine());
                            int counter = 1;
                            while (counter <= N)
                            {
                                Console.Write("*");
                                counter++;
                            }
                        }
                        break;
                    case 3:
                        {
                            int counter = 1;
                            while (counter <= 10)
                            {
                                Console.WriteLine("***");
                                counter++;
                            }
                        }
                        break;
                    case 4:
                        {
                            int counter = 1;
                            while (counter <= 10)
                            {
                                if (counter == 7)
                                {

                                    counter++;
                                }
                            }
                        }
                        break;
                    case 5:
                        {
                            int counter = 1;
                            while (counter <= 100)
                            {

                            }
                        }
                        break;
                    case 6:
                        {
                        }
                        break;
                    case 7:
                        {


                        }
                        break;

                }// end of switch
                //pause
                Console.WriteLine("\n\nHit any key to coutinue");
                Console.ReadKey();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

堆栈溢出时我们无法为您完成作业.但是,我们可以指出您的逻辑问题.

看看案例四,我们看到了

                case 4:
                    {
                        int counter = 1;
                        while (counter <= 10)
                        {
                            if (counter == 7)
                            {
                                counter++;
                            }
                        }
                    }
Run Code Online (Sandbox Code Playgroud)

现在就解决这个问题.我们从1的计数器开始,当它小于或等于10时,我们输入你的if语句.

你的if语句说"如果我的计数器等于7,那么将计数器增加(增加)一." 但是,您的计数器输入1,并且没有其他逻辑定义计数器的行为,因此没有任何反应!这个循环然后永远继续(哎呀!).考虑到这一点,再试一次.

  • 你怎么知道这是作业?这是每所学校的标准课吗?如果用户实际上没有学到任何东西,那么一个成功的家庭作业真的很重要吗? (2认同)