如何将用printf写入的值调用到另一个printf

问题描述 投票:0回答:1

我对编程非常陌生。

我正在尝试编写一个程序来从数组中调用水果的价格。但是我希望代码在写价格之前也要写水果的名字。如果输入2,如何将输出设置为“橙色价格:10”,而不仅仅是价格:10。

#include <stdio.h>
int main(void)
{
    int i;
    int a[3] = { 5,10,15 };
    printf("1) Apple");
    printf("2) Orange");
    printf("3) grape");

    while (1) {

        printf("Which fruit would u like : ");

        scanf("%d", &i);//Enter the fruit number
        if (i <= 0 || i >= 4) {
            printf("\nPlease use the correct number :");
        }
         else {
            printf("Price :%d", a[i]);
            break;
        }
    }
}

我对编程非常陌生。我正在尝试编写一个程序来从数组中调用水果的价格。但是我希望代码在写价格之前也要写水果的名字...

c printf scanf
1个回答
2
投票

只需定义一个名称数组,如

© www.soinside.com 2019 - 2024. All rights reserved.