在C中的循环/ IF结构外部声明变量 [英] Declaring variables outside loop/IF structures in C

查看:104
本文介绍了在C中的循环/ IF结构外部声明变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C语言的新手,而不是整体编程。我想知道为什么当我声明要在结构的if语句中使用的变量超出结构时,我收到的输出是不正确的(无论如何对于这段代码)。

I'm new to the C language, rather programming overall. I was wondering why is it that when I declare a variable to be used within an if statement OUTSIDE the structure, that the output I've received is incorrect (for this piece of code anyway).

这是我的代码:

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
int rate = 10, hours;
double tax, grosspay, netpay;

printf("Enter work hours this week: ");
scanf("%d", &hours);

grosspay = hours * rate;

if (grosspay <= 300 && grosspay > 0)
{
    tax = 0.10;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 300 && grosspay <=1000)
{
    tax = 0.15;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 1000)
{
    tax = 0.25;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else 
{
    printf("Invalid input. Please try again.\n\n");
}
}

编辑:我放置的代码是我的修复程序无法获得正确的输出。我希望当我在整个IF语句外声明一次netpay变量时,我会收到正确的输出,与上面的代码输出相同。

The code I placed was my 'fix' for not getting the correct output. I expected that when I declared the netpay variable once outside the entire IF statement that I would receive the correct output, the same output from the code above.

编辑2:上一个版本

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
    int rate = 10, hours;
    double tax, grosspay, netpay;

    printf("Enter work hours this week: ");
    scanf("%d", &hours);

    grosspay = hours * rate;
    netpay = grosspay - grosspay * tax;

    if (grosspay <= 300 && grosspay > 0)
    {
        tax = 0.10;

        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else if (grosspay > 300 && grosspay <=1000)
    {
        tax = 0.15;
        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else if (grosspay > 1000)
    {
        tax = 0.25;
        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else 
    {
        printf("Invalid input. Please try again.\n\n");
    }
}


推荐答案

也许您正在尝试消除重复的代码。由于 if 块之间的差是税率,因此您可以设置税率,最后进行一次计算。

Maybe you are trying to eliminate the duplicate code. Since the difference between the if blocks is the tax rate, you could set the rate and at the end make one calculation.

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
    int rate = 10, hours;
    double tax, grosspay, netpay;

    printf("Enter work hours this week: ");
    scanf("%d", &hours);

    grosspay = hours * rate;

    if (grosspay <= 300 && grosspay > 0)
    {
        tax = 0.10;
    }
    else if (grosspay > 300 && grosspay <=1000)
    {
        tax = 0.15;
    }
    else if (grosspay > 1000)
    {
        tax = 0.25;
    }
    else
    {
        printf("Invalid input. Please try again.\n\n");
        return;
    }
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}

这篇关于在C中的循环/ IF结构外部声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆