第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎樣才能擺脫這個循環(huán)?

我怎樣才能擺脫這個循環(huán)?

C#
繁星coding 2023-08-13 13:53:43
我是編碼的初學者 - 我一周前才開始。我正在嘗試開發(fā)一個程序,用戶可以在其中查看列表中的所有名稱或將名稱添加到列表中。當用戶提示查看列表中的名稱,然后決定采取另一個操作時,他們會陷入只能再次看到名稱的無限循環(huán)。我希望他們能夠返回到開頭并選擇查看名稱或添加名稱。我嘗試過重新整理和查找,但我仍然不知所措。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;                        }                    }我希望用戶返回到開始的提示,但他們一遍又一遍地陷入相同的提示中。沒有錯誤消息。
查看完整描述

2 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

有兩件事可以解決你的困難。

首先,輸入提示應該位于循環(huán)中,以便它可以顯示每個循環(huán)何時開始。

其次,do while循環(huán)決定循環(huán)是否應該在設計的最后繼續(xù);這取決于應直接用作 while 條件的用戶輸入。

因此,您的代碼可以簡化為

public static void Main()

{

    List<string> names = new List<string>() { "Vasti", "Cameron", "Ezra", "Tilly" };


    string userChoice = "";

    do

    {

        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.");

        userChoice = Console.ReadLine();


        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);

                }


                break;

            default:

                Console.WriteLine("Please type either 1 or 2 to select an option.");

                break;

        }

        Console.WriteLine(@"Wanna do that again? Type yes or no.");

        userChoice = Console.ReadLine();

    }

    while (userChoice.Equals("yes", StringComparison.OrdinalIgnoreCase));


    Console.WriteLine("Program finished");

    Console.ReadLine();

}


查看完整回答
反對 回復 2023-08-13
?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

將 do{}while() 循環(huán)更改為常規(guī) while(){} 循環(huán)。Move string userChoice = Cosole.ReadLine(); 在頂部 while 循環(huán)內部,否則您將永遠不會要求其他選項。。。還要在開頭設置 ((bool program=true; 和 bool program=true;))。Do{}while() 循環(huán)首先執(zhí)行 {} 內的所有操作,然后檢查 while 語句是否仍然為 true。



查看完整回答
反對 回復 2023-08-13
  • 2 回答
  • 0 關注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號