2014-02-06 81 views
0

我在這裏打我的同胞,我的同胞。有沒有一種方法可以生成隨機數字,然後指出它們是正面還是負面?顯示隨機數和排序

我嘗試使用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(); 
+2

您通過X值循環從1到5,所有這些都是積極的。 – rikitikitik

回答

1

請嘗試以下代碼...不要比較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(); 
+0

哦,我看到非常感謝你讓我的一天。 – TheBoringGuy

+2

@TheBoringGuy將它標記爲答案,如果它已經解決了您的問題 –

+1

是這確實回答了我的問題,但有一個計時器:B – TheBoringGuy

1

您正在查看x的值。它將始終保持積極。嘗試在您的循環中:

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

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

我看到感謝:B – TheBoringGuy

+0

快樂..... :) –

0

這將做的工作:

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

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

我認爲這會做你正在尋找什麼:

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

這將打印出5個號碼,並讓他們在結果集顯示如下:

enter image description here

+0

謝謝你,先生,但我已經找到了我的答案,但我仍然要檢查了這一點。謝謝:) – TheBoringGuy

+0

哦,謝謝O_O這看起來更加組織。 – TheBoringGuy

+0

@JasonRoell這是不正確的方式來要求OP來標記答案,也OP要求最簡單的方法來做到這一點,使用集合這樣的問題並不簡單,它是性能開銷 –

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

使用continue大約是另一種方式:

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