开发者

Can't seem to loop switch statement

What I am trying to do, is after the person puts in their name, ask them if they'd like to do it again, if not, hit n, and exit the program. But I can't seem to get it to work. I've tried adding an exit case, still nothing.

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


You are not changing 'answer' variable in the loop. You're missing a Console.ReadLine() in loop for it.

 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

Also you should read the name inside the loop since you'd want to read it again when exit is false. Right now you're checking the same name over and over.

static void Test() <---- remove the name parameter
{
    bool exit = true;
    string answer = "";

    do
    {
      Console.Write("Please enter your name: ");
      string name = Console.ReadLine().ToLower();     <--- read name here


answer = Console.ReadLine(); // you're missing this line in your code.

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

}


Combining the points made by Hasan and Harsh, here's a working sample that seems to do what you're hoping for.

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

Of course, if you enter a name that isn't "name1" or "name2", then things start getting weird because you haven't really defined what you want to have happen in that case. But hopefully this gets you off to a good start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜