#include<stdio.h> int main() { int price; int apple; int pear; int orange; int grape; int choice; printf("[1]apple\n"); printf("[2]pear\n") ; printf("[3]orange\n"); printf("[4]grape\n"); printf("[0]exit\n"); for (int i=1;i<=5;i++) { printf("请输入序号:"); scanf("%n",&choice) ; if(choice==1) printf("price=3"); if(choice==2) printf("price==2"); if(choice==3) printf("price==5"); if(choice==4) printf("price==6"); if(choice==0) break; } 哪里错了
时间: 2024-04-15 21:27:30 浏览: 19
在你的代码中,scanf函数的格式控制字符串`%n`是错误的。应该使用`%d`来读取整数值。另外,你的循环变量`i`没有被使用,可以将其删除。以下是修改后的代码:
```c
#include <stdio.h>
int main()
{
int price;
int choice;
printf("[1]apple\n");
printf("[2]pear\n") ;
printf("[3]orange\n");
printf("[4]grape\n");
printf("[0]exit\n");
for (;;)
{
printf("请输入序号:");
scanf("%d", &choice);
if (choice == 1)
price = 3;
else if (choice == 2)
price = 2;
else if (choice == 3)
price = 5;
else if (choice == 4)
price = 6;
else if (choice == 0)
break;
printf("price=%d\n", price);
}
return 0;
}
```
修改后的代码中,使用了无限循环 `for (;;)`,可以一直接受输入,直到用户选择退出(输入0)。对于不同的选择,使用了`if-else`语句来给`price`赋值,并输出对应的价格。
相关问题
#include <stdio.h> int main() { double t
#include <stdio.h> 是C语言中的一个预处理指令,它告诉编译器在编译过程中要包含stdio.h头文件。stdio.h是标准输入输出库的头文件,其中包含了一些常用的输入输出函数的声明。
int main() 是C语言程序的入口函数,程序从这里开始执行。在这个函数中,你可以编写你的程序逻辑。
double t 是一个变量的声明,它的类型是double,表示它可以存储双精度浮点数。这个变量的名字是t。
#include <stdio.h> #include <math.h> int main() { int a
#include <stdio.h> 是一个C语言的头文件,它包含了一些输入输出函数的声明,比如printf和scanf等。而#include <math.h> 是一个数学库的头文件,它包含了一些数学函数的声明,比如sqrt和sin等。
在你给出的代码中,int main() 是程序的入口函数,它表示程序从这里开始执行。在main函数中,定义了一个整型变量a,但是没有给它赋初值。
如果你想要使用printf函数来输出一些信息,可以在main函数中添加代码,比如:
int main()
{
int a = 10;
printf("a的值是:%d\n", a);
return 0;
}
这样就可以输出a的值为10。你可以根据需要修改a的值和输出的格式。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)