按照你的要求加詳細(xì)注釋的圣誕樹Java程序如下:(編程思想在注釋中說明)
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的蘿北網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
public?class?ShengDanShu2?{
//這個(gè)程序的編程思想是利用對(duì)for循環(huán)變量i的控制達(dá)到一層循環(huán)代替雙層循環(huán)的目的
public?static?void?main(String[]?args)?{????
int???n=5;???//初始化打印圣誕樹層數(shù)變量n
int???a=0;???//初始化打印前置空格數(shù)變量a
int???b=0;???//初始化打印星號(hào)數(shù)變量b
for(int?i=1;i?=n;i++){???//打印n層圣誕樹
if(a!=(n-i)){????//如果前置空格數(shù)不等于n-i
System.out.print("?");?//打印一個(gè)空格
a++;????//前置空格數(shù)加一???
i=i-1;????//i變量減一??目的是固定住i變量不變直到a==n-i
}else?if(b!=(2*i-1)){???//如果星號(hào)數(shù)不等于2*i-1
System.out.print("*");??//打印一個(gè)星號(hào)
b++;????//星號(hào)數(shù)加一
i=i-1;???//i變量減一??目的是固定住i變量不變直到b==2*i-1
}else?if(a==(n-i)??b==(2*i-1)){//當(dāng)以上兩個(gè)條件都滿足時(shí),換行初始化a和b為0???
System.out.println();??//打印換行?
a=0;???//對(duì)新的一行重新初始化前置空格數(shù)變量a
b=0;??//對(duì)新的一行重新初始化打印星號(hào)數(shù)變量b
//這里沒有控制for循環(huán)的i變量減一,因?yàn)檫@時(shí)i變量加一,開始新一行。
}???
}???
}?????
}
運(yùn)行結(jié)果:
*
***
*****
*******
*********
這是代碼,你可以自己調(diào)試一下。
數(shù)據(jù)結(jié)構(gòu)如下:
CREATE TABLE dtree (
id int,
pid int,
name varchar(200),
url varchar(200),
title varchar(200),
target varchar(200),
icon varchar(200),
iconopen varchar(200),
opened bit);
為了實(shí)現(xiàn)獲取數(shù)據(jù)庫變量功能,需要建立一個(gè)DTree類,并編譯生成CLASS文件,放入\WEB-INF\classes文件夾下。
DTree類代碼如下:
package work3;
public class DTree {
private int id;
private int pid;
private String name;
private String url;
private String title;
private String target;
private String icon;
private String iconOpen;
private int opened;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getIconOpen() {
return iconOpen;
}
public void setIconOpen(String iconOpen) {
this.iconOpen = iconOpen;
}
public int getOpened() {
return opened;
}
public void setOpened(int opened) {
this.opened = opened;
}
}
work3.jsp代碼如下:
%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%
%@ page import="java.sql.*"%
jsp:useBean id='settree' scope="application" class="work3.DTree" /
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
""
html
head
meta http-equiv="Content-Type" content="text/html; charset=GB18030"
link rel="StyleSheet" href="dtree.css" type="text/css" /
script type="text/javascript" src="dtree.js"/script
titledTree in MySQL/title
/head
body
h2
Example
/h2
div class="dtree"
p
a href="javascript: d.openAll();"open all/a |
a href="javascript: d.closeAll();"close all/a
/p
script type="text/javascript"
!--
d = new dTree('d');
%
//驅(qū)動(dòng)程序名
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//數(shù)據(jù)庫用戶名
String userName = "sa";
//密碼
String userPwd = "1";
//數(shù)據(jù)庫名
String dbName = "master";
//表名
String tableName = "dtree";
//連接字符串
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;
//加載驅(qū)動(dòng)
Class.forName(driverName).newInstance();
//連接數(shù)據(jù)庫
java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);
//得到Statement實(shí)例
java.sql.Statement statement = conn.createStatement();
//查詢數(shù)據(jù)
String sql = "select * from " + tableName;
//返回結(jié)果
java.sql.ResultSet rs = statement.executeQuery(sql);
//獲取變量
while (rs.next()) {
settree.setId(rs.getInt(1));
settree.setPid(rs.getInt(2));
settree.setName(rs.getString(3));
settree.setUrl(rs.getString(4));
settree.setTitle(rs.getString(5));
settree.setTarget(rs.getString(6));
settree.setIcon(rs.getString(7));
settree.setIconOpen(rs.getString(8));
settree.setOpened(rs.getInt(9));
if(settree.getPid()==0)
settree.setOpened(1);
%
d.add(%=settree.getId()%,%=settree.getPid()%,'%=settree.getName()%','%=settree.getUrl()%','%=settree.getTitle()%','%=settree.getTarget()%','','',%=settree.getOpened()%);
%
}
%
document.write(d);
//--
/script
/div
/body
/html
朋友 你說的最好一句話我沒明白?
你能具體說下嗎?最好弄個(gè)圖片。
我跟你說一下思路:就是用for循環(huán)就可以.
---給你答案 供你參考,倉促寫的:
public class ChristmasTree {
int x = 2;
int i = 0;
public void tree() {
for(i=0; i3; i++) {
for(int j=x; j=0; j--) {
System.out.print(" ");
}
x--;
for(int k=0; k=i; k++) {
System.out.print("*"+" ");
}
System.out.println();
}
}
public static void main(String args[]) {
new ChristmasTree().tree();
}
}