-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPizzasAndPeopleWithInputValidation.java
More file actions
34 lines (26 loc) · 1.18 KB
/
PizzasAndPeopleWithInputValidation.java
File metadata and controls
34 lines (26 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
public class PizzasAndPeopleWithInputValidation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt for the number of pizzas
System.out.print("How many pizzas: ");
int numberOfPizzas = scanner.nextInt();
// Prompt for the number of slices per pizza
System.out.print("how many slices per pizza: ");
int slicesPerPizza = scanner.nextInt();
// Prompt for the number of people
System.out.print("How many people: ");
int numberOfPeople = scanner.nextInt();
// Calculate the total number of slices
int totalSlices = numberOfPizzas * slicesPerPizza;
if(numberOfPizzas < 0 || slicesPerPizza< 0 || numberOfPeople < 0 ){
System.out.println("Invalid input. PLease enter non-negative numbers.");
scanner.close();
return;
}
// Display the result
System.out.println("If you spilt all this pizza evenly among " + numberOfPeople + " people you will have " +totalSlices + " slices left over");
System.out.println("Mmmm! Cold pizza...");
scanner.close();
}
}