Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t似乎不循环switch语句_C# - Fatal编程技术网

C# Can';t似乎不循环switch语句

C# Can';t似乎不循环switch语句,c#,C#,我想做的是,在这个人输入他们的名字后,问他们是否愿意再做一次,如果不愿意,点击n,然后退出程序。但我似乎无法让它发挥作用。我试着添加一个退出案例,但仍然没有结果 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestApp { class Program { static void Main() { Conso

我想做的是,在这个人输入他们的名字后,问他们是否愿意再做一次,如果不愿意,点击n,然后退出程序。但我似乎无法让它发挥作用。我试着添加一个退出案例,但仍然没有结果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestApp
{
    class Program
{
    static void Main()
    {
        Console.Write("Please enter your name: ");
        string name = Console.ReadLine();
        Test(name.ToLower());
        Console.ReadLine();

    }

    static void Test(string name)
    {
        bool exit = true;
        string answer = "";

        do
        {
            switch (name)
            {
                case "name":
                    Console.WriteLine("Hello Name");
                    break;
                case "name2":
                    Console.WriteLine("Hello Name2");
                    break;
            }

            Console.WriteLine("Would you like to enter a new name? y/n: ");
            if (answer == "y")
                exit = false;
            else
                exit = true;
        }
        while (exit == false);
    }
}
}

您没有更改循环中的“应答”变量。您在其循环中缺少一个Console.ReadLine()

 Console.Write("Would you like to enter a new name? y/n: ");
 answer = Console.ReadLine(); <----------- this was missing
 exit = (answer == "y" || answer == "Y"); <------- this slight improvement
Console.Write(“是否要输入新名称?y/n:”);
answer=Console.ReadLine() answer=Console.ReadLine();//代码中缺少这一行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestApp
{
    class Program
{
    static void Main()
    {
        Console.Write("Please enter your name: ");
        string name = Console.ReadLine();
        Test(name.ToLower());
        Console.ReadLine();

    }

    static void Test(string name)
    {
        bool exit = true;
        string answer = "";

        do
        {
            switch (name)
            {
                case "name":
                    Console.WriteLine("Hello Name");
                    break;
                case "name2":
                    Console.WriteLine("Hello Name2");
                    break;
            }

            Console.WriteLine("Would you like to enter a new name? y/n: ");
            answer = Console.ReadLine();   // you're missing this line in your code.
            if (answer == "y")
                exit = false;
            else
                exit = true;
        }
        while (exit == false);
    }
}

}

结合Hasan和Harsh的观点,这里有一个工作示例,似乎满足了您的期望

static void Test()
{
    bool exit = true;
    string answer = "";

    do
    {
        Console.Write("Please enter your name: ");
        string name = Console.ReadLine();

        switch (name)
        {
            case "name":
                Console.WriteLine("Hello Name");
                break;
            case "name2":
                Console.WriteLine("Hello Name2");
                break;
        }

        Console.WriteLine("Would you like to enter a new name? y/n: ");
        answer = Console.ReadLine();
        if (answer == "y")
            exit = false;
        else
            exit = true;
    }
    while (!exit);
}

当然,如果您输入的名称不是“name1”或“name2”,那么事情开始变得奇怪,因为您还没有真正定义在这种情况下希望发生什么。但希望这能让你有一个好的开始。

它目前在做什么?当你说某事“不起作用”时,最好描述一下“不起作用”的含义。这是不是给了你一个错误?如果是,错误是什么。如果没有,它应该(不)做什么?这方面需要任何帮助,或者你可以接受有用的答案。啊,嗯,我补充说,问题是,我为什么打“y”。它只是一遍又一遍地问同样的问题。
static void Test()
{
    bool exit = true;
    string answer = "";

    do
    {
        Console.Write("Please enter your name: ");
        string name = Console.ReadLine();

        switch (name)
        {
            case "name":
                Console.WriteLine("Hello Name");
                break;
            case "name2":
                Console.WriteLine("Hello Name2");
                break;
        }

        Console.WriteLine("Would you like to enter a new name? y/n: ");
        answer = Console.ReadLine();
        if (answer == "y")
            exit = false;
        else
            exit = true;
    }
    while (!exit);
}