使用 while 循环打印两个用户输入值范围内的数字

Print numbers in range of two user-input values with while loop

本文关键字:输入 用户 范围内 数字 两个 循环 while 打印 使用      更新时间:2023-10-16

我觉得我可能在这里错过了一些东西,但有些东西告诉我,我可能只是让这件事变得更加困难,但从"C++入门,第 5 版"一书中,我陷入了这个问题:

练习 1.11.. 编写一个程序,提示用户输入两个整数。 打印这两个整数指定范围内的每个数字。

直到书中的这个时候,唯一使用的循环是while循环,并且没有引入任何条件表达式,如if。如果不是因为用户可以按升序或降序将值放入要求的整数中,那么问题将很简单,因此简单的减法会找到差异,但不测试递增方式。

我怎么能绝对打印数字之间的范围,保证不会在不测试这种数学结果和/或不比较两个数字的情况下向无穷大递增?这就是我所拥有的;当第一个值:v1小于或等于第二个值:v2时,它起作用,但否则不工作:

#include <iostream>
int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): "  
              << endl;  
    std::cin >> v1 >> v2;  
    while (v1 <= v2)  
    {  
        std::cout << v1;  
        ++ v1;  
    }  
    return 0;  
}  

任何帮助将不胜感激!

下面是一个简单的重写,您可以使用minmax来确定要迭代的范围:

#include <iostream>
int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
    std::cin >> v1 >> v2;  
    int current =  std::min(v1, v2);
    int max = std::max(v1, v2);
    while (current <= max)  
    {  
        std::cout << current << std::endl;  
        ++ current;  
    }  
    return 0;  
}

这也允许您保持两个输入"不变",因为您正在使用另一个变量来迭代值。

另请注意,如果将其替换为 current++,则可以在与打印完全相同的行上完成++current。后者将返回当前值 current 然后递增它。

编辑

以下是迈克尔建议我相信的工作代码:

#include <iostream>
int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
    std::cin >> v1 >> v2;  
    int increment = (v2 - v1) / std::abs(v2 - v1);
    int current = v1;
    int max = v2 + increment;
    while (current != max)  
    {  
        std::cout << current << std::endl;  
        current += increment;  
    }  
    return 0;  
}
您可以使用

(v2-v1)/abs(v2-v1)或一些类似的增量。(前提是它们不相等(。对于循环条件,您可以检查当前数字是否仍在两者之间,例如(v1<=v && v<=v2) || (v2<=v && v<=v1)

为了避免零的情况,我会把它变成do while循环并使用v1!=v2 && ...作为条件。

总结一下:

int v = v1;
do {
   std::cout << v << std::endl;
}while( v1!=v2 && ( (v1<=(v+=(v2-v1)/std::abs(v2-v1)) && v<=v2) || (v2<=v && v<=v1) ) );

附言我相信您可以自己解决评论和其他答案中提到的输入问题。

我尝试了业余方式并得到了结果。我希望这会有所帮助。

#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int val;
cout << "Please enter small value "<< endl;
cin >> a;
cout << "Please enter bigger value than the last one " << endl;
cin >> b;
while (val>a)
{
    val = --b;
     cout << "The numbers between a & b are "<< val << endl;
}
return 0;
}

由于我仍然记得C++入门中的这个问题,我认为他们希望您解决该问题的方式是他们在此之前为您提供的编程基础知识。在我看来,如果这是本书中的确切问题,它应该这样解决:

    #include <iostream>
int main()
{
    int v1 = 0, v2 = 0;
    std::cout << "Enter 2 numbers" << std::endl;
    std::cin >> v1 >> v2;
    while (v1 < v2) {
        std::cout << v1 << std::endl;
        ++v1;
    }
}

我还根据我的C++知识(以及两个简单的if陈述(提出了一个解决方案,直到书中的任务。

编写一个程序,提示用户输入两个整数。打印这两个整数指定范围内的每个数字:

#include <iostream>
int main()
{
    std::cout << "Enter two numbers: " << std::endl;
    int n1 = 0, n2 = 0;
    std::cin >> n1 >> n2;
    if (n1 == n2)
    {
        std::cout << "No integers in between the numbers" << std::endl;
    }
    else
    {
        while (n1 != n2)
        {
            if (n1 > n2)
            {
                std::cout << n1 << std::endl;
                n1--;
            }
            else if (n1 < n2)
            {
                std::cout << n1 << std::endl;
                n1++;
            }
        }
        std::cout << n2 << std::endl;
    }
    return 0;
}

和平!

这可能适用于用户可以输入范围的任何值的情况,以及只允许 while 循环和布尔逻辑的情况。引入一个"Done"变量,如果输入第一个 while 循环,则设置为 1,这将跳过第二个 while 循环:

#include <iostream>
int main(){
    int first, second, done = 0;
    std::cout << "Input two numbers: " << std::endl;
    std::cin >> first >> second;
    while (first <= second)
    {
        std::cout << first << std::endl;
        first++;
        done = 1;
    }
    
    while (first >= second && !done)
    {
        std::cout << first << std::endl;
        first--;
    }
    return 0;
}