用Java语言编写程序,打印下面的图形。 * ** *** **** ***** ****** *******
时间: 2023-04-02 20:02:57 浏览: 64
以下是Java语言编写程序,打印图形的代码:
public class PrintPattern {
public static void main(String[] args) {
for (int i = 1; i <= 7; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
输出结果为:
*
**
***
****
*****
******
*******
相关问题
在java语言中,使用图形用户界面,编写一个完整的计算及打印整数(3个整数)乘积的程序。
好的,这是一个计算机编程问题,我可以为您提供相关的解答。
您可以在Java中使用Swing类创建图形用户界面。以下是一个示例代码,可以实现输入3个整数,计算它们的乘积,并将结果输出到控制台:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField num1Field;
private JTextField num2Field;
private JTextField num3Field;
private JButton calculateButton;
public Calculator() {
setTitle("Calculator");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the num1 input field
JLabel num1Label = new JLabel("Num1");
num1Field = new JTextField(10);
// Create the num2 input field
JLabel num2Label = new JLabel("Num2");
num2Field = new JTextField(10);
// Create the num3 input field
JLabel num3Label = new JLabel("Num3");
num3Field = new JTextField(10);
// Create the Calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
// Add the components to the content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(num1Label);
contentPane.add(num1Field);
contentPane.add(num2Label);
contentPane.add(num2Field);
contentPane.add(num3Label);
contentPane.add(num3Field);
contentPane.add(calculateButton);
}
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
int num3 = Integer.parseInt(num3Field.getText());
int result = num1 * num2 * num3;
System.out.println("The product of " + num1 + ", " + num2 + ", and " + num3 + " is " + result);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
您可以将上述代码复制到Java IDE中运行,可以看到一个包含3个输入框和一个"Calculate"按钮的窗口。当点击"Calculate"按钮时,程序将读取3个整数并计算它们的乘积,并将结果输出到控制台。
本案例要求编写一个程序,可以根据用户要求在控制台打印出不同的图形。例如,用户自定义半径的空心圆形和用户自定义边长的空心正方形。使用Java语言编写
好的,我已经了解您的要求。下面是一个简单的 Java 程序,可以根据用户输入的参数在控制台上打印出空心圆形和空心正方形。
```java
import java.util.Scanner;
public class ShapePrinter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要打印的图形类型:1.空心圆形 2.空心正方形");
int type = scanner.nextInt();
if (type == 1) {
System.out.println("请输入圆形的半径:");
int radius = scanner.nextInt();
printHollowCircle(radius);
} else if (type == 2) {
System.out.println("请输入正方形的边长:");
int length = scanner.nextInt();
printHollowSquare(length);
} else {
System.out.println("输入有误,请重新运行程序!");
}
}
public static void printHollowCircle(int radius) {
for (int i = 0; i < 2 * radius + 1; i++) {
for (int j = 0; j < 2 * radius + 1; j++) {
int distance = (int) Math.sqrt((i - radius) * (i - radius) + (j - radius) * (j - radius));
if (distance == radius) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void printHollowSquare(int length) {
for (int i = 1; i <= length; i++) {
for (int j = 1; j <= length; j++) {
if (i == 1 || i == length || j == 1 || j == length) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
程序中定义了两个方法 `printHollowCircle` 和 `printHollowSquare`,分别用于打印空心圆形和空心正方形。在主函数中,根据用户输入的参数调用相应的方法并传入用户自定义的参数。