我的java while循环不会返回周工资
好的,第二部分。我在此基础上重新构造了代码,但现在我想不出一种方法,在不将empWeeklyPay初始化为零的情况下返回empWeeklyPay。如果我将其初始化为零,那么我的工资将始终为零?谁能想到我能做什么我的java while循环不会返回周工资,java,while-loop,Java,While Loop,好的,第二部分。我在此基础上重新构造了代码,但现在我想不出一种方法,在不将empWeeklyPay初始化为零的情况下返回empWeeklyPay。如果我将其初始化为零,那么我的工资将始终为零?谁能想到我能做什么 public double getWeeklyPay()//Call the getWeeklyHours method of the Emp's timecard to get the total //hours worked and then multiply the re
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;
}
public double getWeeklyPay()//调用Emp的时间卡的getWeeklyHours方法来获取总计
//工作小时数,然后将返回值乘以emp的小时费率并返回值。
{
TimeCard empTimeCard=TimeCard;
双周日;
双倍全速;
两个小时;
int i=0;
而(i)8
{
double empHourlyRate=getHourlyRate();
两个小时=0;
双倍注册时间=0;
双相图=0;
int sumRegHours=0;
//抓取加班和reghours并将其存储
otHours=empTimeCard.getHoursByDay(i)%8;
regHours=empTimeCard.getHoursByDay(i)-其他小时;
苏莫图尔+=苏莫图尔;
sumRegHours+=regHours;
双倍tmpBasePay=(sumRegHours*empHourlyRate)+(sumOtHours*empHourlyRate*1.5);
if(Integer.toString(getEmployeeId()).charAt(0)='0'| | Integer.toString(getEmployeeId()).charAt(0)='2'
||整数.toString(getEmployeeId()).charAt(0)='9')
//Java读取1010,因此需要转换为字符串以对值进行计数。
{
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;
如果(总注册小时数>34)
{
tmpBasePay-=(tmpBasePay*.06);
//empWeeklyPay=tmpBasePay;
empWeeklyPay=tmpBasePay;
}
其他的
{
empWeeklyPay=tmpBasePay;
}
}
return empWeeklyPay;//您的第一个while
循环,而只执行一次迭代,因为它总是遇到return
关键字。return
将使您脱离while
循环和getWeekPay()
方法
您只需在方法末尾返回empWeeklyPay
变量,因此此处仅最后一次返回有用。方法结尾应如下所示:
if(RegHours > 34) {
empBasePay -= (empBasePay * .06);
empWeeklyPay = empBasePay;
} else {
empWeeklyPay = empBasePay;
}
return empWeeklyPay;
编辑
为什么您总是得到0:
double empBasePay = (RegHours * empHourlyRate) + (otHours * empHourlyRate * 1.5);
由于RegHours
和otHours
等于0,您不妨编写:
double empBasePay = 0;
由于该方法的其余部分基于empBasePay
的基值,即0,因此您的方法将始终返回0。您的第一个while
循环,而只执行一次迭代,因为它总是遇到return
关键字。return
将使您脱离while
循环和您的方法getWeekPay()
方法
您只需在方法末尾返回empWeeklyPay
变量,因此此处仅最后一次返回有用。方法结尾应如下所示:
if(RegHours > 34) {
empBasePay -= (empBasePay * .06);
empWeeklyPay = empBasePay;
} else {
empWeeklyPay = empBasePay;
}
return empWeeklyPay;
编辑
为什么您总是得到0:
double empBasePay = (RegHours * empHourlyRate) + (otHours * empHourlyRate * 1.5);
由于RegHours
和otHours
等于0,您不妨编写:
double empBasePay = 0;
由于该方法的其余部分是基于empBasePay
的基值,即0,因此您的方法将始终返回0。以下是我的问题的答案。我能够找到答案。请参阅下面的代码,以防其他人在不久或遥远的将来遇到同样的问题:
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;
}
public-double-getWeeklyPay(){
//调用Emp时间卡的getWeeklyHours方法来获取总工作时间
//然后将返回值乘以emp的小时费率并返回值。
双周日;
双相图=0;
双倍sumRegHours=0;
int i=0;
while(i34){
empWeeklyPay-=(empWeeklyPay*.06);
每周一次;
}
其他的
每周一次;
}
以下是我问题的答案。我能够找到答案。请参阅下面的代码,以防其他人在不久或遥远的将来遇到同样的问题:
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;
}
public-double-getWeeklyPay(){
//调用Emp时间卡的getWeeklyHours方法来获取总工作时间
//然后将返回值乘以emp的小时费率并返回值。
双周日;
双相图=0;
双倍sumRegHours=0;
int i=0;
while(i