简体   繁体   English

数组中整数的总和,然后将整数乘以1.5

[英]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. 我在获取数组中整数的总和时遇到问题,并且在获取整数* 1.5的乘积时遇到问题。 My code below could be completely off as I am new to Java and have been at this for hours and hours. 由于我是Java新手,所以我下面的代码可能已经完全关闭了,而且已经花了几个小时了。 The purpose of the program is to enter the number of hours worked, each day, for 5 days. 该程序的目的是输入每天工作5天的小时数。 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] counter增加而不是hours[counter]

@bp_1, I re-do all the code again and pasted it below. @ bp_1,我再次执行所有代码并将其粘贴到下面。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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