import java.awt.Color;
網站建設哪家好,找創新互聯!專注于網頁設計、網站建設、微信開發、重慶小程序開發、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了班瑪免費建站歡迎大家使用!
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
// 點陣法:
// 首先,我們假設金字塔是等邊三角形,等邊三角形與矩形的關系是:
// 1, 底邊是矩形的寬度
// 2, 高是舉行的長度
// 3, 頂點是矩形的底邊中點
// *******
// |* *|
// | * * |
// ---*---
// 這樣,如果我們知道矩形的長和寬,我們就能指導等邊三角形在矩形每一行中的點位于什么位置了:
// 若長(height)為4,寬(width)為7.
// 計算等邊三角形在某一行x的兩點位置為:
// 若x == 1, 則整行都是三角形的邊
// 若x == 4, 則只有中點是三角形的邊。中點 = width / 2 + (width % 2 0 ? 1 : 0)
// 若 x 1 x 4,則左點 = width / (2 * height) * x; 右點 = width - width / (2 * height) * x + 1;
// 知道了三角形的點,我們就能畫出金字塔了。
// Java2D
// 首先,找到三角形的三個點,在兩兩相連即可。
// 示例如下:
public class Question1 {
// 矩形
class Rec{
int height;
int width;
public Rec(int height, int width){
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
}
// 以字符串打印形式繪制[點陣法]
public Question1(int height, int width, char shape) {
height = height = 0 ? 3 : height; // 對參數進行驗證整理
width = width = 0 ? 3 : width; // 對參數進行驗證整理
// 每行有多少點?
int rowPoints = width;
// 不要忘記每行最后還有換行符
rowPoints ++;
// 總共有多少點?
int totalPoints = rowPoints * height;
char[] allChar = new char[totalPoints]; // 所有行的字符
// 特殊處理第一行
for(int i = 0; i rowPoints; i++){
if(i rowPoints - 1) // rowpoints位是換行符,所以這里需要特殊處理。
allChar[i] = shape;
else
allChar[i] = '\n';
}
// 處理從第二行到倒數第二行
for(int i = 2; i height; i++){
//左點 = width / (2 * height) * x; 右點 = width - width / (2 * height) * x + 1;
// 但是這里得牢記,運算符的運算順序,所以必須這樣寫:
int leftpoint = (width * i) / (2 * height);
int rightpoint= width - (width * i) / (2 * height) + 1;
for(int j = 0; j rowPoints; j++){
int index = (i - 1) * rowPoints + j; // 這里對數組index的計算很重要,千萬別算錯了。
if( j rowPoints - 1){
if(j + 1 == leftpoint || j + 1 == rightpoint) // 列序號從0開始,但點位是從1開始的,所以給列序號+1
allChar[index] = shape;
else
allChar[index] = ' ';
}else{
allChar[index] = '\n';
}
}
}
//特殊處理最后一行
int point = width / 2 + (width % 2 0 ? 1 : 0);
int startIndex = (height - 1) * rowPoints;
for(int i = 0; i rowPoints; i++){
if( i rowPoints - 1){
if( i + 1 == point)
allChar[startIndex + i] = shape;
else
allChar[startIndex + i] = ' ';
}else
allChar[allChar.length - 1] = '\n';
}
String result = new String(allChar);
System.out.print(result);
}
class PanelContainer extends JPanel{
private Point point1;
private Point point2;
private Point point3;
public PanelContainer(Point point1, Point point2, Point point3){
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
setBackground(Color.white);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(point1 != null point2 != null point3 != null){
g.setColor(Color.red);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point2.getX(), (int)point2.getY());
g2d.drawLine((int)point2.getX(), (int)point2.getY(), (int)point3.getX(), (int)point3.getY());
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point3.getX(), (int)point3.getY());
}
}
}
//Java2D
public Question1(int height, int width) {
JFrame frame = new JFrame("Java2D 三角形");
frame.setBounds(50, 50, 400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Point point1 = new Point(50, 50);// 讓圖形舉例邊界50像素
Point point2 = new Point(width + 50, 50);
Point point3 = new Point(width/2 + 50, height + 50);
PanelContainer container = new PanelContainer(point1, point2, point3);
frame.getContentPane().add(container);
frame.setVisible(true);
}
public static void main(String args[]){
new Question1(200, 300, '*');
new Question1(200, 300);
}
}
注意: for(int k=7;k=-2*i+9;k--)這個循環語句在i=1的情況下出現死循環,i不肯能=2。
i=1,-2*i+9=7,k=7滿足,K--=6,滿足循環,所以這個for循環會無限執行下去,改改試試吧。
這是我剛才編寫的用于輸出金字塔的一個類。完整的代碼。//輸出金字塔importjava.util.Scanner;publicclassa1{publicstaticvoidmain(String[]args){Scannera=newScanner(System.in);intN=5;//定義行數的變量booleanb=true;do{try{System.out.println("請輸入整數類型的數字:");N=a.nextInt();//獲取輸入行數b=false;}catch(Exceptionea){a=newScanner(System.in);//N=a.nextInt();//獲取輸入行數}}while(b);inti,j,m;for(i=0;iN;i++)//輸出金字塔{for(m=0;mN-1-i;m++){System.out.printf("");}for(j=0;j2*i+1;j++){System.out.printf("*");}System.out.printf("\n");}}}
標題名稱:java倒金字塔代碼 c++倒金字塔
分享網址:http://m.kartarina.com/article30/dogippo.html
成都網站建設公司_創新互聯,為您提供網站導航、網站維護、網站建設、關鍵詞優化、全網營銷推廣、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯