首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的java while循环没有返回每周的工资。

我的java while循环没有返回每周的工资。
EN

Stack Overflow用户
提问于 2012-03-02 03:55:36
回答 2查看 274关注 0票数 0

好的,这是第二部分。我在此基础上重新编写了代码,但现在我想不出在不将empWeeklyPay初始化为0的情况下返回它的方法。如果我将它初始化为零,那么我的工资将始终为零?有谁能想到我能做些什么吗?

代码语言:javascript
复制
public double getWeeklyPay()//Call the getWeeklyHours method of the Emp's timecard to      get the total
//hours worked and then multiply the returned value by the emp's hourly rate and return the value.
{
TimeCard empTimeCard = timeCard;
double empWeeklyPay;
double totalOtHours;
double totalRegHours;
int i = 0;

while(i <= empTimeCard.NUMDAYS && empTimeCard.getHoursByDay(i) > 8)
{
double empHourlyRate = getHourlyRate();
double otHours = 0;
double regHours = 0;
double sumOtHours = 0;
int sumRegHours = 0;

//grabbing the overtime and reghours and storing them
otHours = empTimeCard.getHoursByDay(i) % 8;
regHours = empTimeCard.getHoursByDay(i) - otHours;
sumOtHours += otHours;
sumRegHours += regHours;
double tmpBasePay = (sumRegHours * empHourlyRate) + (sumOtHours * empHourlyRate * 1.5);

if(Integer.toString(getEmployeeId()).charAt(0) == '0' || Integer.toString(getEmployeeId()).charAt(0) == '2'
|| Integer.toString(getEmployeeId()).charAt(0) == '9')
//Java reads in 1010 so need to convert to a string to do a count on the value.
{
  tmpBasePay += (tmpBasePay * .10);        
  i++;
}
else if(Integer.toString(getEmployeeId()).charAt(0) == '3')
{
  tmpBasePay -= (tmpBasePay *.10);
  i++;
}
else if(Integer.toString(getEmployeeId()).charAt(0) == '8')
{ 
  tmpBasePay += (tmpBasePay * .20);
  i++;
}
totalRegHours = regHours;
totalOtHours = sumOtHours;
if(totalRegHours > 34)
{
tmpBasePay -= (tmpBasePay * .06);
//empWeeklyPay = tmpBasePay;
empWeeklyPay = tmpBasePay;
}
else
{
empWeeklyPay = tmpBasePay;
}

}

代码语言:javascript
复制
return empWeeklyPay;// <---need to return back this value.   
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-07 06:50:57

这是我问题的答案。我能够弄明白这一点。请查看下面的代码,以防其他人在不久或遥远的将来遇到同样的问题:

代码语言:javascript
复制
public double getWeeklyPay() {
// Call the getWeeklyHours method of the Emp's timecard to get the total hours worked
// and then multiply the returned value by the emp's hourly rate and return the value.

double empWeeklyPay;
double sumOtHours = 0;
double sumRegHours = 0;
int i = 0;


while(i < getTimeCard().NUMDAYS) {
// grabbing the overtime and regHours and storing them

    double otHours = 0;
    double regHours = 0;
    otHours += getTimeCard().getHoursByDay(i);
    regHours += getTimeCard().getHoursByDay(i) - otHours;
    sumOtHours += otHours;
    sumRegHours += regHours;
    i++;
}    

empWeeklyPay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 1.5);

if(Integer.toString(getEmployeeId()).charAt(0) == '0' || Integer.toString(getEmployeeId()).charAt(0) == '2'
|| Integer.toString(getEmployeeId()).charAt(0) == '9') {
// Java reads in 1010 so need to convert to a string to do a count on the value.

    double tmpBasePay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 1.5);  
    tmpBasePay += (tmpBasePay * .10); 
    empWeeklyPay = tmpBasePay;
}

else if(Integer.toString(getEmployeeId()).charAt(0) == '3') {
  double tmpBasePay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 1.5);
    tmpBasePay -= (tmpBasePay * .10);
    empWeeklyPay = tmpBasePay;
}

else if(Integer.toString(getEmployeeId()).charAt(0) == '8') { 
    double tmpBasePay = (sumRegHours * getHourlyRate()) + (sumOtHours * getHourlyRate() * 1.5);  
    tmpBasePay += (tmpBasePay * .20);
    empWeeklyPay = tmpBasePay;
}

if(sumRegHours > 34) {
    empWeeklyPay -= (empWeeklyPay * .06 );
    return empWeeklyPay;
}

else
    return empWeeklyPay;
}
票数 1
EN

Stack Overflow用户

发布于 2012-03-02 04:00:16

由于总是遇到return关键字,您的第一个while循环只执行一次迭代。return将使您脱离while循环,并您的getWeekPay()方法。

您只需要在方法的末尾返回empWeeklyPay变量,所以这里只有最后一个返回有用。方法的结尾应该如下所示:

代码语言:javascript
复制
if(RegHours > 34) {
    empBasePay -= (empBasePay * .06);
    empWeeklyPay = empBasePay;
} else {
    empWeeklyPay = empBasePay;
}
return empWeeklyPay;

编辑

为什么你总是得到0:

代码语言:javascript
复制
double empBasePay = (RegHours * empHourlyRate) + (otHours * empHourlyRate * 1.5);

RegHoursotHours等于0时,您不妨这样写:

代码语言:javascript
复制
double empBasePay = 0;

由于该方法的其余部分基于empBasePay的基值,即0,因此您的方法将始终返回0。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9523036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档