C代码,我对这个控制语句程序缺少什么?

时间:2016-04-07 07:04:54

标签: c

我正在制作一个节目。这就是我想要实现的目标; 该计划应提供一个可供选择的工资率菜单。使用开关选择支付率。运行的开始应该是这样的: enter image description here

如果选择了选项1到4,程序应该请求工作时间。该程序应循环使用,直到输入5。如果输入了除选择1到5之外的其他内容,程序应该提醒用户选择了什么,然后再循环。使用#defined常数来获取各种收益率和税率。

这是我到目前为止的代码,我缺少什么?;

#include <stdio.h>
#include <stdlib.h>

int main()
{
int choice, hour;
float taxe, total;

printf("****************************************************************\n");
printf("\nEnter the number corresponding to the desired pay rate or action");
printf("\n1)$8.75/hr");
printf("\n2)$9.33/hr");
printf("\n3)$10.00/hr");
printf("\n4)$11.20hr");
printf("\n5)Quit");
printf("**********************************************************\n");

scanf("%d", &choice);

printf("Please enter number of hours: ");
scanf("%d", &hour);

switch(choice){
case 1:
    total = 8.75* hour;
    break;
case 2:
    total = 9.33*hour;
    break;
case 3:
    total = 10.00*hour;
    break;
case 4:
    total = 11.20*hour;
    break;
case 5:
     break;
return 0;
}
}

3 个答案:

答案 0 :(得分:2)

你没有使用循环。直到5进入它才能回收。

选择后,插入一个while循环:

while(choice != 5){

并正常处理你的开关,忽略5值,如果输入另一个值,则添加“默认”情况。

如果您希望整个程序循环使用,请使用do {} while();从你的printf开始,结束开关。

答案 1 :(得分:2)

  

我错过了什么?

你缺少的是实际的迭代。

  

程序应循环使用,直到输入5。

这意味着您必须在循环中完成所有工作,每次检查读取的值是否为5,以便停止迭代。您可以使用以下方法之一:

  1. while循环:

    scanf("%d", &choice);
    while (choice != 5)
    {
        ....
        switch(choice){
            ....
        }
        ....
     scanf("%d", &choice);
    }
    
  2. for循环:

    for (scanf("%d", &choice); choice != 5; scanf("%d", &choice);)
    {
        ....
        switch(choice){
            ....
        }
        ....
    }
    
  3. do-while循环:

    do{
        scanf("%d", &choice);
        ....
        switch(choice){
            ....
        }
        ....
    }while(choice != 5);
    

答案 2 :(得分:1)

你可以在这里使用do while循环,如下所示

 do
 {
    clrscr();
    printf("****************************************************************\n");
    printf("\nEnter the number corresponding to the desired pay rate or action");
    printf("\n1)$8.75/hr");
    printf("\n2)$9.33/hr");
    printf("\n3)$10.00/hr");
    printf("\n4)$11.20hr");
    printf("\n5)Quit");
    printf("**********************************************************\n");

    scanf("%d", &choice);

    printf("Please enter number of hours: ");
    scanf("%d", &hour);

    switch(choice){
    case 1:
        total = 8.75* hour;
        break;
    case 2:
        total = 9.33*hour;
        break;
    case 3:
        total = 10.00*hour;
        break;
    case 4:
        total = 11.20*hour;
        break;
    case 5:
         break;
    default :
        printf("\n please enter proper choice");
        getch();
    }
 }while(choice !=5);
相关问题