我有一个C++实验室,问题是:用户应该为X输入一个值(X是所持有的测试数)。如果x<15,程序不计算任何东西。如果X在16和30之间,程序应计算C=1.0/10.0*(24a);如果X>30,程序应计算C=0.15(24*a)。我的multiple if代码可以工作,但是当我输入X的值时,方程没有解出。有人知道吗??
#include<iostream>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
int main()
{
//variables defined here
float A, X, C, F;
//A stands for number of classes scheduled
//X is for number of tests and C is the modification
cout << "This program predicts the number of cars parked at HVCC at any given hour \n";
cout << "enter a value for A \n";
cin >> A;
cout << "enter a value for number of tests X \n";
cin >> X;
if (X < 15)
{
cout << "No modificatons needed for under 15 tests \n";
}
else if (X > 15 && X < 20)
{
cout << "Approximation for between 15-30 tests \n";
C = 1.0 / 10.0 * (24 * A);
cin >> C;
}
else
{
cout << "Approximation for more than 30 tests \n";
C = 0.15 * (24 * A);
cin >> C;
}
}
使用CIN
读取用户输入。使用cout
打印结果:
#include<iostream>
#include<cmath>
#define _USE_MATH_DEFINES
using namespace std;
int main()
{
//variables defined here
float A, X, C, F;
//A stands for number of classes scheduled
//X is for number of tests and C is the modification
cout << "This program predicts the number of cars parked at HVCC at any given hour \n";
cout << "enter a value for A \n";
cin >> A;
cout << "enter a value for number of tests X \n";
cin >> X;
if (X < 15)
{
cout << "No modificatons needed for under 15 tests \n";
}
else if (X > 15 && X < 20)
{
cout << "Approximation for between 15-30 tests \n";
C = 1.0 / 10.0 * (24 * A);
cout << C; // replace cin >> with cout <<
}
else
{
cout << "Approximation for more than 30 tests \n";
C = 0.15 * (24 * A);
cout << C; // replace cin >> with cout <<
}
}