TheBoringGuy
TheBoringGuy

Reputation: 151

Display random number and sorting

I am hitting a wall here, my fellow people. Is there a way where I can generate random numbers, and then indicate them if they are positive or negative?

I tried using an if statement, but as you know once it hits the if it doesn't go to the else.

Please keep your code simple in such a way that any person would understand what's going on or at least indicate what's going on in your code.

How can I fix this mistake?

Random rnd = new Random();

Console.WriteLine("\n5 random integers from -100 to 100:");
for (int X = 1; X <= 5; X++)
{
    if (X >= 0)
    {
        Console.WriteLine("These are the positive numbers: {0}", rnd.Next(0, 100));
    }
    else
    {
        Console.WriteLine("These are the negative numbers: {0}", rnd.Next(-100, 0));
    }
}
Console.ReadLine();

Upvotes: 0

Views: 165

Answers (6)

A.T.
A.T.

Reputation: 26302

Using continue is another way around:

Random rnd = new Random();
Console.WriteLine("\n5 random integers from -100 to 100:");
for (int X = 1; X <= 5; X++)
{
    int y = rnd.Next(-100, 100);
    if (y < 0)
    {
        Console.WriteLine("These are the negative  numbers: {0}", y);
        continue;
    }
    Console.WriteLine("These are the positive numbers: {0}", y);
}
Console.ReadLine();

Upvotes: 0

user3277522
user3277522

Reputation: 96

Random rnd = new Random();

Console.WriteLine("\n5 random integers from -100 to 100:");
for (int X = 1; X <= 5; X++)
{
    int Y = rnd.Next(-100, 100);

    if (Y > 0)
    {
         Console.WriteLine("These are the positive numbers: {0}", Y);
    }
    else
    {
        Console.WriteLine("These are the negative numbers: {0}", Y);
    }
}

Upvotes: 0

Ofir
Ofir

Reputation: 5279

This will do the work:

Random rnd = new Random();
int randomNumber = rnd.Next(-100, 100);

if (randomNumber >= 0)
    Console.WriteLine("Positive (with Zero)");
else 
    Console.WriteLine("Negative");

Upvotes: 0

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

You are checking the value of x. It will always remain positive. Try this in your loop:

int i = rnd.Next(-100, 100);

if (i >= 0)
    //It is positive
else
    //It is negative

Upvotes: 1

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

Try the following code... Don't compare X; you have to compare the random number.

Random rnd = new Random();

Console.WriteLine("\n5 random integers from -100 to 100:");
for (int X = 1; X <= 5; X++)
{
    int y = rnd.Next(-100, 100);
    if (y >= 0)
    {
        Console.WriteLine("These are the positive numbers: {0}", y.ToString());
    }
    else
    {
        Console.WriteLine("These are the negative numbers: {0}", y.ToString());
    }
}
Console.ReadLine();

Upvotes: 1

Jason Roell
Jason Roell

Reputation: 6809

I think this will do exactly what you are looking for:

public static void Main(string[] args)
{
        var rnd = new Random();
        Console.WriteLine("\n5 random integers from -100 to 100:");
        List<int> random = new List<int>();

        for (int X = 1; X <= 5; X++)
        {
            random.Add(rnd.Next(-100, 100));
        }

        Console.WriteLine("These are the positive numbers:");
        foreach (int number in random)
        {
            if (number >= 0)
            {
                Console.WriteLine(number);
            }
        }

        Console.WriteLine("These are the negative numbers:");
        foreach (int number in random)
        {
            if (number < 0)
            {
                Console.WriteLine(number);
            }
        }

        Console.ReadLine();
    }

This will print out 5 numbers and have them in a result set displayed like below:

enter image description here

Upvotes: 0

Related Questions