简体   繁体   中英

C# How to make use of return true/false from a method (outside of loop) called from within a while loop

I am trying to write a program that lets me enter a list of names with their respective film rating into an array (Would do a list but the course material wants me to use array). Before I add the names to the array I want to make sure that a valid rating has been entered.

I am currently using a for statement that cycles through he array length and lets the user enter each movie to the list that way. This happens in a while loop to make them re-enter the name if the rating is invalid before the name is committed to Array. I check the names by calling a method with a temporary string assigned with the current entered name and rating which will do some conditional checks and return either false or true depending on the outcome. But the way I am doing it is not working at all..

Problem is that I have no idea how to make practical use of the bool statement my method returns:

string[] filmNames = new string[ArrayLength];
                    for (int i = 0; i < filmNames.Length; i = i + 1)
                    {
                        bool ratingFail = true;
                        int displayNumber = i + 1;
                        while (ratingFail)
                        {
                            Console.Write($"> Enter the Name and Rating of film number {displayNumber} of {ArrayLength}: ");
                            string checkRating = Console.ReadLine();
                            CheckRating(checkRating); // currently just does "return false;" for testing purposes
                            if (true) // this statement is clearly not effected by whatever the return value is from above method. Why? What to do?
                            {
                                ratingFail = true;
                            }
                            else
                            {   
                                filmNames[i] = checkRating; // this bit is marked as unreachable, which is is.
                                ratingFail = false;
                            }
                        }
                    }

my test method:

public static bool CheckRating(string checkRating)
        {
            return false;
        }

I am VERY (a week) new to programming and C# so please keep in mind when answering that I may not understand particular lingo referring to programming terms outside of the scope of what you can see here within, but I will Google and research to the best of my abilities if there is no way to simplify what you want to say. Thank you for your time and effort.

bool ret = CheckRating(checkRating); // currently just does "return false;" for testing purposes

if (ret)
{
    [...]
}

Or, like Sinatr's comment:

if(CheckRating(checkRating))
{
    [...]
}
if (true) // this statement is clearly not effected by whatever the return value is from above method. Why? What to do?
    {
        ratingFail = true;
    }

This will always be true, because you're creating a variable that is always true. You want this:

ratingFail = CheckRating(checkRating)
if (!ratingFail) {
    The rating is valid, do stuff here.
}

If ratingFail is true, the loop will continue.

This assumes that CheckRating returns true if the input is invalid, and false if it is valid. The variable naming here is pretty confusing, and I recommend you refactor.

I would do it this way, assuming CheckRating returns true if valid:

for (int i = 0; i < filmNames.Length; i = i + 1)
{
    bool ratingValid; // Defaults to false
    int displayNumber = i + 1;
    while (!ratingValid)
    {
        Console.Write($"> Blablabla: ");
        string input = Console.ReadLine();
        ratingValid = CheckRating(input);
    }
    // Do stuff if rating is valid here. If you got here, rating is valid.
}

// edit by iluvpancakes // I decided to add a comment made by @Sinatr since that was the (version of the) solution I personally ended up using:

    if(CheckRating(checkRating))
    {
        [do stuff and things]
    }

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