寮鍙戠幆澧冿細(xì)Windows Server 2003 Standard Edition SP1, J2SDK 1.5.0_06, Eclipse 3.1.2
婧愪唬鐮佸涓嬶細(xì)
//Factorial.java
import java.math.BigInteger;
import javax.swing.*;
/**
* 璁$畻浠繪剰姝f暣鏁扮殑闃朵箻
*
*
*/
public class Factorial {
public static void main(String[] args) {
BigInteger x = BigInteger.valueOf(1); //瀛樺偍緇撴灉
int num = 1; //寰呰綆楃殑鏁存暟
String s = null;
boolean correct = false;
do {
try {
s = JOptionPane.showInputDialog(null, "璇瘋緭鍏ヨ璁$畻鐨勬暟(姝f暣鏁?錛?);
if (s == null)
break;
else {
num = Integer.parseInt(s);
if (num < 0)
throw new IllegalArgumentException();
else correct = true;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "鏁版嵁鏍煎紡閿欒錛?);
continue;
} catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog(null,"璇瘋緭鍏ヤ竴涓鏁存暟錛?);
continue;
}
break;
} while (true);
if (correct == true) {
for (int i = 1; i <= num; i++)
x = x.multiply(BigInteger.valueOf(i));
JTextArea textArea = new JTextArea(x.toString(), 5, 30);
textArea.setEditable(false);
textArea.setLineWrap(true);
Object[] object = {num + " ! : ",new JScrollPane(textArea)};
JDialog dialog = new JOptionPane(object).createDialog(null,"闃朵箻鐨勭粨鏋?);
dialog.setVisible(true);
}
System.exit(0);
}
}
榪愯緇撴灉濡備笅錛?/p>