i-j是沒有條件的,只是用i-j的值來作為判斷的標準。
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、網(wǎng)站建設(shè)、個舊網(wǎng)絡(luò)推廣、成都小程序開發(fā)、個舊網(wǎng)絡(luò)營銷、個舊企業(yè)策劃、個舊品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供個舊建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:jinyejixie.com
以第1行為例,i等于5,j等于1,time等于0,此時time不等于i-j,所以這個時候要打印空格,并且time自增1,進入下一輪循環(huán)。直到打印了4個空格之后,time等于4,此時
time!=i-j條件為假,跳出循環(huán),執(zhí)行下一步。
package thread;
import java.util.Scanner;
public class PopulationPyramid{
public static int decadeCounts = 0; // 代數(shù)
public static int mdemographics[]; // 男
public static int fdemographics[];// 女
// 用來記錄所有decades當(dāng)中male或者female的最大數(shù)。
public static int maxCount = 0;
/**
* init
* 初始化數(shù)據(jù)方法
*/
public void init() {
Scanner s = new Scanner(System.in);
System.out.println("How many decades please?");
// 獲取代數(shù)并創(chuàng)建對應(yīng)的數(shù)組對象
decadeCounts = s.nextInt();
mdemographics = new int[decadeCounts];
fdemographics = new int[decadeCounts];
// 第一次輸入時初始化不同年齡階段的人口數(shù)量。
for(int i = 0; i decadeCounts; i ++){
System.out.println("How many males of age " + (i * 10) + ":" + ((i + 1) * 10 - 1) + " ?");
// 記錄第I帶中male的數(shù)量
mdemographics[i] = s.nextInt();
// 記錄人口最大數(shù)
maxCount = maxCount mdemographics[i] ? maxCount : mdemographics[i];
System.out.println("How many males of age " + (i * 10) + ":" + ((i + 1) * 10 - 1) + " ?");
fdemographics[i] = s.nextInt();
maxCount = maxCount fdemographics[i] ? maxCount : fdemographics[i];
}
print();
}
public void getNextGeneration(){
Scanner s = new Scanner(System.in);
int maleBabies;
int femaleBabies;
System.out.println("The next generation");
System.out.println("Enter number of male babies ");
maleBabies = s.nextInt();
System.out.println("Enter number of female babies ");
femaleBabies = s.nextInt();
// 調(diào)整年齡結(jié)構(gòu)
for(int i = decadeCounts - 1; i 0; i --){
mdemographics[i] = mdemographics[i - 1];
fdemographics[i] = fdemographics[i - 1];
}
// 將最新的放入數(shù)組當(dāng)中
mdemographics[0] = maleBabies;
fdemographics[0] = femaleBabies;
print();
}
/**
* 輸出人口金字塔
*/
public void print() {
System.out.println("The demographic map");
for (int i = decadeCounts - 1; i = 0; i--) {
// 輸出男性左側(cè)對應(yīng)的空格
for(int j = 0; j maxCount - mdemographics[i]; j ++)
System.out.print(" ");
// 輸出男性人數(shù)
for(int j = 0; j mdemographics[i]; j ++)
System.out.print("*");
// 輸出女性人數(shù)
for(int j = 0; j fdemographics[i]; j ++)
System.out.print("#");
// 換行
System.out.println();
}
}
public static void main(String[] args) {
PopulationPyramid _instance = new PopulationPyramid();
_instance.init();
while(true){
_instance.getNextGeneration();
}
}
}
給分吧,哈哈,還有注釋的。。。。、
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
// 點陣法:
// 首先,我們假設(shè)金字塔是等邊三角形,等邊三角形與矩形的關(guān)系是:
// 1, 底邊是矩形的寬度
// 2, 高是舉行的長度
// 3, 頂點是矩形的底邊中點
// *******
// |* *|
// | * * |
// ---*---
// 這樣,如果我們知道矩形的長和寬,我們就能指導(dǎo)等邊三角形在矩形每一行中的點位于什么位置了:
// 若長(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; // 對參數(shù)進行驗證整理
width = width = 0 ? 3 : width; // 對參數(shù)進行驗證整理
// 每行有多少點?
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';
}
// 處理從第二行到倒數(shù)第二行
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; // 這里對數(shù)組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);
}
}
文章題目:java金字塔圖形的代碼 金字塔代碼 java
網(wǎng)頁URL:http://jinyejixie.com/article34/dochdpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站設(shè)計公司、全網(wǎng)營銷推廣、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)