Java菜单驱动程序,用于确定新会员的成本


当我们去任何地方办理任何类型的会员时,他们会针对不同的套餐提供不同的价格。例如,银卡会员、金卡会员、钻石会员、白金会员等,其中银卡会员的费用低于其他会员,而白金会员的费用高于其他套餐。

在本文中,我们将学习如何使用Java编程语言来检查会员的费用。我们将使用switch case来实现应用程序。

举几个例子:

示例1

假设我们有1840卢比的基本会员价格,您想要一个银卡会员。银卡会员的费用是1932卢比。银卡会员的费用计算方式为:基本金额 + 10%税 – 5%折扣。

示例2

假设我们有1840卢比的基本会员价格,您想要一个金卡会员。金卡会员的费用是2116卢比。金卡会员的费用计算方式为:基本金额 + 20%税 – 5%折扣。

示例3

假设我们有1840卢比的基本会员价格,您想要一个钻石会员。钻石会员的费用是2300卢比。钻石会员的费用计算方式为:基本金额 + 30%税 – 5%折扣。

示例4

假设我们有1840卢比的基本会员价格,您想要一个白金会员。白金会员的费用是2484卢比。白金会员的费用计算方式为:基本金额 + 40%税 – 5%折扣。

算法

步骤1 - 请用户输入当前基本会员价格。

步骤2 - 显示菜单。

步骤3 - 请用户输入他们的选择。

步骤4 - 使用switch case跳转到所选选项并执行操作。

步骤5 - 打印结果。

让我们看看程序来更好地理解它。

示例

import java.util.*; public class Main{ public static void main(String args[]){ int num; Scanner sc = new Scanner(System.in); System.out.print("Enter the base price to opt for a membership: "); num = sc.nextInt(); mainLoop: while (true) { System.out.println("\n***Menu***"); System.out.println("1. Check cost for Silver Membership"); System.out.println("2. Check cost for Gold Membership"); System.out.println("3. Check cost for Diamond Membership"); System.out.println("4. Check cost for Platinum Membership"); System.out.println("5. Terminate the program"); System.out.println("Enter action number (1-5)"); int command = sc.nextInt(); switch(command) { case 1: if(num>1000){ double a1 = num + (.1*num) - (.05*num); System.out.println("Cost for Silver Membership is " + a1); } else{ System.out.println("Base membership price should be greater than 1000"); } break; case 2: if(num>1000) { double a2 = num + (.2*num) - (.05*num); System.out.println("Cost for Gold Membership is "+a2); } else { System.out.println("Base membership price should be greater than 1000"); } break; case 3: if(num>1000) { double a3 = num + (.3*num) - (.05*num); System.out.println("Cost for Diamond Membership is "+a3); } else { System.out.println("Base membership price should be greater than 1000"); } break; case 4: if(num>1000) { double a4 = num + (.4*num) - (.05*num); System.out.println("Cost for Platinum Membership is "+a4); } else{ System.out.println("Base membership price should be greater than 1000"); } break; case 5: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }

输出

Enter the base price to opt for a membership: 2000

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
1
Cost for Silver Membership is 2100.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
2
Cost for Gold Membership is 2300.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
3
Cost for Diamond Membership is 2500.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
4
Cost for Platinum Membership is 2700.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
5
Program terminated

在本文中,我们探讨了如何使用菜单驱动的方法在Java中检查会员费用。

更新于:2022年12月27日

291次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告