本文目錄一覽:
- 1、如何用java畫個等腰三角形.
- 2、java 打印等腰三角形,
- 3、如何用java的for循環做一個等腰三角形?
- 4、各位前輩,如何用java畫個等腰三角形
- 5、怎樣用java打印像這樣等腰三角形 * * * * * * * * * *
如何用java畫個等腰三角形.
1、實心等邊三角形java參考代碼如下:
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();
}
}
2、空心等邊三角形參考代碼如下:
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;j2*(n+1)-1;j++){
System.out.print(x);
}
}
java 打印等腰三角形,
java 打印等腰三角形可以採用如下方式:
public class Mul {
public static void main(String args[]) {
for (int i = 1; i = 6; i++) {
// 空格分佈
for (int j = 6 – i; j 0; j–) {
System.out.print(” “);
}
// 符號分佈
for (int j = 1; j = i; j++) {
System.out.print(“* “);
}
System.out.println();
}
}
}
效果如下:
如何用java的for循環做一個等腰三角形?
第1種方法:
要用三循環才可以, 這個就是三角形的代碼.
public class Test {
public static void main(String[] args) {
for (int i = 1; i = 7; i++) {
for (int j = 1; j = 7-i; j++) {//7-i的意思是第一次的時候7-I等於6那這個for這輸入6個空格第二次的時候7-I等於5那for就輸入5個空格//每循環一次這個I就要自動加一個數
System.out.print(” “); //這裏面意思是輸入空格 //注意這裡的空格是的確要輸入的不然可以在空格里輸入一個數字看一看效果
}
for (int ji = 1; ji =2*i-1; ji++) {//這個for循環的意思是輸入*號
//和ji=2*i-1的意思是;這裡2*i-1的意思是在空格後面加入一個*號,第一次循環是
System.out.print(“*”);
}
System.out.println();//這裡的意思在下一次循環的時候換行ln的意思就是換行。而沒有ln的就不換行
}
}
}
第2種方法:
首先要有一個參數,即等腰三角形的高度h,然後根據h計算第i層打印的字符數量n以及開始位置s,接着把n和s作為參數傳給執行打印的方法printchars(s,n);
例:
——*——
—-*-*-*—-
–*-*-*-*-*–
*-*-*-*-*-*-*
以最後一行的字符數為準,最後一行的字符數應該是2h-1,字符之間的空格數(事例中是「-」)是2h-1-1,所以總字符數是4h-3,也就是每一行的長度是4h-3。第i層的字符數量為2i-1,開始位置是2(h-i)。
public class IsoscelesTriangle {
/**
* 等腰三角形
*/
public void print1(){//形如 ▲向上的
for(int i=1;i=5;i++){
for(int k=1;k=5-i;k++)
System.out.print(” “);
for(int j=1;j=2*i-1;j++)
System.out.print(“*”);
System.out.print(“\n”);
}
}
public void print2(){//形如:倒▲ 向下的
for(int i=1;i=5;i++){
for(int j=1;j=i-1;j++)
System.out.print(” “);
for(int k=1;k=11-(2*i);k++)
System.out.print(“*”);
System.out.print(“\n”);
}
}
public static void main(String[] args) {
IsoscelesTriangle app = new IsoscelesTriangle();
System.out.println(“—————————“);
app.print1();
System.out.println(“—————————“);
app.print2();
System.out.println(“—————————“);
}
}
第3種方法:
public class Trigon
{
public static void main(String[] arges){
for(int i=1;i=9;i++){
for(int j=1;j=9-i ;j++ ){
System.out.print(” “);
}
for(int k=1;k=2*i-1;k++){
System.out.print(i);
}
System.out.println();
}
System.out.println(“———————————-“);
for(int i=9;i=1;i–){
for(int j=1;j=9-i;j++){
System.out.print(” “);
}
for(int k=1;k=2*i-1;k++){
System.out.print(i);
}
System.out.println();
}
}
}
各位前輩,如何用java畫個等腰三角形
窗口的(Swing)就相對容易一些,只要你找到三個點的坐標就可以通過API畫出來
而控制台版就通過打點,打空格來打到畫出等腰三角形;
public class DrawTriangleTest extends JFrame {
private Point top;
private Point bottom1;
private Point bottom2;
public static void main(String[] args) {
new DrawTriangleTest();//swing版的三角形
//下面是控制台版的三角形
//———————–
int n = 10;
String c = “\t”; //分隔符
String x = “\t*”; //打點
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;j2*(n+1)-1;j++){
System.out.print(x);
}
//———————–
}
public DrawTriangleTest() {
init();
}
public void init() {
this.setSize(new Dimension(300, 300));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
drawTrinagle(100, 150);
}
public void drawTrinagle(int h, int w) {
int bx = (int) ((this.getWidth() – w) / 2.0);// 低端左邊點的x
int ty = (int) ((this.getHeight() – h) / 2.0);//
top = new Point(this.getWidth() / 2, ty);// 定點
bottom1 = new Point(bx, ty + h);// 左下角的點
bottom2 = new Point(bx + w, ty + h);// 右下角的點
this.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (top != null bottom1 != null bottom2 != null) {
g.drawLine(bottom1.x, bottom1.y, bottom2.x, bottom2.y);// 底下那條線
g.drawLine(top.x, top.y, bottom2.x, bottom2.y);// 左邊的腰
g.drawLine(bottom1.x, bottom1.y, top.x, top.y);// 右邊的腰
}
}
}
怎樣用java打印像這樣等腰三角形 * * * * * * * * * *
用java語言輸出等腰三角形的話一般都是用for語句就行了
代碼例子給一個你: 如下:
/**
* 輸出各種三角形,菱形,正方形
* @author young
*
*/
public class TrianglePrint {
// 倒三角
public static void p2() {
int n = 5;
int a = 0;
int b = 0;
for (int i = n; i = 1; i–) {
if (a != (n – i)) {
System.out.print(” “);
a++;
i = i + 1;
} else if (b != (2 * i – 1)) {
System.out.print(“*”);
b++;
i = i + 1;
} else if (a == (n – i) b == (2 * i – 1)) {
System.out.println();
a = 0;
b = 0;
}
}
}
public static void main(String[] args) {
p2();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/311255.html