【问题标题】:C Looping a switch?C循环开关?
【发布时间】:2015-01-28 09:23:58
【问题描述】:

这是我的代码:

我的问题是我想用一个 while 循环来循环 switch 语句,这取决于 char 响应得到什么(在底部)。

我尝试将整个 switch 语句放在一个 do while 循环中(失败了)。

无论如何,我是这门语言的新手,我想尝试制作一个复杂的程序。

#include <stdio.h>
#include <stdlib.h>
#define LINE  "____________________"
#define TITLE "Tempature Converter"
#define NAME  "Jose Meza II"

char scale, response;
float temp, celTemp, farTemp, kelTemp;

int main() {

printf("\n \t %s \n \t %s \n", LINE, TITLE );
printf("\t by %s \n \t %s \n\n", NAME, LINE );

printf("Enter C for Celsius, F for Fahrenheit, or K for Kelvin: ");
scanf("%c", &scale);
printf("Tempature value: ");
scanf("%f", &temp);

switch (scale)
{
    case ('c'): /* convert to Fahrenheit and Kelvin */
    {
        farTemp = (temp * 9 / 5) + 32;
        kelTemp = temp + 273.15;
        printf("Fahrenheit = %.2f\n", farTemp);
        printf("Kelvin = %.2f\n\n", kelTemp);
        break;
    } /* end case 'c' */
    case ('f'): /* convert to Celsius and Kelvin */
    {
        celTemp = (temp - 32) * 5 / 9;
        kelTemp = (temp - 32) * 5 / 9 + 273.15;
        printf ("Celsius = %.2f\n", celTemp);
        printf ("Kelvin = %.2f\n\n", kelTemp);
        break;
    } /* end case 'f' */
    case ('k'): /* convert to Celsius and Fahrenheit */
    {
        celTemp = temp - 273.15;
        farTemp = (temp - 273.15) * 9 /5 + 32;
        printf("Celsius = %.2f\n", celTemp);
        printf("Fahrenheit = %.2f\n\n", farTemp);
        break;
    } /* end case 'k' */
    default: exit(0); /* no valid temperature scale was given, exit program */
} /* end switch */

printf("Enter in C to Continue, or S to stop: ");
scanf(" %c", &response);

return 0;
}

我能做什么?

我试过了:

do
{
    printf("Enter C for Celsius, F for Fahrenheit, or K for Kelvin: ");
    scanf("%c", &scale);
    printf("Tempature value: ");
    scanf("%f", &temp);

    switch (scale)
    {
        case ('c'): /* convert to Fahrenheit and Kelvin */
        {
            farTemp = (temp * 9 / 5) + 32;
            kelTemp = temp + 273.15;
            printf("Fahrenheit = %.2f\n", farTemp);
            printf("Kelvin = %.2f\n\n", kelTemp);
            break;
        } /* end case 'c' */
        case ('f'): /* convert to Celsius and Kelvin */
        {
            celTemp = (temp - 32) * 5 / 9;
            kelTemp = (temp - 32) * 5 / 9 + 273.15;
            printf ("Celsius = %.2f\n", celTemp);
            printf ("Kelvin = %.2f\n\n", kelTemp);
            break;
        } /* end case 'f' */
        case ('k'): /* convert to Celsius and Fahrenheit */
        {
            celTemp = temp - 273.15;
            farTemp = (temp - 273.15) * 9 /5 + 32;
            printf("Celsius = %.2f\n", celTemp);
            printf("Fahrenheit = %.2f\n\n", farTemp);
            break;
        } /* end case 'k' */
        default: exit(0); /* no valid temperature scale was given, exit     program */
    } /* end switch */

    printf("Enter in C to Continue, or S to stop: ");
    scanf(" %c", &response);
}
while(response == 'c');

【问题讨论】:

  • 一个建议:尽量让程序更简单。不是更复杂。
  • 这是你的作业吗?您究竟尝试过什么?向我展示你尝试过的 do-while 循环,我们可以看到你可能做错了什么。
  • 这有什么好玩的(我 14 岁)
  • 所以没有。不是我的作业

标签: c loops switch-statement


【解决方案1】:

最常用的方法是使用do-while 循环。进行以下更改

1. 将`response`初始化为`C`。 2. 将 `switch` case 和 next `printf()` 和 `scanf()` 放入 `do..while()` 循环中。 3. 在`while()`条件下,勾选`while(c == `C`)`。

编辑后,问题似乎是

scanf("%c", &scale);

%c之前需要有一个空格,比如

scanf(" %c", &scale);

避免ENTER [\n]在C最后。

【讨论】:

  • 像 C 一样,非常有趣。
  • @el_coder 与scanf(" %c", &amp;response);的原因相同
猜你喜欢
  • 2020-07-06
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2016-08-03
  • 2022-11-25
相关资源
最近更新 更多