冬蕶 发表于 2024-3-28 19:40:24

可以帮我打出这个编程吗?

某航空公司规定:在旅游旺季(4、5、9、10月),如果同时订票数量10张及以上,则优惠20%,10张以下,则优惠10%;在旅游淡季(1-3、6-8、11、12月),如果订票数量10张及以上,则优惠40%,10张以下,则优惠20%。编程计算订票总价格。(机票单价、订票时间、订票数量由键盘输入)(程序中必须包含switch语句)

FishC 发表于 2024-3-28 19:40:36

当然可以!下面是使用switch语句实现的计算订票总价格的代码:

month = int(input("请输入订票的月份:"))
quantity = int(input("请输入订票数量:"))
price = float(input("请输入机票单价:"))

total_price = quantity * price

if month in :# 旅游旺季
    switch_case = {
      True: total_price * 0.8,   # 订票数量大于等于10张
      False: total_price * 0.9   # 订票数量小于10张
    }
else:# 旅游淡季
    switch_case = {
      True: total_price * 0.6,   # 订票数量大于等于10张
      False: total_price * 0.8   # 订票数量小于10张
    }

discounted_price = switch_case

print("订票总价格为:", discounted_price)

希望这个代码能够帮助到你!如果你还有其他问题,随时来问哦~

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

冬蕶 发表于 2024-3-28 19:50:14

FishC 发表于 2024-3-28 19:40
当然可以!下面是使用switch语句实现的计算订票总价格的代码:




这个出现了错误,请帮我改正
#include <stdio.h>
#include <math.h>
int main()
{
    float month,quantity,price,total_price;
    printf("请输入旅游的月份,订票的数量,单张票价:");
    scanf("%f,%f,%f",&month,&quantity,&price);

    total_price = quantity * price;

    if month in
    switch_case = {
      True: total_price * 0.8,   # 订票数量大于等于10张
      False: total_price * 0.9   # 订票数量小于10张
    }
    else:# 旅游淡季
    switch_case = {
      True: total_price * 0.6,   # 订票数量大于等于10张
      False: total_price * 0.8   # 订票数量小于10张
    }

    discounted_price = switch_case

    print("订票总价格为:", discounted_price)
    return 0;
}

FishC 发表于 2024-3-28 19:50:28

冬蕶 发表于 2024-03-28 19:50
这个出现了错误,请帮我改正
#include
#include
...

你的代码中有一些语法错误,我为你修改了一下:

#include <stdio.h>

int main() {
    int month, quantity;
    float price, total_price;
   
    printf("请输入旅游的月份,订票的数量,单张票价:");
    scanf("%d,%d,%f", &month, &quantity, &price);
   
    total_price = quantity * price;

    switch (month) {
      case 4:
      case 5:
      case 9:
      case 10:
            if (quantity >= 10) {
                total_price *= 0.8;
            } else {
                total_price *= 0.9;
            }
            break;
      default:
            if (quantity >= 10) {
                total_price *= 0.6;
            } else {
                total_price *= 0.8;
            }
            break;
    }
   
    printf("订票总价格为:%.2f\n", total_price);
   
    return 0;
}

请注意代码中的修改和调整。希望这次能够满足你的需求。如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

冬蕶 发表于 2024-3-28 19:51:15

FishC 发表于 2024-3-28 19:50
你的代码中有一些语法错误,我为你修改了一下:

#include


你可以告诉我我的语法错误具体出现在哪里吗?
#include <stdio.h>
#include <math.h>
int main()
{
    float month,quantity,price,total_price;
    printf("请输入旅游的月份,订票的数量,单张票价:");
    scanf("%f,%f,%f",&month,&quantity,&price);

    total_price = quantity * price;

    if month in
    switch_case = {
      True: total_price * 0.8,   # 订票数量大于等于10张
      False: total_price * 0.9   # 订票数量小于10张
    }
    else:# 旅游淡季
    switch_case = {
      True: total_price * 0.6,   # 订票数量大于等于10张
      False: total_price * 0.8   # 订票数量小于10张
    }

    discounted_price = switch_case

    print("订票总价格为:", discounted_price)
    return 0;
}

FishC 发表于 2024-3-28 19:51:31

冬蕶 发表于 2024-03-28 19:51
你可以告诉我我的语法错误具体出现在哪里吗?
#includ ...

你的代码中有几个语法错误:

1. 缺少头文件:需要添加 `#include <stdio.h>` 头文件以包含 `printf` 和 `scanf` 函数的声明。

2. switch 语句的语法错误:在 switch 语句中,需要使用整数类型的表达式进行判断,而不是浮点数。因此,将 `month` 的类型从 `float` 改为 `int`。

3. if-else 语句的错误:在 `if month in` 和 `else` 后面缺少冒号(:)以及缩进,导致语法错误。

4. switch_case 的赋值错误:在 C 语言中,没有直接将多个条件值与对应结果映射的内置结构(如字典)。你可以使用 if-else 条件语句实现相同的逻辑。

根据你的需求和代码修改的建议,这里给出已修正的代码:

#include <stdio.h>

int main() {
    int month, quantity;
    float price, total_price;

    printf("请输入旅游的月份,订票的数量,单张票价:");
    scanf("%d,%d,%f", &month, &quantity, &price);

    total_price = quantity * price;
   
    switch (month) {
      case 4:
      case 5:
      case 9:
      case 10:
            if (quantity >= 10) {
                total_price *= 0.8;
            } else {
                total_price *= 0.9;
            }
            break;
      default:
            if (quantity >= 10) {
                total_price *= 0.6;
            } else {
                total_price *= 0.8;
            }
            break;
    }
   
    printf("订票总价格为:%.2f\n", total_price);
   
    return 0;
}

这个版本的代码应该能够正确执行,如果你还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 可以帮我打出这个编程吗?