JAVA 类 直角三角形
时间: 2024-02-15 09:28:37 浏览: 85
下面是四种打印直角三角形的Java代码示例:
示例1 - 打印正直角三角形:
```java
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
```
示例2 - 打印倒直角三角形:
```java
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print("*");
}
System.out.println();
}
}
```
示例3 - 打印正直角三角形(反):
```java
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
```
示例4 - 打印倒直角三角形(反):
```java
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i + 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 5 - i; j++) {
System.out.print("*");
}
System.out.println();
}
}
```
其中,示例1和示例2分别打印正直角三角形和倒直角三角形,示例3和示例4分别打印正直角三角形(反)和倒直角三角形(反)。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Java打印直角三角形](https://blog.csdn.net/qq_37652391/article/details/106183013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Java打印直角三角形、等腰三角形、菱形、沙漏形](https://blog.csdn.net/weixin_39968820/article/details/114707607)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文