2011-01-06 95 views
0

我發了一個號碼,要求用C#輸入2個數字,並以相應的消息作爲響應。第二個數字如何起作用? 無論我爲第二個數字輸入什麼,我都會收到消息「您的號碼在0-10範圍內」。C#控制流程

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string myInput; // declaring the type of the variables 
      int myInt; 

      string number1; 
      int number; 


      Console.WriteLine("enter a number"); 
      myInput = Console.ReadLine(); //muyInput is a string which is entry input 
      myInt = Int32.Parse(myInput); // myInt converts the string into an Integer 

      if (myInt > 0) 
       Console.WriteLine("Your number {0} is greater than zero.", myInt); 
      else if (myInt < 0) 
       Console.WriteLine("Your number {0} is less than zero.", myInt); 
      else 
       Console.WriteLine("Your number {0} is equal zero.", myInt); 

      Console.WriteLine("enter another number"); 
      number1 = Console.ReadLine(); 
      number = Int32.Parse(myInput); 

      if (number < 0 || number == 0) 
       Console.WriteLine("Your number {0} is less than zero or equal zero.", number); 
      else if (number > 0 && number <= 10) 
       Console.WriteLine("Your number {0} is in the range from 0 to 10.", number); 
      else 
       Console.WriteLine("Your number {0} is greater than 10.", number); 

      Console.WriteLine("enter another number"); 

     } 
    }  
} 

回答

7

number = Int32.Parse(myInput);應該讀number = Int32.Parse(number1);

2
number = Int32.Parse(myInput); 

第二數目應該

number = Int32.Parse(number1); 
2

因爲你解析相同的字符串兩次

number1 = Console.ReadLine(); 
number = Int32.Parse(myInput); 

應該是

number1 = Console.ReadLine(); 
number = Int32.Parse(number1); 
+0

確實有人說「weltgeist」? ;) – 2011-01-06 21:41:36

0
number1 = Console.ReadLine(); 
number = Int32.Parse(myInput); 

在你的第二個測試你的評價第一輸入,它應該是

Int32.Parse(number1); 
1

你在爲第一個數字輸入?在「輸入另一個號碼」後,您將number1分配給控制檯的值,但您從不使用該值,但實際上使用的值是myInput