Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
练习1.11:编写一个程序,提示用户输入两个整数。打印这两个整数指定范围内的每个数字 大学本科第一年有C++问题。我在用入门书,这是我的答案。这是一个while语句练习。只有当x1>x2时,代码才能正常工作。 所以,当x2>x1时,它实际上不起作用,它应该起作用吗?这是我编码的第二天,有点混乱。如果这是编程中的一件事,那么书的后面就没有答案了。答案应该是什么样的? */_C++ - Fatal编程技术网

练习1.11:编写一个程序,提示用户输入两个整数。打印这两个整数指定范围内的每个数字 大学本科第一年有C++问题。我在用入门书,这是我的答案。这是一个while语句练习。只有当x1>x2时,代码才能正常工作。 所以,当x2>x1时,它实际上不起作用,它应该起作用吗?这是我编码的第二天,有点混乱。如果这是编程中的一件事,那么书的后面就没有答案了。答案应该是什么样的? */

练习1.11:编写一个程序,提示用户输入两个整数。打印这两个整数指定范围内的每个数字 大学本科第一年有C++问题。我在用入门书,这是我的答案。这是一个while语句练习。只有当x1>x2时,代码才能正常工作。 所以,当x2>x1时,它实际上不起作用,它应该起作用吗?这是我编码的第二天,有点混乱。如果这是编程中的一件事,那么书的后面就没有答案了。答案应该是什么样的? */,c++,C++,编辑:我在git hub上找到了这个: #include <iostream> int main(){ int x1 = 0 , x2 = 0; std::cout << "Enter two integers: " << std::endl; std::cin >> x1 >> x2; std::cout << " The range of the values entered is: &qu

编辑:我在git hub上找到了这个:

#include <iostream>
int main(){
int x1 = 0 , x2 = 0;
std::cout << "Enter two integers: " << std::endl;
std::cin >> x1 >> x2;
std::cout << " The range of the values entered is: " << std::endl; 
std::cout << x1 << std::endl;
while(x1<x2){
std::cout << ++x1 << std::endl;
        }
return 0;
}

/*为什么std::cout当x1 对于x1>x2,您可以在while循环之前交换整数,这样x1如果您不想交换x1和x2舍入,如果前者大于后者,您可能总是希望从x1到x2计数,然后使用

#include <iostream>

int main() {
  int bg = 0, ed = 0;
  std::cout << "Enter the number of begin and end: ";
  std::cin >> bg >> ed;
  while (bg <= ed)
    std::cout << bg++ << " ";
  std::cout << std::endl;
  return 0;
}

x1*x2是0或1,实际上是C++中的假和真,当乘以2时隐式转换为int类型,所以x1<x2*2 - 1是-1或1。< /p>该解决方案可能是X2和X1交换X2>X1或添加另一个分支来支持该情况。例如,当输入为10 5时,预期的输出是什么?您的答案是一个不错的起点。不过,我们当中没有人真正知道这个问题的用意。也许您可以始终打印“低-高”范围,或者如果范围中的x1>x2,您可以倒计时而不是倒计时。但这一点应该在书中或你的老师那里得到澄清。我建议在网上搜索while循环是如何工作的。实际上,当x2>=x1时,您的程序将永远不会工作,因为while循环将永远不会被输入。在执行循环之前,请找到两个循环的较低值和较高值,并从较低值循环到较高值。谢谢,交换似乎是解决此问题的唯一方法。@someone.exe:或两个版本的循环。取决于您希望的简单程度,或者x1>x2的输出是否应与x1不同

std::cout << x1 << std::endl;
while (x1 != x2){
    x1 += (x1 < x2) * 2 - 1;
    std::cout << x1 << std::endl;
}