我正在嘗試編寫一個程序,用於讀取每年從.txt文件中工作的小時數列表,分配/讀取(對正確的術語:)有一點模糊),它們放入一個數組,然後計算過量並打印它們。長期存儲百分比的問題
當我運行,控制檯輸出如下:
Employee 1 worked 1600 hours, an under-run of 160 or 0%
Employee 3 worked 1680 hours, an under-run of 80 or 0%
爲什麼百分比爲零?
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;
}
}