如果声明不正常?

时间:2014-02-02 21:11:57

标签: c

当我运行程序时,如果我为“d”选项输入0,它不会打印我在输入0时设置要打印的if语句,因为你几乎可以在程序结束时看到。这个程序中的注释是程序的一部分,我就是这样做的,所以你可以看到不是注释形式的代码是我的问题。

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

main()
{
    // David Brand
    int result, num, num1, calc, calc1, mark;
    char A1, A2, ch;
    float num2, num3, result1;

    printf("\n\n\tMenu");
    printf("\n\n\ta. Addition");
    printf("\n\tb. Subtraction");
    printf("\n\tc. Multiplication");
    printf("\n\td. Division");
    printf("\n\n\tSelect a, b, c or d: ");
    ch = getch();
    if (ch == 'a')
    {
        printf("\n\n\tEnter a number ");
        scanf("%d", &num);
        printf("\n\n\tEnter another number ");
        scanf("%d", &num1);
        printf("\n\n\tPlease add the two numbers ");
        scanf("%d", &result);
        if (result == num + num1)
            printf("\n\n\tCorrect");
        if (result != num + num1)
            printf("\n\n\tWrong");
    }
    if (ch == 'b')
    {
        printf("\n\n\tEnter a number ");
        scanf("%d", &num);
        printf("\n\n\tEnter another number ");
        scanf("%d", &num1);
        printf("\n\n\tPlease subtract from the first number ");
        scanf("%d", &result);
        if (result == num - num1)
            printf("\n\n\tCorrect");
        if (result != num - num1)
            printf("\n\n\tWrong");
    }
    if (ch == 'c')
    {
        printf("\n\n\tEnter a number ");
        scanf("%d", &num);
        printf("\n\n\tEnter another number ");
        scanf("%d", &num1);
        printf("\n\n\tPlease multiply the two ");
        scanf("%d", &result);
        if (result == num * num1)
            printf("\n\n\tCorrect");
        if (result != num * num1)
            printf("\n\n\tWrong");
    }
    if (ch == 'd')
    {
        printf("\n\n\tEnter a number ");
        scanf("%f", &num2);
        if (num2 != 0)
            printf("\n\n\tEnter another number ");
        scanf("%f", &num3);
        printf("\n\n\tPlease divide the two numbers ");
        scanf("%f", &result1);
        if (num2 == 0)
            printf("\n\n\tZero divisor");
        printf("\n\tHit a key to end the program");
        getch();
        system("cls");
        exit(0);
    }
    if (result1 == num2 / num3)
        printf("\n\n\tCorrect");

    if (result1 != num2 / num3)
        printf("\n\n\tWrong");

    getch();
    system("cls");
}

3 个答案:

答案 0 :(得分:0)

以下是运行自动布局后的代码部分:

if(ch=='d')
{
    printf("\n\n\tEnter a number ");
    scanf("%f", &num2);
    if(num2!=0)
        printf("\n\n\tEnter another number ");
    scanf("%f", &num3);
    printf("\n\n\tPlease divide the two numbers ");
    scanf("%f", &result1);
    if(num2==0)
        printf("\n\n\tZero divisor");
    printf("\n\tHit a key to end the program");
    getch();
    system("cls");
    exit(0);
}

输入0后,它不会打印任何内容,因为它会直接转到另一个scanf。如果您希望使用if有条件地执行多个语句,则需要将这些语句括在大括号中。

我希望你想要这样的东西:

if(ch=='d')
{
    printf("\n\n\tEnter a number ");
    scanf("%f", &num2);

    if(num2!=0) {
        printf("\n\n\tEnter another number ");
        scanf("%f", &num3);

        printf("\n\n\tPlease divide the two numbers ");
        scanf("%f", &result1);
    } else {
        printf("\n\n\tZero divisor");
        printf("\n\tHit a key to end the program");
        getch();
        system("cls");
        exit(0);
    }
}

答案 1 :(得分:0)

将您的代码更改为

if(ch=='d')
{
   printf("\n\n\tEnter a number ");
   scanf("%f", &num2);
   if(num2==0)
   {
      printf("\n\n\tZero divisor");
      printf("\n\tHit a key to end the program");
      getch();
      system("cls");
      exit(0);    
   }
   else if(num2!=0)
   {
      printf("\n\n\tEnter another number ");
      scanf("%f", &num3);
      printf("\n\n\tPlease divide the two numbers ");
      scanf("%f", &result1);
   }
}

答案 2 :(得分:0)

我没有工作中的c编译器,但我认为这应该解决你遇到的大多数问题

你也不需要检查num3是0而不是num2吗? 0/5很好5/0不是

你不需要检查==然后立即检查!=你知道==与使用else的相反的是!=

您应该能够将两个数字甚至答案输入到一行中,以便用户使用单个scanf线更容易,也可以将其中的一部分用于函数,因为有很多重复的代码

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

main()
{
    //David Brand
    int result,num,num1,calc,calc1,mark;
    char A1,A2,ch;
    float num2,num3,result1;

    printf("\n\n\tMenu");
    printf("\n\n\ta. Addition");
    printf("\n\tb. Subtraction");
    printf("\n\tc. Multiplication");
    printf("\n\td. Division");
    printf("\n\n\tSelect a, b, c or d: ");
    ch=getch();

    if(ch=='a')
    {
        printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
        scanf("%d %d", &num, &num1);

        printf("\n\n\tPlease add the two numbers ");
        scanf("%d", &result);

        if (result == num + num1)
            printf("\n\n\tCorrect");
        else
            printf("\n\n\tWrong");
    }
    else if(ch=='b')
    {
        printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
        scanf("%d %d", &num, &num1);

        printf("\n\n\tPlease subtract from the first number ");
        scanf("%d", &result);

        if(result == num-num1)
            printf("\n\n\tCorrect");
        else
            printf("\n\n\tWrong");
    }
    else if(ch=='c')
    {
        printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
        scanf("%d %d", &num, &num1);

        printf("\n\n\tPlease multiply the two ");
        scanf("%d", &result);

        if(result == num * num1)
            printf("\n\n\tCorrect");
        else
            printf("\n\n\tWrong");
    }
    else if(ch=='d')
    {
        printf("\n\n\tEnter two numbers separated by a space i.e. 5.7 7.5");
        scanf("%f %f", &num2, &num3);

        if(num3 == 0)
        {
            printf("\n\n\tZero divisor");
        }
        else
        {
            printf("\n\n\tPlease divide the two numbers ");
            scanf("%f", &result1);

            if(result1 == (num2 / num3))
                printf("\n\n\tCorrect");
            else
                printf("\n\n\tWrong");
        }           
    }   
    printf("\n\tHit a key to end the program");
    getch();
    exit(0);
}