十进制格式问题Java

时间:2013-12-22 05:26:46

标签: java

我写了一个程序,我无法弄清楚我应该怎么做小数格式?我以为我做得对,显然我不能正确地做十进制格式。可以,有人用十进制格式帮助我吗?

这是我的代码:

import java.text.DecimalFormat;
import java.util.*;
public class Mean {
    public static void main(String [] args){
        Scanner s = new Scanner(System.in);
            double[]x = new double[10];
            int i;
            for(i = 0;i < 10; i++){
                x[i] = s.nextDouble();
            }
            double mean = mean(x, i);
            double deviation = var(x);
            System.out.println("The mean is " + mean);
            System.out.println("The standard deviation is " + deviation);
    }
public static double sum(double[] a) {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }
    public static double mean(double[]x, double i){
        if (x.length == 0) return Double.NaN;
        double sum = sum(x);
        return sum / x.length;
    }
    public static double var(double[] x) {
        if (x.length == 0) return Double.NaN;
        double avg = mean(x, 10);
        double sum = 0.0;
        for (int i = 0; i < x.length; i++) {
            sum += (x[i] - avg) * (x[i] - avg);
        }
        DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
        return sum / myFormatter.format(Math.sqrt(x.length - 1));

    }
}

3 个答案:

答案 0 :(得分:1)

尝试使用此格式来设置双重格式...我还必须更新方法var -

public static String formatDouble(double in) {
  DecimalFormat myFormatter = new DecimalFormat(
      "#,##0.00");
  return myFormatter.format(in);
}

public static double var(double[] x) {
  if (x.length == 0)
    return Double.NaN;
  double avg = mean(x);
  double sum = 0.0;
  for (int i = 0; i < x.length; i++) {
    sum += (x[i] - avg) * (x[i] - avg);
  }
  return sum / Math.sqrt(x.length - 1);
}

public static void main(String[] args) {
  double[] x = new double[] {
    2000.20, 1000.10, 3000.30, 4000.40,5000.50,
    6000.60,7000,70,8000.80,9000.90
  };
  double mean = mean(x);
  double deviation = var(x);
  System.out.println("The mean is "
      + formatDouble(mean));
  System.out.println("The standard deviation is "
      + formatDouble(deviation));
}

答案 1 :(得分:1)

问题是格式化程序返回一个String。你应该在metod签名中返回一个String

public static String var(double[] x) 

也是这一行

return sum / myFormatter.format(Math.sqrt(x.length - 1));

如果要返回String,则无效。您应该先进行计算,然后对其进行格式化。然后返回格式化的数字

编辑:试试这个,看它是否有效

public static String var(double[] x) {
    if (x.length == 0) 
        return null;

    double avg = mean(x, 10);
    double sum = 0.0;
    for (int i = 0; i < x.length; i++) {
        sum += (x[i] - avg) * (x[i] - avg);
    }
    DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
    double result = sum / Math.sqrt(x.length - 1);

    return myFormatter.format(result);

}

然后,您将double deviation = var(x);替换为String deviation = var(x);

修改2:完整代码

import java.text.DecimalFormat;
import java.util.*;
public class Mean {
    public static void main(String [] args){
        Scanner s = new Scanner(System.in);
            double[]x = new double[10];
            int i;
            for(i = 0;i < 10; i++){
                x[i] = s.nextDouble();
            }
            double mean = mean(x, i);
            String deviation = var(x);                  // changed to String
            System.out.println("The mean is " + mean);
            System.out.println("The standard deviation is " + deviation);
    }
public static double sum(double[] a) {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }
    public static double mean(double[]x, double i){
        if (x.length == 0) return Double.NaN;
        double sum = sum(x);
        return sum / x.length;
    }
    public static String var(double[] x) {         // changed return
        if (x.length == 0) 
            return null;

        double avg = mean(x, 10);
        double sum = 0.0;
        for (int i = 0; i < x.length; i++) {
            sum += (x[i] - avg) * (x[i] - avg);
        }
        DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
        double result = sum / Math.sqrt(x.length - 1);

        return myFormatter.format(result);

    }
}

更新:正确的标准偏差公式

double result = Math.sqrt(sum / (x.length - 1));

                          ^^

答案 2 :(得分:0)

你需要在打印时格式化double。如果你尝试使用var()函数然后赋值为double偏差,那么由于Var()返回的特殊字符,它将给出numberformatException。你可以运行这段代码它会给你输出。

 public static void main(String[] args) throws ParseException {
            Scanner s = new Scanner(System.in);
            double[]x = new double[10];
            int i;
            for(i = 0;i < 2; i++){
                x[i] = s.nextDouble();
            }
            double mean = mean(x, i);
            double deviation = var(x);
            DecimalFormat myFormatter = new DecimalFormat("#,##0.00");
            System.out.println("The mean is " + mean);
            System.out.println("The standard deviation is " +myFormatter.format(deviation));
        }
        public static double sum(double[] a) {
            double sum = 0.0;
            for (int i = 0; i < a.length; i++) {
                sum += a[i];
            }
            return sum;
        }
        public static double mean(double[]x, double i){
            if (x.length == 0) return Double.NaN;
            double sum = sum(x);
            return sum / x.length;
        }
        public static double var(double[] x) {
            if (x.length == 0) return Double.NaN;
            double avg = mean(x, 10);
            double sum = 0.0;
            for (int i = 0; i < x.length; i++) {
                sum += (x[i] - avg) * (x[i] - avg);
            }
            return sum / Math.sqrt(x.length - 1);
        }