Given the number of people attending a pizza party, output the number of needed pizzas and total cost. For the calculation, assume that people eat 2 slices on average and each pizza has 12 slices and costs $14.95.
Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
System.out.printf("Cost: $%.2f\n", cost)
import java.util.Scanner;
public class Main {
private final static double PIZZA_COST = 14.95;
private final static int SLICES = 12;
private final static float ONE_PERSON_EATS = 2;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of people: ");
String input = sc.nextLine();
//check input format (only digits)
if(!input.matches("\\d+")) {
System.out.println("Wrong input format");
} else {
int peopleCount = Integer.parseInt(input);
int pizzaCount = (int) Math.ceil(peopleCount * ONE_PERSON_EATS / SLICES);
double cost = pizzaCount * PIZZA_COST;
System.out.println("Number of pizzas: " + pizzaCount);
System.out.printf("Cost: $%.2f\n", cost);
}
sc.close();
}
}
Comments
Leave a comment