3.用双重 for 循环打印星号三角形,行数由键盘输入的整数决定。运行效果:输入行数:6 ****** ***** **** *** ** *
时间: 2023-11-26 08:03:15 浏览: 31
以下是使用Java语言编写的双重for循环打印星号三角形的代码,其中行数由键盘输入的整数决定:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入行数:");
int n = input.nextInt();
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
```
运行效果:
```
请输入行数:6
******
*****
****
***
**
*
```
相关问题
以c++来写3.用双重 for 循环打印星号三角形,行数由键盘输入的整数决定。运行效果:输入行数:6 ****** ***** **** *** ** *
以下是C++代码,用双重for循环打印星号三角形,行数由键盘输入的整数决定:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "输入行数:";
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = n; j > i; j--) {
cout << "*";
}
cout << endl;
}
return 0;
}
```
运行效果:
```
输入行数:6
******
*****
****
***
**
*
```
c语言编写用双重for循环打印星号三角形,行数由键盘输入的整数决定。运行效果为: 输入行数:6 ****** ***** **** *** ** *
可以使用双重for循环来实现打印星号三角形的效果。首先,我们需要从键盘输入一个整数作为行数。然后,使用两个嵌套的for循环来控制打印的行数和每行的星号数量。
下面是C语言的代码实现:
```c
#include <stdio.h>
int main() {
int rows;
printf("输入行数:");
scanf("%d", &rows);
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
运行效果如下:
```
输入行数:6
******
*****
****
***
**
*
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)