比数据类型长多了 [英] what a great than datatype long

查看:52
本文介绍了比数据类型长多了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static void Main(string[] args)
{
  long    virus ;
           
  virus = Convert.ToInt32(Console.ReadLine ());
  string c="";
  for (int x = 0; x<=120; x++)
  {
    //   Console.WriteLine(b.ToString ()); 
    virus = virus * 2;
    //c = b.ToString();
  }
  Console.WriteLine((virus).ToString ());
  Console.Read();
}




为什么给我零?聚集时间不长.我认为您将值乘以2 ^ 120.它将长期超出范围.您还可以将字符串用于整数乘法.

static void Main(string[] args)
{
  double    virus ;
           
  virus = Convert.double(Console.ReadLine ());
  string c="";
  for (int x = 0; x<=120; x++)
  {
    //   Console.WriteLine(b.ToString ()); 
    virus = virus * 2;
    //c = b.ToString();
  }
  Console.WriteLine((virus).ToString ());
  Console.Read();
}



对于字符串乘法,您可以看到此链接

点击


这是因为long 是64位带符号的Integer
long 数据类型的范围是 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 ,不能再计算更多

我的建议是使用double代替,它将为您提供指数格式的值,并且不会溢出.

如果有帮助,请 投票 接受答案 .


静态无效Main(string [] args)
{
双重病毒;
病毒= Convert.ToInt32(Console.ReadLine());
for(int x = 0; x< = 120; x ++)
{
//Console.WriteLine(b.ToString());
病毒=病毒* 2;
//c = b.ToString();
}
Console.WriteLine(病毒);
Console.Read();
}

它以指数形式给出解决方案.


static void Main(string[] args)
{
  long    virus ;
           
  virus = Convert.ToInt32(Console.ReadLine ());
  string c="";
  for (int x = 0; x<=120; x++)
  {
    //   Console.WriteLine(b.ToString ()); 
    virus = virus * 2;
    //c = b.ToString();
  }
  Console.WriteLine((virus).ToString ());
  Console.Read();
}




why give me zero?

解决方案

you can use double, there is no other data type that is integer & gather than long. I think you multiply the value by 2^120. It will be out of range in long. You also can use string for integer multiplication.

static void Main(string[] args)
{
  double    virus ;
           
  virus = Convert.double(Console.ReadLine ());
  string c="";
  for (int x = 0; x<=120; x++)
  {
    //   Console.WriteLine(b.ToString ()); 
    virus = virus * 2;
    //c = b.ToString();
  }
  Console.WriteLine((virus).ToString ());
  Console.Read();
}



for string multiplication, you can see this link

Click


That is because long is 64-bit signed Integer
and range of long datatype is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, It can''t count more then this

My advise would be to use double instead will give you value in Exponential format and will not be overflowed.

Please vote and Accept Answer if it Helped.


static void Main(string[] args)
{
double virus;
virus = Convert.ToInt32(Console.ReadLine());
for (int x = 0; x <= 120; x++)
{
// Console.WriteLine(b.ToString ());
virus = virus * 2;
//c = b.ToString();
}
Console.WriteLine(virus);
Console.Read();
}

It is giving solution in exponential form.


这篇关于比数据类型长多了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆