java 三角形 【题目描述】 对于给定的自然数n(n<20),在屏幕上输出仅由“*”构成的n行的 几种 三角形。 【输入】 输入n。 【输出】 题述三角形。* ** *** **** ***** ================== * ** *** **** ***** =================== * *** ***** ******* *********
时间: 2023-07-23 13:32:14 浏览: 97
好的,以下是 Java 代码实现:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
// 输出直角三角形
System.out.println("直角三角形:");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
// 输出等腰三角形
System.out.println("等腰三角形:");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
// 输出倒立等腰三角形
System.out.println("倒立等腰三角形:");
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
// 输出菱形
System.out.println("菱形:");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n-1; i >= 1; i--) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
```
代码中实现了输出四种不同类型的三角形,分别为直角三角形、等腰三角形、倒立等腰三角形和菱形。你可以根据需要自行选择输出哪种类型的三角形,程序会根据输入的 n 值输出对应类型的三角形。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)