jeudi 17 décembre 2020

output different from sample output when use loop and if-statement in java programming

Software Workshop offers programming seminars to companies. The price per person depends on the number of people a company registers. The following table shows the charges per registrant.[charges per registrant][1]

For example, if a company registers three people, then the amount owed by that company is $1350. Your manager asks you to construct a program to handle charges. The program should allow the user to enter the registration for as many companies as desired, and then prompt the user to enter the number of people registers for that company. The program should allow the user to enter 0 to number of people register to end the program, and then displays the total number of people registered, the total charge, and average charge per registrant. For example, if one company registers three people and a second company registers seven, the total number of people registered is 10, the total charge is $4150, and the average per registrant is $415.00

I already done it like this but, the total charge and average are different from sample output. Did I miss something or did i do this wrong? Can you please help me for this?

import java.util.Scanner;

public class ProgrammingSeminar {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int registrant, charges = 0, totalregistrant = 0;
    int counter=1;      //while user is not inputting "0" to exit the program
    double total = 0;
    
    System.out.println("|-------------------------------|-------------------------------|");
    System.out.println("|   Number of registrants   |   Charges per person ($)  |");
    System.out.println("|-------------------------------|-------------------------------|");
    System.out.println("|       1-5     |       450     |");
    System.out.println("|-------------------------------|-------------------------------|");
    System.out.println("|       6-10        |       400     |");
    System.out.println("|-------------------------------|-------------------------------|");
    System.out.println("|       11 or more  |       250     |");
    System.out.println("|-------------------------------|-------------------------------|");
    
    do {
        Scanner sc = new Scanner(System.in);
        System.out.print("Company #" + counter + " - Number of Participant(s) (Enter 0 to exit): ");
        registrant = sc.nextInt(); //program exit when integer=0
        
        if (registrant >= 1 && registrant <= 5)
            charges = (registrant*450);
        else if (registrant >= 6 && registrant <= 10)
            charges = (registrant*400);
        else if (registrant >=11)
            charges = (registrant*250);
            totalregistrant += registrant;
            counter++;
    } while (registrant > 0); //check if registrant is greater than 0
    total = total + charges;
    
    System.out.println("The total number of people registered is " + totalregistrant);
    System.out.println("The total charges is $" + charges);
    System.out.println(String.format("The average per registrant is $%.2f", (total/totalregistrant)));
    
}

}

Aucun commentaire:

Enregistrer un commentaire