简体   繁体   English

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

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

I feel like I might be missing something here, but something is telling me that I might just be making this more difficult that it has to be, but from the book, "C++ Primer, 5th ed.," I'm stuck on this problem:我觉得我可能在这里遗漏了一些东西,但有些东西告诉我,我可能只是让这件事变得更加困难,但从“C++ Primer,第 5 版”一书中,我被困在了这一点上问题:

Exercise 1.11: Write a program that prompts the user for two integers.练习 1.11:编写一个程序,提示用户输入两个整数。 Print each number in the range specified by those two integers.打印由这两个整数指定的范围内的每个数字。

Up until this time in the book, the only loop being used is the while loop, and no conditional expressions, like if , have been introduced.直到本书的这个时候,唯一使用的循环是while循环,并且没有引入条件表达式,如if The problem would be simple if it were not for the fact that a user could put the values into the integers asked for in ascending or descending order, so simple subtraction WOULD find the difference, but without testing which way to increment.如果不是因为用户可以按升序或降序将值放入要求的整数中,那么问题将很简单,因此简单的减法可以找到差异,但无需测试以哪种方式递增。

How could I absolutely print the range between the numbers, guaranteeing not to just increment towards infinity without testing the outcome of such math and/or without comparing the two numbers?我怎么能绝对打印数字之间的范围,保证不只是增加到无穷大而不测试这种数学的结果和/或不比较两个数字? This is what I have;这就是我所拥有的; it works when the first value: v1 is less than or equal to the second: v2 , but not otherwise:当第一个值: 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;  
}  

Any help would be greatly appreciated!任何帮助将不胜感激!

You can use (v2-v1)/abs(v2-v1) or some such for increment.您可以使用(v2-v1)/abs(v2-v1)或一些类似的方法进行增量。 (provided they're not equal). (前提是它们不相等)。 And for loop condition you may check if current number is still in between, like (v1<=v && v<=v2) || (v2<=v && v<=v1)对于循环条件,您可以检查当前数字是否仍在中间,例如(v1<=v && v<=v2) || (v2<=v && v<=v1) (v1<=v && v<=v2) || (v2<=v && v<=v1) . (v1<=v && v<=v2) || (v2<=v && v<=v1)

And to avoid the case of zero, I'd turn it into do while loop and use v1!=v2 && ... as a condition.并且为了避免零的情况,我会将它变成do while循环并使用v1!=v2 && ...作为条件。

To sum it all up:总结一下:

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

PS I trust you can resolve the input issue mentioned in comments and in the other answer on your own. PS我相信您可以自己解决评论和其他答案中提到的输入问题。

Here is a simple rewrite where you use min and max to determine the range you want to iterate on:这是一个简单的重写,您可以使用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;  
}

This also allows you to keep the two inputs "intact", because you're using another variable to iterate on the values.这也允许您保持两个输入“完整”,因为您正在使用另一个变量来迭代这些值。

Also note that the ++current could be done on the exact same line it is printed if it was replaced by current++ .另请注意,如果++currentcurrent++替换,则++current可以在它打印的完全相同的行上完成。 The later would return the current value of current and THEN increment it.稍后,将返回的当前值current ,然后加一。

Edit编辑

Here's what Michael suggested I believe, in working code:这是迈克尔建议我相信的,在工作代码中:

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

I tried my amateur way and got the result.我尝试了我的业余方式并得到了结果。 I hope it will be helpful.我希望它会有所帮助。

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

Since I still remember this problem from the C++ Primer, I think the way they wanted you to solve it was with the basics of programming they provided you until then.由于我仍然记得 C++ Primer 中的这个问题,我认为他们希望您解决它的方式是使用他们为您提供的编程基础知识。 In my opinion it should have been solved like this, if this was this exact problem from this book:在我看来,它应该是这样解决的,如果这是本书中的确切问题:

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

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

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