用循环和条件语句来计算一个值 [英] For loop and conditional statement to calculate a value

查看:166
本文介绍了用循环和条件语句来计算一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使用下面设置的条件计算Interest变量的值,但遇到麻烦,因为它返回0,或者如果我重新排列for循环,它将返回6471为每个帐户。如果你能帮助我修正for循环,以便在控制台中显示正确的兴趣,那将是非常棒的。

  #include<的iostream> 

使用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;

//在这里添加你的代码



cout<< 帐号\ t<< Balance \t\t<< Days \t<< Interest''t<< ENDL;
for(int i = 0; i <8; i ++)

cout< AccountNumber [i]<< \t\t<<余额[i] \t\t<< DaysSinceDebited [i]<< \t<<兴趣< \t<< ENDL;
$ b $ for(int i = 0; i <8; i ++)
if(Balance [i]> 10000 || DaysSinceDebited [i]> 30)
interest =(余额[i] * 0.06);
else
interest =(Balance [i] * 0.03);



系统(暂停);
返回0;




$ b $ p
$ b

这是我的修改:

  #include< iostream> 

使用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;

//在这里添加你的代码


(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<< 帐号\ t<< Balance \t\t<< Days \t<< Interest''t<< ENDL;
for(int i = 0; i <8; i ++)

cout< AccountNumber [i]<< \t\t<<余额[i] \t\t<< DaysSinceDebited [i]<< \t<<兴趣< \t<< ENDL;




$ b系统(暂停);
返回0;

$ b


解决方案

{
if(Balance [i]> 10000 || DaysSinceDebited [i]> 30)
interest =(余额[i] * 0.06);
else
interest =(Balance [i] * 0.03);
cout<< AccountNumber [i]<< \t\t<<余额[i] \t\t<< DaysSinceDebited [i]<< \t<<兴趣< \t<< ENDL;

$ / code $ / pre
$ b $ p $输出:

 帐户余额天数利息
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


Trying to Calculate a value for the Interest variable using the conditions i set below, but having trouble as it is returning 0, or if i rearrange the for loop it returns 6471 for each account. Would be great if you can help me fix the for loop so that the correct interest is shown in the console

#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;

}

here is my modification:

#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;

}

解决方案

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;
}

Output:

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

这篇关于用循环和条件语句来计算一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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