gpt4 book ai didi

java - 开关声明(价格以 5 美分为增量)

转载 作者:行者123 更新时间:2023-11-30 10:11:02 25 4
gpt4 key购买 nike

金额不能少于 25 美分或多于 1 美元。此外,根据问题,价格以 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);

}
}

最佳答案

检查该条件非常容易。你可以使用这样的东西:

if (exchange >= 25 && exchange <= 100 && exchange % 5 == 0)
System.out.println("You bought an item for " + exchange + " cents and gave me a dollar");
else {
System.out.println("Invalid number!");
System.exit(0);
}
// rest of your code

关于java - 开关声明(价格以 5 美分为增量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com