简体   繁体   中英

Sum of integers in an array and multiplying integer by 1.5

I am having an issue getting the sum of the integers in my array AND an issue getting the product of an integer * 1.5. My code below could be completely off as I am new to Java and have been at this for hours and hours. The purpose of the program is to enter the number of hours worked, each day, for 5 days. With that, and the pay rate, you're supposed to output the average hours worked, total hours, and total pay. The pay should also include overtime if there is any. Any help would be appreciated.

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...

In place of

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

write

for(counter = 0; counter < 5; counter++)
  1. semicolon removed.
  2. counter incremented instead of hours[counter]

@bp_1, I re-do all the code again and pasted it below. It WORKS. There was some fundamental error you making while coding. Compare your code with mine and you will see the difference.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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