c - 我用什么代码来询问数量和付款?
问题描述
我认为问题在嵌套 if 开关之前开始,因为在代码末尾它应该输出 and 的结果sales = quantity * price
并change = payment - total amount
询问quantity
andPayment
#include<stdio.h>
main() {
int choice,ch,Quantity,Payment;
printf(" SELECT 1 OPPO \n SELECT 2 SAMSUNG \n SELECT 3 HUAWEI \n\n\n CHOICE: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("\n OPPO!!!");
printf("\n\nSelect 1 oppo F11 \n Select 2 OPPO A5S \n select 3 OPPO F9 PRO \n\n CHOICE: ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\n Oppo F11 smartphone was launched in March 2019. The phone comes with a 6.50-inch touchscreen display.\n");
printf("\n P 18,990.00");
break;
case 2:
printf("\n Oppo A5s is built quite sturdily and feels light in hand considering its large footprint.\n");
printf("\n P 6,990.00");
break;
case 3:
printf("\n Oppo F9 Pro features a dual rear camera setup.\n");
printf("\n P 15,990.00");
break;
}
break;
case 2:
printf("\n SAMSUNG!!!");
printf("\n\nSelect 1 SAMSUNG GALAXY A10\n Select 2 SAMSUNG NOTE 10\n select 3 SAMSUNG S9\n\nCHOICE: ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\n SAMSUNG Galaxy A10 is not a light smartphone, but it is also rather large, making its heft appropriate at least.\n");
printf("\n P 6,990.00");
break;
case 2:
printf("\n The Galaxy Note 10 is Samsung’s easiest to use S Pen-toting phone yet, and while there may not be any game-changing features to make this a must-buy handset, it’s a solid addition to the Note range.\n");
printf("\n P 53,990.00");
break;
case 3:
printf("\n The Samsung Galaxy S9 features a 12MP rear camera with dual-aperture technology. This is the big takeaway from the S9.\n");
printf("\n P 45,990.00");
break;
}
break;
case 3:
printf("\nHUAWEI!!!");
printf("\n\nSelect 1 HUAWEI P30 \n Select 2 HUAWEI NOVA 5T \n select 3 HUAWEI NOVA 3I\n\n CHOICE: ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\n The Huawei P30 has one of the best cameras we've seen in a phone, with features like incredible optical zoom and night mode that other handsets could only dream of, but other than that it feels more like a decent mid-range handset than a flagship.\n");
printf("\n P 50,990.00");
break;
case 2:
printf("\n Hiding the front camera beneath the screen, HUAWEI nova 5T’s screen retains its full integrity, revolutionizing user experience.\n");
printf("\n P 14,990.00");
break;
case 3:
printf(" \n HUAWEI nova 3i provides three color models for you to choose from, Pearl White, Black and Iris Purple. With beautiful hues of color on the back glass and metal mid-frame, you will enjoy wonderful visual and handling experience.\n");
printf("\n P 14,990.00");
break;
}
break;
default:
printf("\n TRY AGAIN CHOOSE YOUR PHONE BRAND WISELY!!!");
}
scanf("%d", &quantity);
total Amount = quantity * price;
scanf("%d", &payment);
scanf("change = Payment - total Amount);
getch();
}
解决方案
您应该定义另一个变量来指示您选择的手机的价格,例如 phonePrice。在您的嵌套开关中,分配该 phonePrice。然后您可以使用该价格变量来计算金额和所有其他内容。
顺便说一句,在scanf之前使用一个printf来提示用户输入数量。不要只是简单地使用 scanf 而不提示用户做什么。
推荐阅读
- php - 从 Woocommerce 中的可变产品获取所有变体的总库存
- c# - ThreadPoolTaskScheduler internals - Task.WaitAll() 的参数顺序会改变执行顺序吗?
- codenameone - 属性值更改时更新 CodenameOne sidemenu 命令
- environment-variables - conemu 将 env var 传递给 WSL bash 终端
- c# - 为什么我在发布 Web 服务时没有看到 SmartCard 的 PIN 提示?C#, IIS
- python - 有没有更好的方法来打印超过 1 个学生的面向对象编程?
- php - PHP - 检查用户名是否存在或提交的用户名是否是当前的
- android - 片段返回导航
- bash - 仅在文件存在时执行命令
- c# - 如何将数据存储在对象数组中并在 C# 中一次显示/读取所有数据