[英]I can't figure out the problem with my code not showing expected results
Write a program that gets two positive single digit integers from the user.编写一个程序,从用户那里获取两个正的个位数整数。 Then print all the numbers from 1 to 1,000 that are divisible by both of those integers.然后打印从 1 到 1,000 的所有可以被这两个整数整除的数字。 Print ten numbers per line and separate them by a tab.每行打印十个数字并用制表符分隔。 ('\t') ('\t')
I have done a code to print numbers from 1 - 1000 that are divisible by two numbers, x and y.我已经编写了一个代码来打印 1 - 1000 的数字,这些数字可以被两个数字 x 和 y 整除。
#include <iostream> using namespace std; int main() {
int x, y;
cout << "Enter two positives single digit integers: ";
cin >> x >> y;
for (int i = 1; i <= 1000; i++) {
if ( i % x == 0 && i % y == 0)
cout << i << '\t';
if (i % 10 == 9)
cout << endl;
else i++;
}
return 0; }
I'm not getting any results that I need for my code.我没有得到我的代码所需的任何结果。 Expected results:预期成绩:
Enter two positive single digit integers: 5 6
30 60 90 120 150 180 210 240 270 300
330 360 390 420 450 480 510 540 570 600
630 660 690 720 750 780 810 840 870 900
930 960 990
The 'i%10 ==9' should be a 0 and not a 9 since you want that after a number divisible by 10 (meaning that there is no remainder) you end the line. 'i%10 ==9' 应该是 0 而不是 9,因为您希望在可被 10 整除的数字之后(意味着没有余数)结束该行。 Second iterating i in the for loop is wrong since this is what a for loop initially does.在 for 循环中第二次迭代 i 是错误的,因为这是 for 循环最初所做的。 This is just some bad basics, try rereading c++ introduction books or watching more tutorials.这只是一些糟糕的基础知识,请尝试重读 c++ 介绍书籍或观看更多教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.