Make the program return to the beginning of a conditional if the condition is not true

Asked

Viewed 57 times

0

I can’t get the program to repeat until the condition is true, I’m a beginner, someone can help me? :(

        Console.WriteLine("Digite sua hora de entrada");
        int hours = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Digite os minutos da entrada");            
        int minutes = Convert.ToInt32(Console.ReadLine());
        if (minutes < 00 && minutes <= 60)
        {
            Console.WriteLine("Minutos inválidos... Digite um valor de 0 a 60");
            minutes = Convert.ToInt32(Console.ReadLine());
        } else
        {
            string hournmin = "Sua hora de entrada foi: " + hours + "h" + minutes + "min";
            Console.WriteLine(hournmin);
        }
  • See if the answer helps you, I edited your question to better describe the error, I hope I have correctly understood your question.

1 answer

4


Change the signs < for >= in the first condition. Then change the location of the exits and within the condition change the value 60 for 59 since minutes count from scratch. Your structure will work, see the example below:

C#:

    Console.WriteLine("Digite sua hora de entrada");
    int hours = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Digite os minutos da entrada");            
    int minutes = Convert.ToInt32(Console.ReadLine());
    if (minutes >= 00 && minutes <= 59)
    {
        string hournmin = "Sua hora de entrada foi: " + hours + "h" + minutes + "min";
        Console.WriteLine(hournmin);
    } else
    {
        Console.WriteLine("Minutos inválidos... Digite um valor de 0 a 60");
        minutes = Convert.ToInt32(Console.ReadLine());
    }

According to the above loop, if the user enters a value less than zero or greater than 59, the loop will return a message stating the error.

Additional

It is possible to improve the logic using a while, which better treats the condition presented. See example below:

C#:

    while (minutes < 00 || minutes > 59)
    {
        Console.WriteLine("Minutos inválidos... Digite um valor de 0 a 60");
        minutes = Convert.ToInt32(Console.ReadLine());
    }
    string hournmin = "Sua hora de entrada foi: " + hours + "h" + minutes + "min";
    Console.WriteLine(hournmin);

By replacing the if for while and change the link conditions, your program treats an invalid entry more intelligently, forwarding to the minute input until a valid entry is entered.

  • Thank you very much, your reply was really helpful! Let me ask: Is there any way to make the loop repeat if "minutes" is not less than or equal to 00 and also not less than or equal to 59? The idea was that if the user put a value

  • 1

    Yes, You can create a While, send me msg in Linkedin that I show you. you can find my Inkedin in the profile.

  • 1

    If it helped you, don’t forget to let it mark as a response below the <3 poll buttons

  • I’m not getting in touch with you via Linkedin, can you show me how I do around here? That would help so much. :(

  • 1

    added in the reply, see.

  • Thank you very much, friend! I spent more time breaking my head, I couldn’t see the logic of how to do it. Thank you very much!!!!!

  • We continue chat.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.