首页 > 解决方案 > 使用“while”为多个字符获取一个循环

问题描述

我是 Stackoverflow 的新手,我对编码非常非常陌生。只是弄乱C。这就是我在这里要做的(不要把这个程序从科学上看是准确的),这是一个计算狭义相对论长度、质量和时间方程的程序。我实际上有3个问题:

当我尝试在 y/n 问题中输入其他字符时,一切正常,但例如,如果我输入“sfkl”,警告会出现 4 次,因为我输入了 4 个字符。如果我输入空格,它甚至不会发出警告,直到我输入另一个字符然后输入。无论我在一行中输入多少个字符(包括空格),我都可以让它发出 1 个警告吗?

我的另一个问题是,我有点阻止输入除 y/n 以外的任何内容,但对于双值输入(质量、长度和时间),我无法找出类似的系统(一遍又一遍地要求双值) . 你能建议我一个解决方案吗?

而我的第三个问题是,在做"scanf_s("%c",&answer);"的时候,如果我不在"%c"前面加一个空格,就不能正常工作。它注册一个输入并要求我只输入 y/n。为什么在此之前需要一个空间?

这是代码:

#include <stdio.h>
#include <math.h>
#define LIGHT 299792458

int input();

int main()
{
    printf("\n\n\tThis program calculates how length, mass and time changes with respect to your speed.\n\n\tThe values you enter are the quantites which are observed by a stationary observer and the output values are the quantites observed by the person in a vehicle which is moving at the speed that you enter.");

    input();

    return 0;
}

int input()
    {
        double length, mass, utime, speed;
        char answer;

        do
        {
            printf("\n\n     **************************************************");

            printf("\n\n\tPlease enter a quantity of length: ");
            scanf_s("%lf", &length);

            printf("\n\tPlease enter a quantity of mass: ");
            scanf_s("%lf", &mass);

            printf("\n\tPlease enter a quantity of time: ");
            scanf_s("%lf", &utime);

            printf("\n\tNow enter the speed of the vehicle (m/s): ");
            scanf_s("%lf", &speed);

            while (speed > LIGHT)
            {
                printf("\n\n\tNothing can surpass the speed of light in the universe. Enter a smaller value: ");
                scanf_s("%lf", &speed);
            }

            double newlength = length * (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));
            double newmass = mass / (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));
            double newutime = utime / (sqrt(1 - pow(speed, 2) / pow(LIGHT, 2)));

            if (speed == LIGHT)
            {               
                printf("\n\n     **************************************************");

                printf("\n\n\n\tIt's technically impossible to reach the speed of light if you have mass but here are the mathematical limit results:\n\n\t*The new length quantity is 0\n\n\t*The new mass quantity is infinity\n\n\t*The new time quantity is infinity\n\n\n\t- Time successfully dilated -\n\n");

                printf("\n\tDo you want to start over? (y/n): ");
                scanf_s(" %c", &answer);

                if (answer == 'n')
                {
                    return 0;
                }
                else if (answer == 'y')
                {
                    continue;
                }
                else
                {
                    while (answer != 'y' && answer != 'n')
                    {
                        printf("\n\tPlease only enter 'y' or 'n': ");
                        scanf_s(" %c", &answer);
                    }
                }
            }


            if (speed < LIGHT)
            {
                printf("\n\n     **************************************************");

                printf("\n\n\n\t*The new length quantity is %.20lf\n\n\t*The new mass quantity is %.20lf\n\n\t*The new time quantity is %.20lf\n\n\n\t- Time successfully dilated -\n\n", newlength, newmass, newutime);

                printf("\n\tDo you want to start over? (y/n): ");
                scanf_s(" %c", &answer);

                if (answer == 'n')
                {
                    return 0;
                }
                else if (answer == 'y')
                {
                    continue;
                }
                else
                {
                    while (answer != 'y' && answer != 'n')
                    {
                        printf("\n\tPlease only enter 'y' or 'n': ");
                        scanf_s(" %c", &answer);
                    }
                }
            }
        }
        while (answer == 'y');

        return 0;
    }

谢谢你,祝你有美好的一天

标签: cwhile-loop

解决方案


的返回值为scanf解析成功的元素个数;您可以使用它来重复,直到成功读取某些内容:

double nr=0;
while (!feof(stdin) && scanf("%lf",&nr)!=1) {
  printf("not a number; try again.");
  while ( (c = getchar()) != '\n' && c != EOF ) { }
}

请注意,您必须从缓冲区中取出“无效”输入;否则,scanf会一次又一次地失败。


推荐阅读