用于循环和条件语句来计算值

时间:2016-02-07 02:24:25

标签: c++ loops if-statement for-loop

尝试使用我在下面设置的条件计算Interest变量的值,但在返回0时遇到麻烦,或者如果我重新排列for循环,则每个帐户返回6471。如果你可以帮助我修复for循环以便在控制台中显示正确的兴趣

,那将会很棒
#include <iostream>

using namespace std;



int main()

{

int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };

float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };

int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
int interest = 0;

//add your code here



cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" << endl;
for (int i = 0; i < 8; i++)

    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;

for (int i = 0; i < 8; i++)
    if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
        interest = (Balance[i] * 0.06);
    else
        interest = (Balance[i] * 0.03);



system("pause");
return 0;

}

这是我的修改:

#include <iostream>

using namespace std;



int main()

{

int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };

float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };

int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
int interest = 0;

//add your code here


for (int i = 0; i < 8; i++)
    if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
        interest = (Balance[i] * 0.06);
    else
        interest = (Balance[i] * 0.03);

cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" << endl;
for (int i = 0; i < 8; i++)

    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;





system("pause");
return 0;

}

3 个答案:

答案 0 :(得分:1)

int interest = 0;

您的代码首先将interest变量设置为0。

for (int i = 0; i < 8; i++)
    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;

接下来发生的事情是,您的代码会为每个帐户打印interest变量的值。由于interest设置为0,因此您可以看到打印的值为0。

您的代码中发生的下一件事是第二个循环,在循环内部,您为每个帐户计算并设置interest。哪个好,除了你已经显示interest的值。

您需要在计算之后显示interest ,而不是之前。

答案 1 :(得分:0)

for (int i = 0; i < 8; i++)
{
    if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
        interest = (Balance[i] * 0.06);
    else
        interest = (Balance[i] * 0.03);
    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
}

输出:

Account Number  Balance         Days    Interest
1001            4254.4          20      127
7940            27006.2         35      1620
4382            123.5           2       3
2651            85326.9         14      5119
3020            657             5       19
7168            7423.34         360     445
6245            4.99            1       0
9342            107864          45      6471

答案 2 :(得分:0)

你应该移动

cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;

到第二个for循环。它会打印相同的结果6471,因为您计算所有值并重新设置为一个名为 interest 的唯一变量,但是您不会在每次计算迭代中显示它,那么6471是最后的结果由最后一个元素的条件赋予数据向量。然后,您应该在 for loop 的每次迭代中显示兴趣,就像:

    for (int i = 0; i < 8; i++){
        if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
            interest = (Balance[i] * 0.06);
        else
            interest = (Balance[i] * 0.03);
cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
}

此外,兴趣必须浮动。我希望它可以帮到你:D