Home > database >  Can I loop through a Switch using C#?
Can I loop through a Switch using C#?

Time:09-12

I'm attempting to loop through a switch in C# for a simple console application that returns data based on user input. I am new to coding. I know that the Switch works as I tested it before trying to implement my loop. But when I add the code for loop to my program I cannot get it to provide the data.

I tried taking the switch out completely and using independent If statements for each "case" but this was not working either. I'd really like to see the switch work in the loop if it's possible.

   static void Main(string[] args)
    {
        var repeat = true;
        while(repeat)
        { 
            Console.WriteLine("Hello. Which instrument would you like to review? Type Quit to exit the program.");

            string instruments = Console.ReadLine();

            var action = Console.ReadLine();
            if (action == instruments)
            {

                switch (instruments)
                {
                    case "Jazzmaster":
                        string jazzmasterMake = "Fender";
                        string jazzmasterModel = "Jazzmaster";
                        string jazzmasterType = "guitar";
                        string jazzmasterCountry = "Japan";
                        int jazzmasterYear = 1997;
                        string jazzmasterSerial = "A019459";
                        string jazzmasterColor = "Sunburst";

                        {
                            Console.WriteLine("Your "   jazzmasterMake   " "   jazzmasterModel   " is a "   jazzmasterType   " made in "   jazzmasterCountry
                                  " in "   jazzmasterYear   " in a "   jazzmasterColor   " color and with a serial number of "   jazzmasterSerial   ".");
                        }
                        break;

                    case "Jaguar":
                        string jaguarMake = "Fender";
                        string jaguarModel = "Jaguar";
                        string jaguarType = "guitar";
                        string jaguarCountry = "Japan";
                        int jaguarYear = 1997;
                        string jaguarSerial = "A035931";
                        string jaguarColor = "White";

                        {
                            Console.WriteLine("Your "   jaguarMake   " "   jaguarModel   " is a "   jaguarType   " made in "   jaguarCountry
                                  " in "   jaguarYear   " in a "   jaguarColor   " color and with a serial number of "   jaguarSerial   ".");
                        }
                        break;

                    case "Stratocaster":
                        string stratocasterMake = "Fender";
                        string stratocasterModel = "Stratocaster";
                        string stratocasterType = "guitar";
                        string stratocasterCounty = "USA";
                        int stratocasterYear = 2018;
                        string stratocasterSerial = "US18004688";
                        string stratocasterColor = "Black";

                        {
                            Console.WriteLine("Your "   stratocasterMake   " "   stratocasterModel   " is a "   stratocasterType   " made in "   stratocasterCounty
                                  " in "   stratocasterYear   " in a "   stratocasterColor   " color and with a serial number of "   stratocasterSerial   ".");
                        }
                        break;

                    default:
                        Console.WriteLine("That instrument is not in your collection.");
                        break;
                }
            }
            
            else if (action == "Quit")
                 {
                    repeat = false;
                 }      
        }
    }
}

}

CodePudding user response:

I would just remove action and instruments and the if else and just call the input you get choice, because it's what the user wants. I'm not going to recreate your whole code here, but:

static void Main(string[] args)
{
    var repeat = true;
    while(repeat)
    { 
        Console.WriteLine("Hello. Which instrument would you like to review? Type Quit to exit the program.");

        string choice = Console.ReadLine();

        switch (choice)
        {
            case "Jazzmaster":
                // Jazzmaster stuff
                break;
            case "Jaguar":
                // Jaguar stuff
                break;
            case "Stratocaster":
                // Stratocaster stuff
                break;
            case "Quit"
                repeat = false;
                break;
            default:
                Console.WriteLine("That instrument is not in your collection.");
                break;
        }
    }
}

This seems much easier to understand what's going on. There are other tricks you can play to clean up your code, like creating an abstract Instrument class that overrides ToString(), to print whatever attributes you want, but that's beyond the scope of the question you've asked.

Ultimately the problem is that you're trying to get TWO inputs from your existing code.

CodePudding user response:

For the code you shared, you have to to type in instrument twice to get a review. Problem is caused here:

Console.WriteLine("Hello. Which instrument would you like to review? Type Quit to exit the program.");

string instruments = Console.ReadLine();

var action = Console.ReadLine();
if (action == instruments)

Notice Console.ReadLine() is executed twice. In order to enter the if, you have to have have the two input strings that match.

CodePudding user response:

Here's an alternative way of implementing your code. I post it not as a direct answer to your question, but as a way of showing an alternative to help your learning.

static void Main(string[] args)
{
    Dictionary<string, Instrument> instruments = new Dictionary<string, Instrument>()
    {
        { "Jazzmaster", new Instrument() { Make = "Fender", Model = "Jazzmaster", Type = "guitar", Country = "Japan", Year = 1997, Serial = "A019459", Color = "Sunburst", } },
        { "Jaguar", new Instrument() { Make = "Fender", Model = "Jaguar", Type = "guitar", Country = "Japan", Year = 1997, Serial = "A035931", Color = "White", } },
        { "Stratocaster", new Instrument() { Make = "Fender", Model = "Stratocaster", Type = "guitar", Country = "USA", Year = 2018, Serial = "US18004688", Color = "Black", } },
    };
    while (true)
    {
        Console.WriteLine("Hello. Which instrument would you like to review? Type Quit to exit the program.");
        string input = Console.ReadLine();
    
        if (input.ToLowerInvariant() == "quit")
        {
            break;
        }
        else if(instruments.ContainsKey(input))
        {
            Instrument instrument = instruments[input];
            Console.WriteLine($"Your {instrument.Make} {instrument.Model} is a {instrument.Type} made in {instrument.Country} in {instrument.Year} in a {instrument.Color} color and with a serial number of {instrument.Serial}.");
        }
        else
        {
            Console.WriteLine("That instrument is not in your collection.");
        }
    }
}

public class Instrument
{
    public string Make { get; init; }
    public string Model { get; init; }
    public string Type  { get; init; }
    public string Country { get; init; }
    public int Year { get; init; }
    public string Serial { get; init; }
    public string Color { get; init; }
}
  • Related