2015-04-07 61 views
0

我有一个函数,其中我把数字作为字符串,当我将数字转换为int时,它们与其他值一起保存。 这是为什么请帮助。从字符串转换为INT保存另一个值

private string DoTheMath() 
{ 
    string s = Console.ReadLine(); 
    string[] s1 = s.Split(' '); 

    int n1 = Convert.ToInt32(s1[0]); 
    int k1 = Convert.ToInt32(s1[1]); 
} 

当我输入49 51 INT N1得到价值31 和INT K1获得价值33

+0

尝试使用小数点代替 – bit

+0

如何检查数字? – Euphoric

+1

断点,因为我添加它们。 –

回答

1

由于您解析字符串整数,你可能想Int32.Parse:

private string DoTheMath() 
{ 
    string s = Console.ReadLine(); 
    string[] s1 = s.Split(' '); 

    int n1 = Int32.Parse(s1[0]); 
    int k1 = Int32.Parse(s1[1]); 
} 
+0

'Convert.ToInt32'调用'Int32.Parse'。通过调用'Int32.Parse',他不会得到任何不同的结果。 –