lundi 28 septembre 2020

(JAVA) Is there a more efficient way to do this?

This part of my program asks the user to enter the number of drinks they want and it must return "Invalid amount" if the number of drinks is negative.

System.out.print("Please enter the number of each drink that you like to purchase:");
System.out.println();
        
System.out.print("Small Coffee: ");
int smallCoffee = scnr.nextInt();
if (smallCoffee < 0) {
    System.out.println("Invalid amount")
    System.exit(1);
}
System.out.print("Medium Coffee: ");
int mediumCoffee = scnr.nextInt();
        
System.out.print("Large Coffee: ");
int largeCoffee = scnr.nextInt();
        
System.out.print("Banana Berry Smoothie: ");
int berrySmoothie = scnr.nextInt();
        
System.out.print("Mocha Shake: ");
int mochaShake = scnr.nextInt();
        
System.out.print("Raspberry Green Tea: ");
int greenTea = scnr.nextInt();

Instead of putting an if-statement under each option, is there any other way I can output "invalid amount" under each option?

Aucun commentaire:

Enregistrer un commentaire