如何获得输出值 [英] How Can I Get The Output Value
本文介绍了如何获得输出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
class pd
{
public static void Main(String [] args)
{
long sum=1;
long i=1;
while(i<=1000)
{
sum=sum*2;
i++;
}
Console.WriteLine(sum);
pd1 ob=new pd1(sum);
}
}
class pd1
{
public pd1(long n)
{
long s=0,k;
while(n!=0)
{
k=n%10;
s+=k;
n=n/10;
}
Console.WriteLine(s);
}
}
推荐答案
嗯。
那不是你的意思应该用于类。
所有这一切都是将代码从Main方法转移到类构造函数中并希望 - 它不是有效的OOP设计。
并且这忽略了它甚至不会起作用,因为long只有64位,并且不能容纳2 1000 - 最好sum
将最终为零;在最坏的情况下,你会得到一个溢出的例外。
我建议你不要试图获得输出值,而是回到你的作业问题并重新读它:你几乎肯定会走错路。
Um.
That isn't what you are supposed to do with classes.
All that is doing is moving the code from the Main method into a class constructor and hoping - it isn't a valid OOPs design.
And that's ignoring that it won't even work, as a long has only 64 bits, and can't hold 21000 - at bestsum
will end up as zero; at worst you will get an overflow exception.
I'd suggest that instead of trying to "get the output value" you go back to your homework question and re-read it: you are almost certainly going down the wrong path.
这篇关于如何获得输出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文