簡體   English   中英

do while 循環的問題

[英]Issues with do while loop

該代碼允許您在星形和三角形電阻網絡轉換之間進行選擇。 還有一個退出選項。 我想驗證 R_a、R_b、R_c 等的值。但是,我在 do while 循環中遇到了一些問題。 下限為 1000,上限為 1000000。

如果輸入在范圍內,我打算讓程序繼續運行,如果不在范圍內,則提示用戶輸入另一個輸入。 但是,截至目前,如果該值在范圍內,程序將繼續,但如果不在范圍內,則在發出警告后也會繼續 - 當我希望它循環回到第一個輸入提示時。

一旦正確,我會將循環添加到所有輸入。 如果有人能夠解決/找到問題,將不勝感激。

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

int main(void)
{
    printf("\n\n\t\tDelta and Star Converter\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Star \n");
        printf("2. Delta\n");
        printf("0. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);



        switch(choice)
        {
        case 1:;

        float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;



        printf("Please enter the value of the Star connected resistors:\n");

          do {
        printf("R_a = ");
         scanf("%f",&R_a);
        if (R_a > 1000000) {
            printf("Please");
        } else if (R_a < 1000) {
            printf("Number to low\n");
        }else {
        }


     }while(R_a = -0);





        printf("R_b = ");
        scanf("%f",&R_b);
        printf("R_c = ");
        scanf("%f",&R_c);


        R_ab=R_a+R_b+(R_a*R_b)/R_c;
        R_bc=R_b+R_c+(R_b*R_c)/R_a;
        R_ac=R_a+R_c+(R_a*R_c)/R_b;

        printf("the equivalent Delta values are: \n");
        printf("R_ab = %.2f Ohms\n",R_ab);
        printf("R_bc = %.2f Ohms\n",R_bc);
        printf("R_ac = %.2f Ohms\n",R_ac);
        break;

        case 2:;



        printf("Please enter the values of the Delta connected resistors:\n");

        printf("R_ab = ");
        scanf("%f",&R_ab);
        printf("R_bc = ");
        scanf("%f",&R_bc);
        printf("R_ac = ");
        scanf("%f",&R_ac);



        R_a = (R_ab*R_ac)/(R_ab + R_bc + R_ac);
        R_b = (R_ab*R_bc)/(R_ab + R_bc + R_ac);
        R_c = (R_ac*R_bc)/(R_ab + R_bc + R_ac);

        printf("the equivalent Star values are: \n");
        printf("R_a = %.2f Ohms\n",R_a);
        printf("R_b = %.2f Ohms\n",R_b);
        printf("R_c = %.2f Ohms\n",R_c);
        break;

            case 0:
                printf("\n\nAdios!!\n\n\n");
                exit(0);    // terminates the complete program execution
        }
while (0) ;  }
    printf("\n\n\t\t\tThank you!\n\n\n");
    return 0;
}
while(R_a = -0)

=賦值運算符 這將-0分配給R_a ,並評估為相同的虛假值,結束循環。

do... while改為無限循環,當值在范圍內時break循環。

while (1) {
    printf("R_a = ");

    if (1 != scanf("%f", &R_a))
        exit(EXIT_FAILURE);

    if (R_a > 1000000) {
        fprintf(stderr, "Number too high.\n");
    } else if (R_a < 1000) {
        fprintf(stderr, "Number to low.\n");
    } else {
        break;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM