0

I wanted to try how the if conditional works so I created this code almost by myself. I also had problems with random into int.

Here's my code:

using System;

namespace Bigger_Smaller_Equal
{
    class Program
    {
        static void Main(string[] args)
        {
            int min = 1;
            int max = 100;

            Random rnd = new Random();
            int gen = rnd.Next(min, max);
            Console.WriteLine("My Number is : " + gen + "!");
            Console.WriteLine("Tell me your number:");
            string typ = Console.ReadLine();
            int num = int.Parse(typ);
            if (num == gen)
            {
                Console.WriteLine(num + " is Equal to " + gen);

            }
            else if (num > gen)
            {
                Console.WriteLine(num + " Is Bigger than " + gen);
            }
            else if (num < gen)
            {
                Console.WriteLine(num + " Is Smaller than " + gen);
            }
            Console.WriteLine("Press Any Key to exit.");
            Console.ReadLine();
        }
    }
}

How to make the console stop, so it will allow me to enter another number?

Basically:

  1. I write a number it tells me if its smaller bigger or equal to number which was randomly generated
  2. After I press enter instead of closing the console the number will be generated again and I can write new number and so on.
Blue
  • 22,608
  • 7
  • 62
  • 92
Kenedian
  • 9
  • 5
  • 2
    What you need is a [loop](https://msdn.microsoft.com/en-us/library/f0e10e56(v=vs.90).aspx). Also check out [this](http://stackoverflow.com/questions/1754282/how-to-loop-a-console-app) question. – bkaf Oct 06 '16 at 12:16
  • bad practice but: `while(true){/*yourcode*/}` – fubo Oct 06 '16 at 12:16
  • @fubo Why is it bad practice? – Blue Oct 06 '16 at 12:58
  • @FrankerZ a loop should have a exit condition - this one is a inifity loop – fubo Oct 06 '16 at 13:00
  • @FrankerZ It's not bad practice. There's times when having the condition to break out of the loop in other places than strictly at the top or bottom is useful. – Chris Oct 06 '16 at 13:42

3 Answers3

1

Here's an example using goto, although it is not recommended for more complex applications as you could end up creating endless loops. Feel free to try it out

    static void Main(string[] args)
    {
        int min = 1;
        int max = 100;

        Random rnd = new Random();

    again:
        int gen = rnd.Next(min, max);
        Console.WriteLine("My Number is : " + gen + "!");
        Console.WriteLine("Tell me your number:");
        string typ = Console.ReadLine();
        int num = int.Parse(typ);
        if (num == gen)
        {
            Console.WriteLine(num + " is Equal to " + gen);

        }
        else if (num > gen)
        {
            Console.WriteLine(num + " Is Bigger than " + gen);
        }
        else if (num < gen)
        {
            Console.WriteLine(num + " Is Smaller than " + gen);
        }
     repeat:
        Console.WriteLine("Play again? (Y/N)");
        string ans = Console.ReadLine();
        switch (ans.ToUpper())
        {
            case "Y": goto again; break;
            case "N":  break; //continue
            default: goto repeat; break;
        }
    }
Innat3
  • 3,561
  • 2
  • 11
  • 29
0

You can use console.ReadKey() insted of using console.ReadLine(). console.ReadLine() wait for input set of character thats why you console window is apperre there after pressing any key.

  • This does not answer the question. The OP wanted to know how to cause the code to repeat. In other words, how to loop the code. – Chris Dunaway Oct 06 '16 at 14:36
0

You can use "do while" or "while" operator.If you dont want to use while(true) , you can use this diffirent way. I mean that when user enter 0 or -1 this system can stop. while()

bool repeat = true;
    do 
{
    Console.WriteLine("Enter value  ");
    string typ = Console.ReadLine();
    int num = int.Parse(typ);
    if (num!=0) 
       // bla bla bla.
    else
        repeat = false; 
}while (repeat);
Erdem Köşk
  • 190
  • 1
  • 10
  • Why would you introduce a boolean variable? Simply do `while (true) { /*code*/ }` and use `break;` to exit the loop? – Blue Oct 06 '16 at 12:57
  • Both of answer are true but I think that using while(true) is not right way for every solution. In another method can use this bool or another thread can check this but using while(true) you cannot access to information about this . – Erdem Köşk Oct 06 '16 at 13:31