2013-12-22 75 views
1

我寫了一個程序,我不知道我應該如何做小數格式?我以爲我做得很對,顯然我不能正確地做十進制格式。可以,有人幫我用十進制格式?十進制格式問題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)); 

    } 
} 
+0

它取決於你希望你的小數點格式是怎麼樣的? –

+0

顯示你想要的輸出,電流輸出和輸入 –

+0

(我想要的)有點這樣:1,234.56(在第二個十進制數之後舍入)。 (我得到)錯誤! – Wumbo

回答

1

試試這個格式化你的雙(S)...我也不得不更新你的方法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)); 
} 
+0

我試過你的代碼,我有一個編譯錯誤?另外,用戶應該輸入數字... – Wumbo

1

的問題是格式化的回報一個字符串。你應該在梅託德簽名

public static String var(double[] x) 

而且這條線,如果你是返回一個字符串

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

都不行返回一個字符串。您應該先進行計算,然後對其進行格式化。然後返回格式的數字

編輯:試試這個,看看它是否工作

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)); 

          ^^ 
+0

這究竟在哪裏呢?我不確定我應該替換哪個公共靜態? – Wumbo

+0

這可能是一個荒謬的問題,但你可以幫我嗎? ( – Wumbo

+0

查看我的編輯。檢查是否可行 –

0

你需要在你嘗試在VAR()函數print.If的時間來格式化雙,然後用雙偏差分配那麼它會因特殊字符給NumberFormatException的它返回Var()。你可以運行這個代碼它會給你輸出。

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); 
     }