Question: In my assignment I'm to modify the AvgSalary code to allow the user to set the counter value before entering into the loop. Below is
In my assignment I'm to modify the AvgSalary code to allow the user to set the counter value before entering into the loop.
Below is my teachers comments after I tried to modify an average salary code. I porvided in BOLD the orginal code he speaks of. Below that is code I submitted, but he says I'm not there yet. For some reason I'm missing something.
Still not there. This really is not that tough of an assignment and I think you are over thinking it. So, here is some hints in the form of comments that would follow the scanner declaration in the original code:
//SMH Ask the user for the number of salaries to enter
//SMH Receive that number while assigning it to an integer variable
//SMH Use that variable instead of the hard-coded number (5) below Essentially, you are adding two lines of code after the scanner and changing the number in the while condition to the variable used. Give it one more shot.
//Imports import java.util.Scanner;
//Begin Class AvgSalSent public class AvgSalSent {
//Begin Main Method public static void main(String[] args) {
//Declarations int salCnt = 0; double salary = 1; //Used as sentinel value in loop double tot = 0; double salAvg = 0;
//New Scanner object Scanner sc = new Scanner(System.in);
//User instruction placed outside of loop for a one-time display System.out.println("Enter a 0 to end program entries.");
//While loop to find average while (salary != 0) { System.out.printf("Enter the salary/hr amount %d: $", salCnt + 1); salary = sc.nextDouble(); tot += salary; //Add entry to running total salCnt += 1; //Increment the counter }
/*Determine the average *If user enters zero to end entry of salaries, decrement the counter so *a proper average can be determined. */ if (salary == 0){ salCnt -= 1; //Decrement counter salAvg = tot / salCnt; }
//Output the average System.out.printf("The average of the %d salaries is: $%.2f ", salCnt, salAvg);
}//End Main Method } //End Class AvgSalSent
Here is what I submitted, and it's still not there based on his above comments.
//Imports
import java.util.Scanner;
//Begin Class AvgSalaryDoWhile
public class AvgSalaryDoWhile {
//Begin Main Method
public static void main(String[] args) {
//Declarations
int salCnt = 0;
double salary = 0;
double tot = 0;
double salAvg = 0;
String ans;
//New Scanner object
Scanner sc = new Scanner(System.in);
//Begin Do-While loop to find average
do {
System.out.printf("Enter the salary/hr amount %d: $", salCnt + 1);
salary = sc.nextDouble();
tot += salary; //Add entry to running total
salCnt += 1; //Increment the counter
System.out.print("Enter another salary? (Y for Yes, N for No): ");
ans = sc.next();
} while (ans.equalsIgnoreCase("Y"));//End do-while loop
//Determine the average
salAvg = tot / salCnt;
//Output the average
System.out.printf("The average of the %d salaries is: $%.2f ", salCnt, salAvg);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
