none
Text repeat until correct answers is input RRS feed

  • Question

  • I am new to C# and I want to create loop.

    Example: 

    Choose fruit (only apple or banana)

    if player type in apple or banana will continue to next step creating either apple juice of banana juice

     but if player type any word or key(spacebar, enter,etc) other than "apple" or "banana"

    The text "wrong answer"will continue to appear until "apple" or "banana"is type in.

    my current code is this 

         Console.WriteLine("Pick apple or banana");

            string answer = Console.ReadLine().ToLower();
            bool fruit = false;
            do
            {

                if (answer == "apple")
                {
                    fruit = true;
                    
                   Console.WriteLine("make apple juice."); break;

                }
                if (answer == "banana")
                {
                    fruit= true;
                    Console.WriteLine(:make banana juice);break;
                    
                }
                else
                {
                    Console.WriteLine("input not correct");
                }
            } while (fruit == false);



    Thursday, October 24, 2019 1:15 PM

Answers

  • Your while loop is incorrect. Equality has lower precedence than addition so it is evaluated last. Hence the expression is actually

    while (juice == ("apple" + "banana"))
    while (juice == "applebanana")

    The compiler will combine the 2 literals into a single literal resulting in the last line. Hence your code is comparing the variable `juice` with the concated value which isn't correct. Even if it did work then your subsequent if checks would not match and it would display the wrong answer.

    In your specific example your while loop isn't really doing anything other than making sure that the code repeats. Suppose you wrote your loop like this

    while (juice == "apple" || juice == "banana")

    Now you are checking to see if `juice` is EITHER apple or banana. If it is anything else you don't go into the loop. However your if logic (inside the loop) is already handling this so you don't really need any checks in your while loop related to the choices. That is what your if logic is for. 

    while (true)

    Yes this is an infinite loop but provided you "exit" the loop inside the loop you're fine. Since you're using a `break` statement in the cases where you have found what you're looking for this loop will properly iterate until a valid value is entered.

    Later you'll probably want to replace this if logic with a `switch` statement as it is designed to solve this issue more cleanly. Additionally you clearly what to loop at least once so you should probably be using the `do-while` instead of the `while`. A `do-while` is a post test so it executes the statements at least once. A `while` loop is a pre-test and executes the statements zero or more times.

    Finally, in general users don't care about spacing or case in strings. You are handling case by calling `ToLower` but you should also trim the string of any leading/trailing spaces using the Trim method.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by toastycreamy Thursday, October 24, 2019 3:31 PM
    Thursday, October 24, 2019 1:43 PM
    Moderator

All replies

  • Your while loop is incorrect. Equality has lower precedence than addition so it is evaluated last. Hence the expression is actually

    while (juice == ("apple" + "banana"))
    while (juice == "applebanana")

    The compiler will combine the 2 literals into a single literal resulting in the last line. Hence your code is comparing the variable `juice` with the concated value which isn't correct. Even if it did work then your subsequent if checks would not match and it would display the wrong answer.

    In your specific example your while loop isn't really doing anything other than making sure that the code repeats. Suppose you wrote your loop like this

    while (juice == "apple" || juice == "banana")

    Now you are checking to see if `juice` is EITHER apple or banana. If it is anything else you don't go into the loop. However your if logic (inside the loop) is already handling this so you don't really need any checks in your while loop related to the choices. That is what your if logic is for. 

    while (true)

    Yes this is an infinite loop but provided you "exit" the loop inside the loop you're fine. Since you're using a `break` statement in the cases where you have found what you're looking for this loop will properly iterate until a valid value is entered.

    Later you'll probably want to replace this if logic with a `switch` statement as it is designed to solve this issue more cleanly. Additionally you clearly what to loop at least once so you should probably be using the `do-while` instead of the `while`. A `do-while` is a post test so it executes the statements at least once. A `while` loop is a pre-test and executes the statements zero or more times.

    Finally, in general users don't care about spacing or case in strings. You are handling case by calling `ToLower` but you should also trim the string of any leading/trailing spaces using the Trim method.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by toastycreamy Thursday, October 24, 2019 3:31 PM
    Thursday, October 24, 2019 1:43 PM
    Moderator
  • In general, I find statements that compare boolean values to "true" or "false" to be bad form.  It's like an unnecessary extra cast; the comparison operators are used to convert something to Boolean, but a bool is already in that form.  It seems clearer to me to write:

        while( !fruit );

    Partly, this is a result of ancient history when people used to write:

        #define TRUE 1
        #define FALSE 0
        ...
        if( value == TRUE )

    Many bugs were caused by code like that where people really wanted any non-zero value to be true.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, October 25, 2019 7:08 AM