Jump to content

loop issue noob question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

I am practicing my looping, specifically doing while loop, here is it below, but for some reason, what it is suppose to do if you enter a number exactly 20 or above, it asks please enter another number less and you enter it in, then it does a cound up to 20, so far got the number to go if you press something less than 20 but anything 20 or above, it just kicks out: can you please correct the program and explain what i did wrong for future reference would be appreciated.


namespace ConsoleApplication1

{

        class Program

        {

            /*

            static string write()

            {

                string mystring;

                Console.WriteLine("Please enter first name");

                mystring = Console.ReadLine();

                return mystring;

                

            }

            */

            

            static void Main(string[] args)

            {

                int num1 = 20;

                int num2;

                Console.WriteLine("Please enter a number less than 20: ");

                

                num2 = int.Parse(Console.ReadLine());

                if (num2 >= num1)

                    Console.WriteLine("Please enter another number");

                while (num2 < num1)

                    {

                        num2++;

                        Console.WriteLine(num2);

                    }

                Console.ReadKey();




#2
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Finally got it, for any reference here is the code below, what i was doing wrong from what i can think is that i should not just say in the if statement, enter another number not equal to or more than 20, but also have the user type in the number again geting the varaible then it will go into the loop. I hope i explained it right if not please correct me. here is the code below:


class Program

        {

            /*

            static string write()

            {

                string mystring;

                Console.WriteLine("Please enter first name");

                mystring = Console.ReadLine();

                return mystring;

                

            }

            */

            

            static void Main(string[] args)

            {

                int num1 = 20;

                int num2;

                Console.WriteLine("Please enter a number less than 20: ");

                

                num2 = int.Parse(Console.ReadLine());

                if (num2 >= num1)

                {

                    Console.WriteLine("Please enter another number");

                    num2 = int.Parse(Console.ReadLine());

                }

                while (num2 < num1)

                    {

                        num2++;

                        Console.WriteLine(num2);

                    }

                

                Console.ReadKey();



#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Sorry no that didnt solve it, sorta did but what happens it if you press something 20 or greater after 2 times, then it still kicks you out of the program, i want it where it will not kick you out of the program until you enter a small number... again if you can correct the code and explain that would be very helpful. thanks in advance.

#4
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
if you change this if statement:

if (num2 >= num1)

                {

                    Console.WriteLine("Please enter another number");

                    num2 = int.Parse(Console.ReadLine());

                }

to a while statement:

while (num2 >= num1)

                {

                    Console.WriteLine("Please enter another number");

                    num2 = int.Parse(Console.ReadLine());

                }

Then it will loop back through and ask for the number again if it is still to large. The problem is that the if loop is only evaluating the one time and then the code is done operating where a while statement will wait until an appropriate answer is recieved. I hope this helps you out:)

#5
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
this helps a lot thank you gaylo565 for the assist.