1

我在获取数组中整数的总和时遇到问题,并且在获取整数 * 1.5 的乘积时遇到问题。我下面的代码可能完全关闭,因为我是 Java 新手,并且已经使用了几个小时。该程序的目的是输入每天工作 5 天的小时数。有了这个和工资率,你应该输出平均工作时间、总小时数和总工资。工资还应包括加班(如果有的话)。任何帮助,将不胜感激。

String name;
String id;
int payRate;
int[] hours = new int[5];
int avgHours;
int totalPay;
int totalHours = 0;
int counter;
int overTime = 0;

//housekeeping
System.out.print("Enter the Employee's name: ");
inputString = input.readLine();
name = inputString;

System.out.print("Enter the Employee's ID: ");
inputString = input.readLine();
id = inputString;

System.out.print("Enter the Employee's pay rate: ");
inputString = input.readLine(); 
payRate = Integer.parseInt(inputString);

//hoursPay
counter = 0;
for(hours[counter] = 0; counter < 5; counter++)
{
    System.out.print("How many hours did the employee work? ");
    inputString = input.readLine();
    hours[counter] = Integer.parseInt(inputString);
}//endfor
    for(totalHours = 0; counter < 5; hours[counter]++);
    {
        totalHours += hours[counter];
        if(totalHours > 40)
        {
            overTime = payRate + (payRate / 2);
        }//endif
    }//endwhile

//print
if(counter == 5)
{
    System.out.println(name + " " + id + " $" + payRate + "/hour" );

    avgHours = totalHours / counter;
    totalPay = totalHours * payRate + overTime; 
    System.out.println...
    System.out.println...
    System.out.println...
4

2 回答 2

0

@bp_1,我再次重新执行所有代码并将其粘贴在下面。有用。您在编码时犯了一些基本错误。将您的代码与我的代码进行比较,您将看到不同之处。

String name;
String id;
int payRate;
int[] hours = new int[5];
int avgHours;
int totalPay;
int totalHours = 0;
int counter;
int overTime = 0;
Scanner input = new Scanner(System.in);
//housekeeping
System.out.print("Enter the Employee's name: ");
String inputString = input.nextLine();
name = inputString;

System.out.print("Enter the Employee's ID: ");
inputString = input.nextLine();
id = inputString;

System.out.print("Enter the Employee's pay rate: ");
inputString = input.nextLine();
payRate = Integer.parseInt(inputString);

//hoursPay
counter = 0;
for (hours[counter] = 0; counter < 5; counter++) {
System.out.print("How many hours did the employee work? ");
inputString = input.nextLine();
hours[counter] = Integer.parseInt(inputString);
}//endfor

counter = 0;// reset counter here
for (totalHours = 0; counter < 5; counter++) {
totalHours += hours[counter];
if (totalHours > 40) {
overTime = payRate + (payRate / 2);
}//endif
}//end of for loop

if (counter == 5) {
System.out.println(name + " " + id + " $" + payRate + "/hour");
avgHours = totalHours / counter;
totalPay = totalHours * payRate + overTime;
System.out.println("Average Hours: " + avgHours);
System.out.println("Total pay: " + totalPay);
System.out.println("Total Hours: " + totalHours);
System.out.println("Overtime ($): " + overTime);
}//end of if
于 2013-08-26T09:49:37.200 回答
0

代替

for(totalHours = 0; counter < 5; hours[counter]++);

for(counter = 0; counter < 5; counter++)
  1. 分号被删除。
  2. counter递增而不是hours[counter]
于 2013-08-26T04:21:27.760 回答