3个回答
展开全部
以下是实心、空心、倒立,三种等边三角形的代码:
实心等边三角形:
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();
}
}
展开全部
package com.webkfa.test;
/**
* web开发技术网
* 网址:
* www.
* webkfa.
* com
*/
public class Test {
public static void main(String[] args){
printTriangle(5);
}
/**
* 等腰三角形
* @param len
*/
public static void printTriangle(int len) {
//原代码地址:http://www.webkfa.com/one7/w498.html
int a = 0;
int b = 0;
for (int i=1; i<=len; i++) {
if (a != (len - i)) {
System.out.print(" ");
a++;
i = i - 1;
} else if (b != (2 * i - 1)) {
System.out.print("*");
b++;
i = i - 1;
} else if (a == (len - i) && b == (2 * i - 1)) {
System.out.println();
a = 0;
b = 0;
}
}
}
}
更多追问追答
追问
有简单些的代码吗?刚学Java
追答
就一个方法,main函数里面就一句话
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用java的图形类可以画出多种图形。你搜下java画图。
追问
需要Java代码编写的,暂时不需要Java画图
追答
你难道只要用简单的循环打印一个等边三角形啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询