Returning to top of program to try again

后端 未结 1 730
梦毁少年i
梦毁少年i 2021-01-13 12:01

I am teaching myself C# and the current chapter challenge asked me to:

Take your last project and create additional methods that subtract, multiply or divide the two

相关标签:
1条回答
  • 2021-01-13 12:42

    (1) switch is often a cleaner way to express this kind of branching. The major limitation of switch is that it only works on compile-time constant values (const variables, literal strings, ints, enums, etc.). In this case, that doesn't seem to be an issue, so switch would probably be a good choice for cleaner, slightly shorter code. In performance-sensitive code (which this is certainly not), a single switch can be faster than a bunch of ifs because the program will test the value and jump straight to the right case rather than testing against each if condition until reaching a match.

    (2) an easy way to do this would be to wrap your entire program in a loop:

    var retry = true;
    while (retry)
        retry = false;
        // your program
        else { // or default: if you're going with switch
            ...
            retry = true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题