例3-8 查询价格

例3-8 查询价格

程序核心——两种循环语句的镶嵌

程序

#include<stdio.h>
int main()
{
	int choice,i;
	double price;
	
	for(i=1;i<=5;i++) 
	{
		printf("[1]Select crisps\n");
		printf("[2]Select popcrn\n");
		printf("[3]selert chocolat\n"); 
		printf("[4]Selert cola\n");
		printf("[0]exit\n");
		
		printf("Enter choice:");
		scanf("%d",&choice);
		
		if(choice==0)
			break;
		switch(choice)
		{
			case 1:price=3.0;break;
			case 2:price=2.5;break;
			case 3:price=4.0;break;
			case 4:price=3.5;break;
			default:price=0.0;break; 
		}
		printf("price=%0.1f\n",price); 
	}
	printf("Thanks\n");
	return 0;
 } 

结果

[1]Select crisps
[2]Select popcrn
[3]selert chocolat
[4]Selert cola
[0]exit
Enter choice:1
price=3.0
[1]Select crisps
[2]Select popcrn
[3]selert chocolat
[4]Selert cola
[0]exit
Enter choice:2
price=2.5
[1]Select crisps
[2]Select popcrn
[3]selert chocolat
[4]Selert cola
[0]exit
Enter choice:3
price=4.0
[1]Select crisps
[2]Select popcrn
[3]selert chocolat
[4]Selert cola
[0]exit
Enter choice:

分析

重点:switch语句判断的是一个常量

posted on 2019-03-23 09:33  凯*凯  阅读(101)  评论(0编辑  收藏  举报

导航