舍入错误给了我无效的答案 [英] Rounding error gives me invalid answer

查看:47
本文介绍了舍入错误给了我无效的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在四舍五入方面遇到了问题.出于某种原因,我无法将 testP 舍入到第十位.例如,在第一个给定的例子 (Alex Smith) 上,它给了我 82.0 的答案,但它应该是 82.6.

I'm having a problem with rounding. For some reason I can't round testP to the tenth place. For example, on the first given example (Alex Smith) it gives me the answer of 82.0, but it should be 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;
        }
}

推荐答案

更改

double testP =num/numT;

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

由于 numnumTint,您当前有一个整数除法,它将是 00.xyz...

As num and numT are int, you currently have a integer division, which will be 0 instead of 0.xyz...

这篇关于舍入错误给了我无效的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆