我在获取数组中整数之和时出现问题,并且遇到整数* 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...
答案 0 :(得分:0)
取代
for(totalHours = 0; counter < 5; hours[counter]++);
写
for(counter = 0; counter < 5; counter++)
counter
递增而不是hours[counter]
答案 1 :(得分: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