首页 > 解决方案 > 变量变化荒谬,如 3 到 51?

问题描述

我一直在尝试解决代码战问题,但是当我使用 Math.Pow 时,我得到了有趣的结果。

就像我输入 3 作为基础和 3 作为功率但结果

我知道我应该使用 double 但 int 在其他项目中可以正常工作:D

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


namespace SoloLearn
{
class Program
{
    static void Main(string[] args)
    {
    int value = 371;
    int cL;
    List<int> NumberList = new List<int>();
    
    string Sayı = value.ToString();             //to make value to string so I could 
    int L = Sayı.Length;                        // count length and putto array
    char[] charArray = Sayı.ToCharArray();

    

    foreach(char chr in charArray)  // getting digits multiply with himself for L times
    {                             
        cL = chr;                 //    chr İS 3 BUT WHEN ASSİGNED TO cL İT CHANGES TO 51
        Console.WriteLine(chr)    // gives 3
        Console.WriteLine(cL)     // gives 51
        double nmbr = Math.Pow(cL, L);       // calculating power etc
        int charr = (int) nmbr;              // assign result to int
        NumberList.Add(charr);               // take assigned int to list
    }

标签: c#variablesintegerdouble

解决方案


字符有代码,您的字符 3 的值为 51。有关所有字符的列表,请参见此处:

https://www.w3schools.com/charsets/ref_html_ascii.asp


推荐阅读