Сумма не может быть меньше 25 центов или больше одного доллара.Кроме того, в соответствии с проблемой, цены с шагом в 5 центов.
Если пользователь вводит недопустимые данные, должно отображаться сообщение об ошибке и НЕТ обработки.
Я хотел быЗнай также, могу ли я использовать цикл вместо оператора switch
Заранее спасибо:)
Мой код:
import java.util.Scanner;
public class VendingMachine {
public static void main (String[]args) {
int exchange;
int quarters;
int dimes;
int nickels;
int pennies;
Scanner money = new Scanner(System.in);
System.out.println("Enter a price of item\n"
+ "(from 25 cents to a dollar, in 5-cent increments.");
exchange = money.nextInt();
switch(exchange)
{
case 25:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 30:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 35:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 40:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 45:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 50:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 55:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 60:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 65:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 70:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 75:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 80:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 85:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 90:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 95:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
case 100:
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar"); break;
default:
System.exit(0);
}
exchange = 100 - exchange;
System.out.println("Amount in Return: " + exchange + " cents");
quarters = exchange / 25;
exchange = exchange % 25;
dimes = exchange / 10;
exchange = exchange % 10;
nickels = exchange / 5;
exchange = exchange % 5;
pennies = exchange;
System.out.println("Quarters: " + quarters + "\nDimes: " + dimes + "\nNickels: " + nickels + "\nPennies: " + pennies);
}
}