编写java程序,输出如下图形 ****** ****** ******
时间: 2023-07-18 15:43:20 浏览: 120
以下是输出如上图形的Java程序:
```java
public class PrintPattern {
public static void main(String[] args) {
String pattern = "******";
for (int i = 0; i < 3; i++) {
System.out.print(pattern + " ");
}
}
}
```
输出结果为:
```
****** ****** ******
```
相关问题
2. 编写java程序,输出如下图形 * *** ***** *******
以下是Java程序实现:
```java
public class Main {
public static void main(String[] args) {
int rows = 4;
int stars = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < stars; j++) {
System.out.print("*");
}
System.out.println();
stars += 2;
}
}
}
```
在这个程序中,我们使用两个变量`rows`和`stars`来控制输出的行数和星号数量。外层循环用于控制行数,内层循环用于输出星号。在每行输出结束后,我们将星号数量增加2,以便输出下一行。最后,我们使用`System.out.println`语句输出结果。
编写程序在窗体上输出如下图形。 * ** *** **** java
您可以使用以下代码在窗体上输出如下图形:
```
public class PrintTriangle extends JFrame {
public PrintTriangle() {
setTitle("Print Triangle");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new TrianglePanel());
}
public static void main(String[] args) {
PrintTriangle frame = new PrintTriangle();
frame.setVisible(true);
}
}
class TrianglePanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 10;
int y = 30;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
g.drawString("*", x, y);
x += 10;
}
x = 10;
y += 20;
}
g.drawString("java", x, y);
}
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)