简体   繁体   English

长时间存储百分比的问题

[英]Issue with storing percentages in a long

I am trying to write a program for class that reads a list of hours worked annually from a .txt file, assigns/reads (A little fuzzy on the proper terminology :)) them into an array, and then calculates overages and prints them. 我正在尝试为课堂编写一个程序,该程序从.txt文件中读取每年的工作时间列表,然后将它们分配/读取(对适当的术语有些迷惑:),然后将它们分配到一个数组中,然后计算出剩余量并打印出来。

When I run, console output is as follows: 当我运行时,控制台输出如下:

Employee 1 worked 1600 hours, an under-run of 160 or 0%
Employee 3 worked 1680 hours, an under-run of 80 or 0%

Why are the percentages zero? 为什么百分比为零?

import java.io.*;
import javax.swing.*;

class Calculator 
{
 public static void main(String args[])
  {

  try{

      String filePathString=JOptionPane.showInputDialog(null,"What is the file path?");  

      FileInputStream fstream = new FileInputStream("C:\\hello.txt");
      BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
      String strLine;
      int counter = 0;
      long[] Array = new long[5];   


      //Read File Line By Line
      while (counter <= 4)

        {
            strLine = br.readLine();
            Array[counter] = Long.parseLong(strLine); 
            OverageUnderage(Array[counter], counter);
            counter = counter + 1;
        }

  //Close the input stream
  in.close();

    }

  catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }

 public static long OverageUnderage(long hours, long counter)
 {
 long overageAmount = 0;
 long overagePercent = 0;

     if (hours < 1760)
     {    
        overageAmount = 1760 - hours;
        overagePercent = ((overageAmount/1760)*100);

        System.out.println("Employee "+counter+" worked "+hours+" hours, an under-run of "+overageAmount+" or "+overagePercent+"%");
    }

    return counter;
 }

}

The number ( overageAmount / 1760 ) is being treated as a long, not a double, which you'll need. 该数字( overageAmount / 1760 )被视为您需要的长( overageAmount / 1760 ) ,而不是双( overageAmount / 1760 ) Try: 尝试:

overagePercent = ( ( overageAmount / 1760.0 ) * 100 );

using the .0 on 1760 will have it treated as a decmial value. 在1760年使用.0会将其视为decmial值。 This will prevent the rounding to 0 issue you were seeing, and allow the calculation to have decimals for the rest of the calculations, providing you with your desired result. 这样可以避免四舍五入到您看到的问题,并使其余计算的小数点后一位,从而为您提供所需的结果。

That's why (IMHO) you divide long instead of doubles: 这就是为什么(恕我直言)您划分长而不是双倍:

overagePercent = ((overageAmount / 1760)*100);

Here overageAmount/1760 returns 0 ! 这里overageAmount/1760返回0
So you have two solutions: 因此,您有两种解决方案:

  1. First convert overageAmount to double and then execute the operation 首先将overageAmount转换为两倍,然后执行操作
  2. Divide with float 1760.0 so resulting number is already a float 除以浮点数1760.0因此结果数字已经是浮点数

The order you perform the calculations matters, especially for int values. 执行计算的顺序很重要,尤其是对于int值。

Instead of 代替

overagePercent = overageAmount / 1760 * 100;

which is likely to be 0 * 100 or 0 you can do 您可能会选择0 * 100或0

overagePercent = overageAmount * 100 / 1760;

this could still be 0, but only if the answer is less than 1. 它仍然可以是0,但前提是答案小于1。

If you need to get answer in decimals, you have to use double or decimal. 如果您需要以小数形式获得答案,则必须使用双精度或十进制。 You cant use Long which is for integers. 您不能将Long用于整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM