试试我这个代码,应该能答应出菱形来!
import javax.swing.JOptionPane;
public class PrintStar {
public static void main(String[] args) {
String number = JOptionPane.showInputDialog("Enter an integer!");
int n = Integer.parseInt(number);
String output = "打印图形为:" + "\n";
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= n - i; k++)
output += "
";
for (int j = 0; j <= i - 1; j++)
output += "* ";
for (int m = 1; m <= i - 1; m++)
output += "* ";
output += "\n";
}
for (int i = n; i >= 1; i--) {
for (int k = 1; k <= n - i; k++)
output += "
";
for (int j = 1; j <= i - 1; j++)
output += "* ";
for (int m = 1; m <= i; m++)
output += "* ";
output += "\n";
}
JOptionPane.showMessageDialog(null, output);
}
}