C++ x应该是=28.24778761,但我得到28
我刚开始学c++ 在这个程序中,我尝试根据两个电阻的值对两个电阻进行分类 但最后我无法正确打印出x,只能打印出之前的部分, 例如,我试过r1=0 r2=50 r3=100 r4=60 V=9 n=4第一个值=55第二个=56第三个=52第四个=57 x应该是56*57/(56+57)=28.24778761,但我只得到28为什么C++ x应该是=28.24778761,但我得到28,c++,C++,我刚开始学c++ 在这个程序中,我尝试根据两个电阻的值对两个电阻进行分类 但最后我无法正确打印出x,只能打印出之前的部分, 例如,我试过r1=0 r2=50 r3=100 r4=60 V=9 n=4第一个值=55第二个=56第三个=52第四个=57 x应该是56*57/(56+57)=28.24778761,但我只得到28为什么 #include <iostream> using namespace std; int main() { int n; float V,
#include <iostream>
using namespace std;
int main()
{
int n;
float V, TOTAL1, TOTAL2, y;
int r1, r2, r3, r4, i;
cout << "Give r1: "; // Ask for resistors.
cin >> r1;
cout << "Give r2: ";
cin >> r2;
cout << "Give r3: ";
cin >> r3;
cout << "Give r4: ";
cin >> r4;
cout << "Give voltage: ";
cin >> V;
cout << "Give number of resistors: ";
cin >> n;
int a, b; // Ccount the number on its category.
int m;
m = 0;
a = 0;
b = 0;
y = 0;
TOTAL2 = 0;
for(i = 1; i < n + 1; i++)
{
y = TOTAL2 + y; // Calculate the as they are in series
float value;
m = m + 1;
cout << "\n Give resistance: ";
cin >> value;
if((value >= r1) && (value >= r2) && (value <= r3) && (value <= r4))
{
if(m % 2>0)
{
cout << "It belongs to the first";
a = a + 1;
TOTAL1 = value + TOTAL1; // If they are in the first category they are connected in series
}
else
{
cout << "It belongs to the second";
b = b + 1;
TOTAL2 = 1 / value;
}
}
else if((value >= r1) && (value <= r2)){
cout << "It belongs to the first";
a = a + 1;
TOTAL1 = value + TOTAL1;
}
else if((value >= r3) && (value <= r4)) {
cout << "It belongs to the second";
b = b + 1;
TOTAL2 = 1 / value + 1 / TOTAL2;
}
}
long double x;
x = 1 / y;
cout << "\n The first category has: " << a;
cout << "\n The second category has: " << b;
cout << "\n The total resistance of the first category is: " << TOTAL1;
cout << "\n The total resistance of the second category is: " << x;
return 0;
}
#包括
使用名称空间std;
int main()
{
int n;
浮点数V,总计1,总计2,y;
int r1,r2,r3,r4,i;
cout>r1;
cout>r2;
cout>r3;
cout>r4;
cout>V;
cout>n;
int a,b;//计算其类别上的数字。
int m;
m=0;
a=0;
b=0;
y=0;
TOTAL2=0;
对于(i=1;i值;
如果((值>=r1)&&(值>=r2)&&(值变量y
仅在循环顶部更新,则上次更新将丢失。因此y
包含1/28(第一个值),当取其倒数时,正好得到28。如果您重构代码,使其具有您提供的输入,这样,如果有人想要测试答案,他们只需要运行它,就可以更容易地为您提供帮助。