简体   繁体   English

如何根据它们的值将两个双精度数分配给不同的变量?

[英]How do I assign two doubles to different variables depending on their value?

I am trying to compare the value of two double inputs and assign them as different variables depending on which one is greater.我正在尝试比较两个双输入的值,并将它们分配为不同的变量,具体取决于哪个更大。 I have this code so far but it doesn't seem to be working.到目前为止我有这个代码,但它似乎没有工作。 What should I change?我应该改变什么?

double a, b;
double q, w;

cout << "Enter the first value of the first range: " << endl;
cin >> a;
cout << "Enter the second value of the first range: " << endl;
cin >> b;


if (a < b) {
    a = q, b = w;
}
else {
    b = q, a = w;
}

I think what you meant to do is:我认为你的意思是:

if (a < b)
{
    q = a;
    w = b;
}
else
{
    q = b;
    w = a;
}

By the way, a more compact way to achieve the same result would be:顺便说一句,实现相同结果的更紧凑的方法是:

q = std::min(a, b);
w = std::max(a, b);

Try this, you have to change the lines on the assignment like that and give at q and wa value before use them.试试这个,你必须像这样更改作业中的行,并在使用之前给出 q 和 wa 值。

double a, b;
double q=0, w=0;

cout << "Enter the first value of the first range: " << endl;
cin >> a;
cout << "Enter the second value of the first range: " << endl;
cin >> b;


if (a < b) {
    a = q;
    b = w;
}
else {
    b = q
    a = w;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM