简体   繁体   English

C跳过“while”循环?

[英]C skip a “while” loop?

I have a problem, I tried to write a program to show the whole sum from 1 to 22 and after that, to do 2 while loops. 我有一个问题,我试着写一个程序来显示从1到22的整个总和,然后,做2个while循环。 The first one is supposed to perform the sum of some numbers given by the user, as an example: you type 10, 30 and 40 then as you enter a 0 the program sums the first three numbers. 第一个应该执行用户给出的某些数字的总和,例如:输入10,30和40然后当输入0时,程序将前三个数字相加。 Unfortunetly the first while loop is not working. 不幸的是,第一个while循环不起作用。 It goes directly to the last while loop where it is supposed to type a decimal numbers like (10.20 30.50 40.55) and after you type 0 again it sum those numbers and add and multipli every entry with 1.19. 它直接进入最后一个while循环,它应该键入一个十进制数字,如(10.20 30.50 40.55),再次输入0后,它将这些数字相加,并将每个条目加上和乘以1.19。 So far the last loop is working properly, unfortunately the second loop does not, if I move printf and scanf over the while it let me write but just start writing w/o stopping the number I wrote . 到目前为止,最后一个循环工作正常,不幸的是第二个循环没有,如果我移动printf和scanf,它让我写,但只是开始写,没有停止我写的数字。 Thank You in advance! 先感谢您!

Here is the code : 这是代码:

#include <stdio.h>

int main() 
{
    int sum = 0;                         
    int a;
    int b;
    double i;
    double sum1 = 0;
    for (a= 0; a <= 22; a++) {

        sum = sum + a; 
        printf("the sum from 1 till 22 : %i\n ", sum);
    }

    while (b != 0) {
        printf("type a number:");
        scanf("%i", &b);
        sum += b;
        printf("%i\n", b);

    }
    printf("the sum is : %i\n", sum);

    while(i !=0) {
        printf ("Type a decimal number:");
        scanf ("%lf",&i);                       
        sum1 += i*1.19;


        printf("%lf\n", i);

    }

    printf("The decimal summ is: %lf\n",sum1);
    return 0;
}

You don't initialise i to any value before entering the loop with 在进入循环之前,不要将i初始化为任何值

while(i != 0)

i might very well be zero at this point, so your loop won't be entered even once. i可能很好,所以你的循环甚至不会输入一次。 Initialising i to a non-zero value should fix this particular problem. i初始化为非零值应解决此特定问题。 The same holds for the variable b . 变量b

You should turn on warnings in your compiler, so it can show you problems like this one. 您应该在编译器中打开警告,这样就可以向您显示这样的问题。

The first time the condition of the second while is evaluated, b has undefined value, since it wasn't initialized. 第一次评估第二次while的条件时, b具有未定义的值,因为它未初始化。 The same applies to the third while. 这同样适用于第三种情况。

Whether or not both loops are executed is only a question of chance. 两个循环是否都被执行只是一个偶然的问题。

Initialize both variables with non-zero values to ensure both whiles are entering. 使用非零值初始化两个变量以确保两个输入都进入。 Or use a do-while: 或者使用do-while:

do {

    printf("type a number:");
    scanf("%i", &b);
    sum += b;
    printf("%i\n", b);

} while (b != 0);

Don't test b with while , test it after the user enters the number. 不要用while测试bwhile用户输入数字后进行测试。 Then you can use break to exit the loop. 然后你可以使用break来退出循环。

while (1) {
    printf("type a number:");
    scanf("%i", &b);
    if (b == 0) {
        break;
    }
    sum += b;
    printf("%i\n", b);
}

while(1) {
    printf ("Type a decimal number:");
    scanf ("%lf",&i); 
    if (i == 0.0) {
        break;
    }                      
    sum1 += i*1.19;
    printf("%lf\n", i);
}

Your only issues are initialization: see edits in the code below. 您唯一的问题是初始化:请参阅下面的代码中的编辑。 (it compiles and runs) (它编译并运行)
Did you get any compiler warnings for these? 你有没有得到任何编译器警告? If not, you should change your settings so you do. 如果没有,您应该更改您的设置。

#include <stdio.h>

int main() 
{
   int sum = 0;                         
   int a;
   int b=-1;  //initialize (any non-zero value will work)
   double i;
   double sum1 = 0;
    for (a= 0; a <= 22; a++) {//a initialized in for(...) statement, (this is good)

    sum = sum + a; 
    printf("the sum from 1 till 22 : %i\n ", sum);
        }

    while (b != 0) { //b Needs to be initialized before using (done above)
        printf("type a number:");
        scanf("%i", &b);
        sum += b;
        printf("%i\n", b);

    }
    printf("the sum is : %i\n", sum);
    i=-1;                          //initialize i to any non-zero value
    while(i !=0) {  
     printf ("Type a decimal number:");
     scanf ("%lf",&i);                      
     sum1 += i*1.19;


      printf("%lf\n", i);

    }
     printf("The decimal summ is: %lf\n",sum1);

     getchar();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM