Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示随机数和排序_C#_Sorting_If Statement_Random - Fatal编程技术网

C# 显示随机数和排序

C# 显示随机数和排序,c#,sorting,if-statement,random,C#,Sorting,If Statement,Random,我在这里碰壁了,我的同胞们。有没有办法生成随机数,然后指出它们是正的还是负的 我尝试使用if语句,但正如您所知,一旦它点击if它就不会转到else 请让您的代码保持简单,让任何人都能理解代码中发生了什么,或者至少指出代码中发生了什么 我怎样才能纠正这个错误 Random rnd = new Random(); Console.WriteLine("\n5 random integers from -100 to 100:"); for (int X = 1; X <= 5; X++) {

我在这里碰壁了,我的同胞们。有没有办法生成随机数,然后指出它们是正的还是负的

我尝试使用
if
语句,但正如您所知,一旦它点击
if
它就不会转到
else

请让您的代码保持简单,让任何人都能理解代码中发生了什么,或者至少指出代码中发生了什么

我怎样才能纠正这个错误

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();
Random rnd=new Random();
Console.WriteLine(“\n5个从-100到100的随机整数:”);
对于(int X=1;X=0)
{
WriteLine(“这些是正数:{0}”,rnd.Next(01100));
}
其他的
{
WriteLine(“这些是负数:{0}”,rnd.Next(-100,0));
}
}
Console.ReadLine();

您正在检查
x
的值。它将永远是积极的。在循环中尝试以下操作:

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

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

请尝试以下代码。。。不要比较X;你必须比较随机数

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();
Random rnd=new Random();
Console.WriteLine(“\n5个从-100到100的随机整数:”);
对于(int X=1;X=0)
{
WriteLine(“这些是正数:{0}”,y.ToString());
}
其他的
{
WriteLine(“这些是负数:{0}”,y.ToString());
}
}
Console.ReadLine();
这将完成以下工作:

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

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

我想这正是你想要的:

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();
    }
publicstaticvoidmain(字符串[]args)
{
var rnd=新随机数();
Console.WriteLine(“\n5个从-100到100的随机整数:”);
List random=新列表();
对于(int X=1;X=0)
{
控制台写入线(编号);
}
}
WriteLine(“这些是负数:”);
foreach(随机整数)
{
如果(数字<0)
{
控制台写入线(编号);
}
}
Console.ReadLine();
}
这将打印出5个数字,并将其显示在结果集中,如下所示:

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);
    }
}

Random rnd=new Random();
Console.WriteLine(“\n5个从-100到100的随机整数:”);
对于(int X=1;X 0)
{
WriteLine(“这些是正数:{0}”,Y);
}
其他的
{
WriteLine(“这些是负数:{0}”,Y);
}
}

使用
继续
是另一种解决方法:

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();
Random rnd=new Random();
Console.WriteLine(“\n5个从-100到100的随机整数:”);

对于(int X=1;X),您正在循环从1到5的X值。所有这些值都是正的。@如果解决了您的问题,BoringGuy会将其标记为答案。这确实回答了我的问题,但有一个计时器:B@TheBoringGuy这将打印出“这些是正数”和“这些都是负数”多次。你为什么想要冗余?只需显示所有的正数,然后显示所有的负数,就像我提供的答案一样。@JasonRoell我知道,但它更密集,所以我更容易记住我在C上瞎搞了一个月,我还没有准备好大局我想慢慢放松m但是我还是很感激。谢谢你,先生,但是我已经找到了我的答案,但是我还是要去看看。谢谢:)@JasonRoell这不是询问OP-to-mark答案的正确方法,也是询问OP-to-mark答案的最简单方法,使用集合解决此类问题并不简单,这是性能开销