至于小数的算法程序就不用回了!请回复的高手注意以下几个问题:1.如果待求数(如1000000) 本身已经超过int或long或double所存储的范围,用什么接收待求数?
2.如果用控制台输出,由于位数极多,像cmd窗口即使计算正确,也不能够完整显示出所有数字.
(前面的数字被后面的覆盖)
3.请注意效率问题
2.如果用控制台输出,由于位数极多,像cmd窗口即使计算正确,也不能够完整显示出所有数字.
(前面的数字被后面的覆盖)
3.请注意效率问题
打印简单,输出到文件不就可以了~~在LINUX/UNIX下直接 在命令行后面"> out.txt"
java用BigInteger,不过就是慢,没办法。3.请注意效率问题
....没有解决方法。可能用64位机器会快些。或者自己参考BigInteger的方法改写,如不用每次new BigInteger()构建新对象.BigInteger的方法其实就是;将字节的移位算法换成用字符窜不过我想知道这程序用来干嘛?
2.eureka0891(迷茫中...)兄和eastred(东方红) 兄请你们看看我写的程序,改一下,再回复
我自己写了一个程序,请改一下,让它能计算输入数大于int接受范围的数
import javax.swing.*;
import java.math.BigInteger;public class BigNumFrame extends JFrame {
private JTextArea output;
public BigNumFrame(){
super("阶乘计算器");
output=new JTextArea();
output.setLineWrap(true);
output.setWrapStyleWord(true);
JScrollPane out=new JScrollPane(output); getContentPane().add(out);
setSize(400,300);
setVisible(true);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
String input=JOptionPane.showInputDialog("请输入您要求阶乘的数:"); int n=Integer.parseInt(input);
BigInteger result=BigInteger.ONE;
for(int i=1;i<=n;i++)
result=result.multiply(new BigInteger(""+i));
BigNumFrame app=new BigNumFrame(); app.output.setText(n+"!="+result);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception e){
System.exit(0);
} }}
import javax.swing.*;
import java.math.BigInteger;public class JieCheng extends JFrame {
/**
* @author Min Shakes
*/
private JTextArea output;
private BigInteger cal(String input){ //计算方法cal
BigInteger result=BigInteger.ONE;
BigInteger max=new BigInteger(input);
for(BigInteger i=BigInteger.ONE;max.compareTo(i)!=-1;i=i.add(BigInteger.ONE))
result=result.multiply(i);
return result;
}
public JieCheng(){ //constructor
super("阶乘计算器");
output=new JTextArea();
output.setLineWrap(true); //设置JTextArea自动换行
output.setWrapStyleWord(true);
JScrollPane out=new JScrollPane(output);
getContentPane().add(out);
setSize(400,300);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
String input=JOptionPane.showInputDialog("请输入您要求阶乘的数:");
JieCheng app=new JieCheng();
app.output.setText(input+"!="+app.cal(input));
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception e){
System.exit(0);
}
}
}