我怎样才能摆脱这个循环?

我是编码的初学者 - 我一周前才开始。我正在尝试开发一个程序,用户可以在其中查看列表中的所有名称或将名称添加到列表中。当用户提示查看列表中的名称,然后决定采取另一个操作时,他们会陷入只能再次看到名称的无限循环。我希望他们能够返回到开头并选择查看名称或添加名称。


我尝试过重新整理和查找,但我仍然不知所措。


using System;

using System.Linq;

using System.Collections.Generic;


public class Program

{

    public static void Main()

    {

        List<string> names = new List<string>();

        names.Add"Vasti");

        names.Add("Cameron");

        names.Add("Ezra");

        names.Add("Tilly");

        bool program = false;

        bool program2 = false;

        Console.WriteLine("Welcome to the names lost! If you wish to add 

        a name to the list, type 1. If you want to see current names in 

        the list, 

        type 2.");

        string userChoice = Console.ReadLine();

        do

        {

            switch (userChoice)

            {

                case "1":

                    Console.WriteLine("Add a name to the squad.");

                    string userAddName = Console.ReadLine();

                    names.Add(userAddName);

                    break;

                case "2":

                    Console.WriteLine("Here's the list:");

                    foreach (string name in names)

                    {

                        Console.WriteLine(name);

                     }


                    Console.WriteLine("Wanna do that again? Type yes or 

                    no.");

                    do

                        {

                    string userContinue = Console.ReadLine();


                        switch (userContinue)

                        {

                            case "yes":

                                program = true;

                                program2 = false;

                                break;

                            case "Yes":

                                program = true;

                                program2 = false;

                                break;

                        }


                    }


我希望用户返回到开始的提示,但他们一遍又一遍地陷入相同的提示中。没有错误消息。


繁星coding
浏览 115回答 2
2回答

MM们

有两件事可以解决你的困难。首先,输入提示应该位于循环中,以便它可以显示每个循环何时开始。其次,do while循环决定循环是否应该在设计的最后继续;这取决于应直接用作 while 条件的用户输入。因此,您的代码可以简化为public static void Main(){&nbsp; &nbsp; List<string> names = new List<string>() { "Vasti", "Cameron", "Ezra", "Tilly" };&nbsp; &nbsp; string userChoice = "";&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($@"Welcome to the names lost!{Environment.NewLine} If you wish to add a name to the list, type 1.{Environment.NewLine} If you want to see current names in the list, type 2.");&nbsp; &nbsp; &nbsp; &nbsp; userChoice = Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; switch (userChoice)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Add a name to the squad.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string userAddName = Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names.Add(userAddName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "2":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Here's the list:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (string name in names)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Please type either 1 or 2 to select an option.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(@"Wanna do that again? Type yes or no.");&nbsp; &nbsp; &nbsp; &nbsp; userChoice = Console.ReadLine();&nbsp; &nbsp; }&nbsp; &nbsp; while (userChoice.Equals("yes", StringComparison.OrdinalIgnoreCase));&nbsp; &nbsp; Console.WriteLine("Program finished");&nbsp; &nbsp; Console.ReadLine();}

沧海一幻觉

将 do{}while() 循环更改为常规 while(){} 循环。Move string userChoice = Cosole.ReadLine();&nbsp;在顶部 while 循环内部,否则您将永远不会要求其他选项。。。还要在开头设置 ((bool program=true; 和 bool program=true;))。Do{}while() 循环首先执行 {} 内的所有操作,然后检查 while 语句是否仍然为 true。
打开App,查看更多内容
随时随地看视频慕课网APP