我和tryst (刘术) 的问题基本上一致,但用了 honkyjiang(老蒋) 的方法的时候出现如下错误,是缺少哪些东西?500 Servlet Exception Note: sun.tools.javac.Main has been deprecated. /test.jsp:5: Class _jsp.DecimalFormat not found. DecimalFormat df = new DecimalFormat("##,###,###,###,###,##0.0000000000000"); ^ /test.jsp:5: Class _jsp.DecimalFormat not found. DecimalFormat df = new DecimalFormat("##,###,###,###,###,##0.0000000000000"); ^ /test.jsp:9: Variable df may not have been initialized. String str = df.format(argdouble); ^ 3 errors, 1 warning
double argdouble;
String str = df.format(argdouble);
Note: sun.tools.javac.Main has been deprecated.
/test.jsp:5: Class _jsp.DecimalFormat not found.
DecimalFormat df = new DecimalFormat("##,###,###,###,###,##0.0000000000000");
^
/test.jsp:5: Class _jsp.DecimalFormat not found.
DecimalFormat df = new DecimalFormat("##,###,###,###,###,##0.0000000000000");
^
/test.jsp:9: Variable df may not have been initialized.
String str = df.format(argdouble);
^
3 errors, 1 warning
?
我是用的他但是我想保留两位小数大家知道怎么办吗
@see BigDecimal#setScale(int,int)比如:
BigDecimal newDecimal = aDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
这样就可以保留两位小数。
用valueOf方法可以将一个普通数字转换成一个大数字(BigInteger a=BigInteger.valueOf(100); )。
但是不能用+ - * / 运算符。必须用类中的方法(BigInteger c=a.add(b);)。
////////////BigInteger
//BigInteger add(BigInteger other)
//BigInteger subtract(BigInteger other)
//BigInteger multiply(BigInteger other)
//BigInteger divide(BigInteger other)
//BigInteger mod(BigInteger other)
//BigInteger compateTo(BigInteger other)
////////////////BigDecimal
//BigDecimal add(BigDecimal other)
//BigDecimal subtract(BigDecimal other)
//BigDecimal multiply(BigDecimal other)
//BigDecimal divide(BigDecimal other)
///////////////////////////////////////////
import java.math.*;
import javax.swing.*;
public class BigIntegerTest
{
public static void main(String[] args)
{
String input=JOptionPane.showInputDialog("How many numbers do you need to draw?");
int k=Integer.parseInt(input);
input=JOptionPane.showInputDialog("How many numbers do you need to draw?");
int n=Integer.parseInt(input);
BigInteger lotteryOdds=BigInteger.valueOf(1);
for(int i=1;i<=k;i++)
lotteryOdds=lotteryOdds.multiply(BigInteger.valueOf(n-i+1)).divide(BigInteger.valueOf(i));
System.out.println("Your odds are 1 in "+lotteryOdds+" . Good luck!");
System.exit(0);
}
}
java.math包中有两个类,BigInteger和BigDecimal,可用于具有任意长度的数字,
BigInteger实现了任意长度的整数运算;
BigDecimal实现了任意精度的浮点数运算。
用valueOf方法可以将一个普通数字转换成一个大数字
(BigInteger a=BigInteger.valueOf(100); )。
但是不能用+ - * / 运算符。必须用类中的方法(BigInteger c=a.add(b);)。
////////////BigInteger
//BigInteger add(BigInteger other)
//BigInteger subtract(BigInteger other)
//BigInteger multiply(BigInteger other)
//BigInteger divide(BigInteger other)
//BigInteger mod(BigInteger other)
//BigInteger compateTo(BigInteger other)
////////////////BigDecimal
//BigDecimal add(BigDecimal other)
//BigDecimal subtract(BigDecimal other)
//BigDecimal multiply(BigDecimal other)
//BigDecimal divide(BigDecimal other)///////////////////////////////////////////示例
import java.math.*;
import javax.swing.*;
public class BigIntegerTest
{
public static void main(String[] args)
{
String input=JOptionPane.showInputDialog("How many numbers do you need to
draw?");
int k=Integer.parseInt(input);
input=JOptionPane.showInputDialog("How many numbers do you need to draw?");
int n=Integer.parseInt(input);
BigInteger lotteryOdds=BigInteger.valueOf(1);
for(int i=1;i<=k;i++)
lotteryOdds=lotteryOdds
.multiply(BigInteger.valueOf(n-i+1))
.divide(BigInteger.valueOf(i));
System.out.println("Your odds are 1 in "+lotteryOdds+" . Good luck!");
System.exit(0);
}
}