2013-05-09 51 views
0

我遇到舍入問題。出於某種原因,我無法將testP排到第十位。例如,在第一個給出的例子(Alex Smith)中,它給了我82.0的答案,但它應該是82.6。舍入錯誤給我無效的答案

這裏是有例子的分配:http://www.hpcodewars.org/past/cw5/problems/Average.htm

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.PrintWriter; 
import java.math.*; 
import java.util.Scanner; 


public class Average { 
     public static void main(String[] args) { 
       int num = 0, temp =0;; 
       String fileName="AverageInput.txt"; 
       String fileName2="AverageOutput.txt"; 
       Scanner inputStream = null; 
       PrintWriter outputStream = null; 
       double homeworkP = 0; 

       //read 
       try{ 
         inputStream = new Scanner(new File(fileName)); //try to open the file 
       } 
       catch(Exception e){ 
         System.out.println("Could not open the file named "+ fileName); // if it doesn't find it, tell them 
         System.exit(0); // and then exit. 
       } 
       //write 
       try{ 
         outputStream = new PrintWriter(new FileOutputStream(fileName2,true)); //try to create the file 
       } 
       catch(Exception e){ 
         System.out.println("Could not open the file named "+ fileName2); // if it doesn't find it, tell them 
         System.exit(0); // and then exit. 
       } 


       int numH = inputStream.nextInt(); 
       int numT = inputStream.nextInt(); 
       int[] arrayH = new int[numH]; 
       outputStream.println("Averages"); 
       String blank = inputStream.nextLine(); 
       while (inputStream.hasNextLine()){ 
         String line = inputStream.nextLine().replaceAll(" H","").replaceAll(" T",""); 
         String[] nameParts = line.split(" "); 
         String name = nameParts[0] +" "+ nameParts[1]; 
             for (int i =0, p=2; i < numH; i++, p++){  //collects homework grades. 
               arrayH[i] = Integer.valueOf(nameParts[p]); 
               temp = temp + arrayH[i]; 
             } 
             homeworkP = (temp - arrayMin(arrayH))/(numH-1) ; //gets percentage as rounded decimal. 
             //System.out.println("homeP: " + homeworkP); 
             temp =0; 
             num = 0; 
             for (int p = numH; p < numT + numH; p++){  //collects test grades. 
                 num = num + Integer.valueOf(nameParts[p]); 
             } 
             double testP =num/numT; //gets percentage as decimal. 
             System.out.println("homep: "+ homeworkP + "TestP: "+ testP); 
             double TotalP = (homeworkP * 40) + (testP * 60); 
             System.out.println(name + " " + TotalP); 
       } 
         outputStream.close(); 
         inputStream.close(); 
     } 


     public static int arrayMin(int[] arr) { 
      int i = 0; 
      int min = Integer.MAX_VALUE; 
      if (arr == null) { 
       return 0; 
      } else { 
       while (i < arr.length) { 
        if (arr[i] < min) { 
         min = arr[i]; 
        } 
        i++; 
       } 
      } 
      return min; 
     } 
} 
+0

「出於某種原因,我無法將'testP'舍入到第十位」意味着您已嘗試過。你有什麼嘗試?它做錯了什麼?提示:你將要嘗試ROUNDING FUNCTION。 – Colleen 2013-05-09 20:50:39

+0

對不起,沒有進一步解釋,這是一段代碼,我多次編輯。我曾嘗試過使用Math.round和其他東西。我可以建議你更親切嗎? – 2013-05-09 21:10:15

回答

5

變化

double testP =num/numT; 

double testP = ((double) num)/numT; 

由於numnumTint,你現在有一個整數除法, 哪一個 將0而不是0.xyz ...

+0

謝謝你的工作!我無法理解,這很容易。 – 2013-05-09 20:52:28