简体   繁体   中英

C# how to make this code more compact (If,else if,else)

I'm first year student and we are asked to do assigment,I wonder is there any other way to check user input so I would not have to do all the if's and else's after each input or is there any way to loop it and save 6 different answers or something like that.....

    static void Main(string[] args)
    {
        //var
        string s0, s1, s2, s3, s4, s5; //subejects
        string g0, g1, g2, g3, g4, g5;//grades

        //input grades/subject
        Console.Write("Enter Subject 1  : ");
        s0 = Console.ReadLine();
        Console.Write("Enter Grade 1  : ");
        g0 = Console.ReadLine();
        g0 = g0.ToUpperInvariant();
        if (g0 == "H1")
            g0 = "100";
        else if (g0 == "H2")
            g0 = "88";
        else if (g0 == "H3")
            g0 = "77";
        else if (g0 == "H4")
            g0 = "66";
        else if (g0 == "H5" || g0 == "O1")
            g0 = "56";
        else if (g0 == "H6" || g0 == "O2")
            g0 = "46";
        else if (g0 == "H7" || g0 == "O3")
            g0 = "37";
        else if (g0 == "H8")
            g0 = "0";
        else if (g0 == "O4")
            g0 = "28";
        else if (g0 == "O5")
            g0 = "20";
        else if (g0 == "O6")
            g0 = "12";
        else if (g0 == "O7" || g0 == "O8")
            g0 = "0";
        else
            Console.WriteLine("Wrong Grade Format!");

        Console.Write("\nEnter Subject 2 : ");
        s1 = Console.ReadLine();
        Console.Write("Enter Grade 2  : ");
        g1 = Console.ReadLine();
        g1 = g1.ToUpperInvariant();

        Console.Write("\nEnter Subject 3 : ");
        s2 = Console.ReadLine();
        Console.Write("Enter Grade 3  : ");
        g2 = Console.ReadLine();
        g2 = g2.ToUpperInvariant();

        Console.Write("\nEnter Subject 4 : ");
        s3 = Console.ReadLine();
        Console.Write("Enter Grade 4  : ");
        g3 = Console.ReadLine();
        g3 = g3.ToUpperInvariant();

        Console.Write("\nEnter Subject 5 : ");
        s4 = Console.ReadLine();
        Console.Write("Enter Grade 5  : ");
        g4 = Console.ReadLine();
        g4 = g4.ToUpperInvariant();

        Console.Write("\nEnter Subject 6 : ");
        s5 = Console.ReadLine();
        Console.Write("Enter Grade 6  : ");
        g5 = Console.ReadLine();
        g5 = g5.ToUpperInvariant();

        Console.Clear();

        //collect information into array
        for (int i = 0; i <= 5; i++)
        {
            string[] subjects = { s0, s1, s2, s3, s4, s5 };
            string[] grades = { g0, g1, g2, g3, g4, g5 };

            Console.WriteLine("{0,15}:{1,-15}", subjects[i],grades[i]);//output
        }
    }
}

Please don't be mad if im asking stupidly easy question but all I can find in web is much more advanced ways of doing it....Thanks.

I suggest initialising a dictionary to do the lookup like so:

var lookup = new Dictionary<string, string>
{
    ["H1"] = "100",
    ["H2"] = "88",
    ["H3"] = "77"
    // Etc.
};

Then look up the strings as follows:

g0 = g0.ToUpperInvariant();

if (!lookup.TryGetValue(g0, out g0))
    Console.WriteLine("Wrong Grade Format!");

You can use switch

switch(g0){
    case "H1":
        g0 = 100;
        break;
    case "H2":
        g0 = 88;
        break;
    case "H3":
        g0 = 73;
        break;
    default:
        Console.WriteLine("Wrong Grade Format!");
        break;
}

The switch is like your if-else code internally. In the parameter you put the var to check and then you check each case with the value case "H3" is the same as if(g0 == "H3") . After the : and before the break; you put the code you want to do if that condition is true . If none of the conditions are true , the switch goes to default: and execute that code.

judging from your problem, you're somewhat looking for patterns within your input to shorten your conditions. If so, that might be a little bit rough since you really need to interpret every input the user might type and do so with conditions no matter how long.

But despite that, I've attempted to indeed shorten the conditions through the use of patterns I've analyzed based from your code snippet and user input.

This is as far as I can give you for code snippet:

var totalGrade0 = 100;
if (g0.Contains("H")) {
    var gradeVal = Convert.ToInt32(g0.Replace("H", ""));
    var initDeductionSet = 12;
    g0 = totalGrade0.ToString();
    if (gradeVal == 2) {
        g0 = (totalGrade0 - initDeductionSet).ToString();
    }
    if (gradeVal >= 4 && gradeVal <= 6) {
        g0 = (totalGrade0 - (10 * (gradeVal - 2)) + initDeductionSet).ToString();
    }
    if (gradeVal == 7) {
        g0 = (totalGrade0 - 63).ToString();
    }

}

I noticed that the input is using H1, H2, H3 to define grade codes and give respective grades based from the code. And also, grades were deducted the higher code is given(eg H7. Though I didn't included those O1, O2 in my code snippet. Just to give you an idea). Using the integer value from the grade code to my advantage, I was able to see a pattern and use it to automate the deduction process a little bit, effectively reducing the conditions. This pattern is noticeable from H4-H6 where they deduct the grade by 10. Other than that, I don't see anything that might reduce the condition further. Perhaps if you could see some more patterns which are noticeable in the grade code system, you might be able to reduce it a few notches.

The key here is to parse the grade code the users inputted(H1, H2, etc.) and extract the integer value in the code.

If you're looking for a cleaner approach of if-else condition, you might try to use

Dictionary / Hash tables

or

switch-case

approach in which you haven't encountered yet I believe. But know that these approach won't shorten your conditions. But they will further make your code readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM