java中怎样打印一个等边三角形
答案:3 悬赏:10 手机版
解决时间 2021-03-18 22:22
- 提问者网友:树红树绿
- 2021-03-18 04:40
java中怎样打印一个等边三角形
最佳答案
- 五星知识达人网友:傲气稳了全场
- 2021-03-18 04:57
以下是实心、空心、倒立,三种等边三角形的代码:
实心等边三角形:
public static void main(String[] args) {
int n = 5;
String c = "0";
String x = "*";
for (int i = 0; i < n; i++) {
for (int k = 0; k < n - i - 1; k++) {
System.out.print(c);
}
for (int k = 0; k < i + 1; k++) {
System.out.print(x);
}
for (int k = 0; k < i; k++) {
System.out.print(x);
}
// for (int k = 0; k < n - i - 1; k++) {
// System.out.print(c);
// }
System.out.println();
}
}
空心等边三角形:
public static void main(String[] args) {
int n = 6;
String c = " ";
String x = "*";
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * n; j++) {
if (j == (n - i) || j == (n + i)) {
System.out.print(x);
} else {
System.out.print(c);
}
}
System.out.println();
}
for(int j=0;j<2*(n+1)-1;j++){
System.out.print(x);
}
}
倒等边三角形:
public static void main(String[] args) {
int n = 5;
String c = " ";
String x = "*";
for(int j=0;j<2*(n+1)-1;j++){
System.out.print(x);
}
System.out.println();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * n; j++) {
if (j == i+1 || j == (2*n - i-1)) {
System.out.print(x);
} else {
System.out.print(c);
}
}
System.out.println();
}
}
实心等边三角形:
public static void main(String[] args) {
int n = 5;
String c = "0";
String x = "*";
for (int i = 0; i < n; i++) {
for (int k = 0; k < n - i - 1; k++) {
System.out.print(c);
}
for (int k = 0; k < i + 1; k++) {
System.out.print(x);
}
for (int k = 0; k < i; k++) {
System.out.print(x);
}
// for (int k = 0; k < n - i - 1; k++) {
// System.out.print(c);
// }
System.out.println();
}
}
空心等边三角形:
public static void main(String[] args) {
int n = 6;
String c = " ";
String x = "*";
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * n; j++) {
if (j == (n - i) || j == (n + i)) {
System.out.print(x);
} else {
System.out.print(c);
}
}
System.out.println();
}
for(int j=0;j<2*(n+1)-1;j++){
System.out.print(x);
}
}
倒等边三角形:
public static void main(String[] args) {
int n = 5;
String c = " ";
String x = "*";
for(int j=0;j<2*(n+1)-1;j++){
System.out.print(x);
}
System.out.println();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * n; j++) {
if (j == i+1 || j == (2*n - i-1)) {
System.out.print(x);
} else {
System.out.print(c);
}
}
System.out.println();
}
}
全部回答
- 1楼网友:老鼠爱大米
- 2021-03-18 07:20
用java的图形类可以画出多种图形。你搜下java画图。追问需要Java代码编写的,暂时不需要Java画图追答你难道只要用简单的循环打印一个等边三角形啊!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯