C#控制台中的Yes或No确认

3

我最近学习了一些C#。我完成了涵盖字符串和数字的前几个教程。

我想知道如何创建一个是或否的确认消息?

如果答案是否定的,我希望它会循环回到第一个问题。如果是肯定的,那么我还没有解决这部分:P,我希望继续阅读和学习,以便不断添加新内容。

using System;

namespace Digimon
{
    class Program
    {
        static void Main(string[] args)
        {
            string YourName = ""; string DigimonName = ""; string DigimonGender = "";
            Console.WriteLine("Welcome to Digimon World.\r");
            Console.WriteLine("created by Raw\n");

            //Ask for the digidestines name.
            Console.WriteLine("Jijimon: What is your name?");
            YourName = (Console.ReadLine());

            //Ask for the digimons name. 
            Console.WriteLine($"Jijimon: Great {YourName}, what is your Digimon's name?");
            DigimonName = (Console.ReadLine());

            //Ask for its sex.
            Console.WriteLine("Jijimon: Is it a boy or a girl?");
            DigimonGender = (Console.ReadLine());

            //Confirm details given in intro.
            Console.WriteLine($"Jijimon: Okay {YourName}, so your Digimon's a {DigimonGender} and its name is {DigimonName}?");
        }
    }
}

非常感谢大家提前的支持。


2
我会将Main函数中的功能放入一个单独的函数中,这样你就可以在answer为"no"时调用它。 - Kiwimanshare
1
尝试研究Console.ReadLineif结构。 - Alejandro
1
看一下 while - 似乎你想在答案是“否”的情况下循环遍历你的问题。因此,你可以创建一个变量 confirmed,如果答案是“是”,则将其设置为 true,然后只要输入不是“confirmed”,就可以循环遍历你的问题。 - Christoph Lütjen
1个回答

1

尝试

while (true)
{
    Console.WriteLine("Welcome to City Mail.");

    string YourName = ""; string DigimonName = ""; string DigimonGender = ""; Console.WriteLine("Welcome to Digimon World.\r"); Console.WriteLine("created by Raw\n");
    //Ask for the digidestines name.
    Console.WriteLine("Jijimon: What is your name?");
    YourName = (Console.ReadLine());

    //Ask for the digimons name. 
    Console.WriteLine($"Jijimon: Great {YourName}, what is your Digimon's name?");

    DigimonName = (Console.ReadLine());

    //Ask for its sex.
    Console.WriteLine("Jijimon: Is it a boy or a girl?");
    DigimonGender = (Console.ReadLine());

    //Confirm details given in intro.
    Console.WriteLine($"Jijimon: Okay {YourName}, so your Digimon's a {DigimonGender} and its name is {DigimonName}?");

    Console.WriteLine("Is this correct? (Y)es (N)o");
    ConsoleKeyInfo yesNo = Console.ReadKey(true);

    // if 'y' key is pressed
    if (yesNo.Key == ConsoleKey.Y)
    {
        Console.WriteLine("Exit and do something else..");
        break;
    }
}

错误添加。@DurgaPrasad - Oyeme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接