java 阶乘,循环使用

来源:百度知道 编辑:UC知道 时间:2024/05/15 19:37:02
import java.io.IOException;

class Text {

/**
* @param args
*/
public static void main(String[] args)
throws IOException{

int i;
int sum=1;
int n;
System.out.println("please input(0--7):");
n=System.in.read()-48;
for(i=1;i<=n;i++)
{sum=sum*i;}
System.out.println(n+"!="+sum);

}

}
这是我写的程序,但是我返现有个缺点,就是,每运行一个数字的阶乘,就要重新运行才能计算其他的数的阶乘,我希望各位帮我改一下,让这个程序能在计算过一个数的阶乘之后,不用从新运行就能再次计算其他数的阶乘。

import java.io.IOException;

class Text {

/**
* @param args
*/
public static void main(String[] args)
throws IOException{

int i;
int sum=1;
int n;
while(ture){
System.out.println("please input(0--7):");
n=System.in.read()-48;
for(i=1;i<=n;i++)
{sum=sum*i;}
System.out.println(n+"!="+sum);
} //要想结束按Ctrl+c,没测试,自己试下,呵呵
}

}

这个是我做的例子,在java1.5上运行通过。你可以参考一下,计算结成最好用递归算法!
package com.icss.test;

import java.io.IOException;
import java.util.Scanner;
/**
* @author 神的右手
*
* */
public class Test {

/**
* @param args
*/
public static void main(String[] args)
throws IOException{
Scanner scan = new Scanner(System.in);
int n;
boolean t=true;
while(t){
System.out.print("请输入你要计算的数字:"