c++ 如何使程序在顺序IF语句中执行特定计算

xdnvmnnf  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(114)
#include <stdio.h>

int main()
{
   char ticketType;
   int totalBill, studAmount, ticketType_R= 6500, ticketType_G= 7500;

   printf("\nEnter your ticket type: ");
   scanf("%c", &ticketType);
   printf("\nEnter amount of students: ");
   scanf("%d", &studAmount);

   if(ticketType==ticketType_R)
   {
       totalBill==studAmount*6500;
       printf("\nYour ticket type is R and your total bill is: %d", ticketType_R, totalBill);
   }

   if (ticketType==ticketType_G)
   {
       totalBill==studAmount*7500;
       printf("\nYour ticket type is G and your total bill is: %d", ticketType_G, totalBill);
   }

   printf("\nThe amount of students attending are: %d ", studAmount);

   return 0;
}

我尝试了上面的代码,我希望它
1.用户选择的票据类型
1.打印学生出席人数
1.将学生人数乘以机票价格即可计算出总人数
1.打印总额

yuvru6vn

yuvru6vn1#

在你的情况下,我相信你要计算的价值根据门票类型和学生人数。
这就是我们可以做到的:

#include <stdio.h>

int main()
{
   char ticketType;
   int totalBill, studAmount, ticketType_R= 6500, ticketType_G= 7500;

    printf("Enter the ticket type (R/G): "); // Input ticket type
    scanf("%c", &ticketType);

    // Use of Sequential if-else statement to calculate the total bill
    if (ticketType == 'R')
    {
        printf("Enter the number of students: ");
        scanf("%d", &studAmount);
        totalBill = ticketType_R * studAmount;
        printf("Your ticket type is R and your total bill is: %d", totalBill);
    }
    else if (ticketType == 'G')
    {
        printf("Enter the number of students: ");
        scanf("%d", &studAmount);
        totalBill = ticketType_G * studAmount;
        printf("Your ticket type is G and your total bill is: %d", totalBill);
    }
    else
    {
        printf("Invalid ticket type");
    }

    // Total Students 
    printf("\nTotal number of students: %d", studAmount);

    return 0;
}

如果你有兴趣用Python来做这件事,那就是你要做的。

ticket_type = input("Enter the ticket type (R/G): ")
stud_amount = int(input("Enter the number of students: "))

if ticket_type == "R":
    total_bill = 6500 * stud_amount
    print("Your ticket type is R and your total bill is: ", total_bill)
elif ticket_type == "G":
    total_bill = 7500 * stud_amount
    print("Your ticket type is G and your total bill is: ", total_bill)
else:
    print("Invalid ticket type")

print("Your Total number of students: ", stud_amount)

相关问题