Topic: 这个求阶乘 用递归实现的 程序为什么得不到结果?

  Print this page

1.这个求阶乘 用递归实现的 程序为什么得不到结果? Copy to clipboard
Posted by: jjzz
Posted on: 2005-10-16 12:39

请尽量使用准确的文字描述作为标题

Original subject :
大家好 请大家帮我看看这个程序为什么得不到结果?


import java.io.*;
class Digui
{
      public static void main(String []args)
      {
       int c = 0;
       int y;
            System.out.println("请输入一个数字:");
            try
            {
                c = System.in.read();
            }
            catch(IOException e){}
             hanshu tt=new hanshu();
            y=tt.jisuan(c);
            System.out.println(y);
            System.out.println(new hanshu().jisuan(c));
            //System.out.println("the score is:"+y);
      }
}

class Hanshu
{ int t = 1;
     int jisuan(int i)
      {
          
          if(i==1)
          {
          t=1;
          
          }
          else
          { t = i*jisuan(i-1);
            // System.out.println(t);
            
         }return t;
        
          
      }
}

这个程序求阶乘 用递归实现的 ~ 它得出的结果总是0 还是把注释去掉后 它得到的结果根本不和想象的相同 大家帮我看看好吗?不好意思 啥都不懂 麻烦大家了

2.Re:大家好 请大家帮我看看这个程序为什么得不到结果? [Re: jjzz] Copy to clipboard
Posted by: hamlet
Posted on: 2005-10-16 15:46

你的调用Hanshu类的jisuan方法没有传递参数阿!!!

3.Re:这个求阶乘 用递归实现的 程序为什么得不到结果? [Re: jjzz] Copy to clipboard
Posted by: why
Posted on: 2005-10-16 19:33

1.
Hanshu tt=new Hanshu();
System.out.println(new Hanshu().jisuan(c));

2.
c = System.in.read();
gets the value of the character, e.g. '0' is 58, 'A' is 65, not its literal value.
and you can only get a number from 0 to 9 with your program

3. 得出的结果总是0
12! or 13! will already overflow int (32 bit integer) and I guess at most 55!, which has 12 trailing zeros (2*5, 10, 12*15; 20, 22*25, 30, 32*35, 40, 42*45, 50, 52*55, 20*50), will make the int 0.

4.Re:这个求阶乘 用递归实现的 程序为什么得不到结果? [Re: jjzz] Copy to clipboard
Posted by: rhvic
Posted on: 2005-10-17 11:39

perfect explanations!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923