Fork me on GitHub
打赏

查询自动售货机中的商品价格

一、题目描述

假设自动售货机出售四种商品,在屏幕上显示以下菜单(编号和选项),用户可以连续查询商品的价格,当查询次数超过5次时,自动退出查询;不到5次时,用户可以选择退出。当用户输入编号1~4,显示相应的商品价格(保留1位小数);输入0,退出查询;输入其他编号,显示价格为0.

 

二、所需技能

  • if 、switch 语句
  • for 循环
  • 数据类型转换

 

三、代码实现

    int choice,i;
    double price;
    for(i=1;i<=5;i++){
        //以下5行显示菜单
        printf("[1]Select crisps\n");  //查询薯片价格
        printf("[2]Select popcorn\n");  //查询爆米花价格
        printf("[3]Select chocolate\n");  //查询巧克力价格
        printf("[4]Select cola\n");  //查询可乐价格
        printf("[0]exit\n");  //退出查询
        printf("Enter choice:");
        scanf("%d",&choice);  
        
        if(choice == 0)  //如果输入为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.0;break;
            default:price = 0.0;break;
            
        
        }
        
    printf("price = %0.1f\n",price);
    }
    printf("Thank you\n");
    return 0;

      

 

posted @ 2017-04-01 19:39  Zoctopus_Zhang  阅读(2862)  评论(0编辑  收藏  举报
// function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);