2017-01-17 55 views
1

,当我在这个节目输出的最佳它炸毁整数数量应该是多少有些道理的。很难理解这个实数是什么!价值观发生翘曲

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

namespace Safr_Manager 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 



      var sortarry = new[] {'4', '6', '4', '7', '8', '3'}; 

      int best = -1; 




      for(int i = 0; i < 6; i++) 
      { 
       var cursel = sortarry[i]; 
       if (cursel > best) 
       { 

        best = cursel; 

       } 



       Console.WriteLine(cursel); 
       Console.WriteLine(best); 



      } 



     } 

    } 
} 

这就是它输出的内容。你可以看到它如何改变当前最好的部分应该是什么!

Current selected: 4 
Current best: 52 
Current selected: 6 
Current best: 54 
Current selected: 4 
Current best: 54 
Current selected: 7 
Current best: 55 
Current selected: 8 
Current best: 56 
Current selected: 3 
Current best: 56 
+1

52表示炭的值 '4',从阵列 –

+1

除去单引号或'Console.WriteLine(Char.GetNumericValue(最好)的ToString());' – Slai

回答

9

在数字周围使用引号,例如: '4'告诉c#这是一个字符。如果将其隐式转换为int,则它使用该char的ASCII代码(例如52)。为了使用整数,请像这样设置阵列:

var sortarry = new[] {4, 6, 4, 7, 8, 3}; 
+0

谢谢您!它现在有效! –

1

您正在比较int和char。如果你想比较价值,这不是一个好主意。 因此,将您的数组更改为int。

using System; 

public class Program 
{ 
    public static void Main() 
    { 
     var sortarry = new[] {4, 6, 4, 7, 8, 3}; 

      int best = -1; 
      for(int i = 0; i < 6; i++) 
      { 
       var cursel = sortarry[i]; 
       if (cursel > best) 
       { 

        best = cursel; 

       } 
       Console.WriteLine(cursel); 
       Console.WriteLine(best); 
      } 

    } 
} 

代码例如:https://dotnetfiddle.net/hjlYjU

+0

你也不需要指定6进行循环。只需使用'sortarry.Length' – xszaboj