我正试图根据用户输入的数字输出一个三角形类型的图形。例如,如果用户输入3,输出应该是:
1
12
123
但我得到的只是
1
2
3
我需要修复什么?
//File: digits.cpp
//Created by: Noah Bungart
//Created on: 02/16/2020
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int Rows;
cout << "Enter number of rows: ";
cin >> Rows;
int i = 1;
int j = 1;
while (i <= Rows){
while (j <= i){ //Should be iterating through j loop but isn't
if (i < 10){
cout << j;
}
if (i >= 10){
cout << (j % 10);
}
j++;
}
cout << endl;
i++;
}
return(0);
}
您刚刚忘记"重置"j
变量。
只需在i++
之后(或在第二个while循环之前(添加一个j = 1;
。