java打印菱形通過(guò)二維數(shù)組來(lái)打印,實(shí)例如下:
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括修水網(wǎng)站建設(shè)、修水網(wǎng)站制作、修水網(wǎng)頁(yè)制作以及修水網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,修水網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到修水省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
int?hangshu?=?11;
int?yiban?=?hangshu?/?2?+?1;
int?yibanduo?=?hangshu?/?2;
System.out.println("空心菱形:");
for(int?k?=?1;k?=?yiban;k++){
for(int?i?=?1;i?=?(yiban?-?k);i++){
System.out.print("?");
}
System.out.print("*");
for(int?i?=?1;i?=?(?(k?-?2)?*?2?+?1);i++){
System.out.print("?");
}
if(k?!=?1){
System.out.print("*");
}
System.out.println();
}
for(int?k?=?yibanduo;k?=1;k--){
for(int?i?=?1;i?=?(yiban?-?k);i++){
System.out.print("?");
}
System.out.print("*");
for(int?i?=?1;i?=?(?(k?-?2)?*?2?+?1);i++){
System.out.print("?");
}
if(k?!=?1){
System.out.print("*");
}
System.out.println();
}
System.out.println("實(shí)心菱形:");
for(int?i?=?1;i?=?hangshu;i++){
if(i?=?hangshu/2?+?1){
for(int?k?=?1;k?=?hangshu/2?+?1-?i;k++){
System.out.print("?");
}
for(int?k?=?1;k?=?i;k++){
System.out.print("*?");
}
System.out.println();
}else{
for(int?k?=1;k?=?(i?-(hangshu/2?+?1));k++){
System.out.print("?");
}
for(int?k?=?1;k?=?(2?*(hangshu/2?+?1)?-?i);k++){
System.out.print("*?");
}
System.out.println();
}
}
結(jié)果如下:
for(i=1;i=4;i++)
{
這個(gè)小的for是打印每一行前邊的空格
for(j=1;j=4-i;j++)
System.out.print(" ");
這個(gè)是打印星星的
for(j=1;j=2*i-1;j++)
System.out.print("*");
這個(gè)是打印完一行的換行
System.out.println("");
}
下邊的是一樣的 ?只不過(guò)每一行的空格和星星數(shù) ? 和上邊是翻過(guò)來(lái)的
for(i=1;i=3;i++)
{
for(j=1;j=i;j++)
System.out.print(" ");
for(j=1;j=7-2*i;j++)
System.out.print("*");
System.out.println("");
}
java.sql.SQLException: 無(wú)效的列索引
無(wú)效的列索引
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:207)
atoracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5328)
atoracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5320)
at untitled5.addreader1.validID(addreader1.java:128)
at untitled5.addreader1.jButton1_actionPerformed(addreader1.java:97)
at untitled5.addreader1_jButton1_actionAdapter.actionPerformed(addreader1.java:175)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
代碼:String driverName = "oracle.jdbc.OracleDriver";
Driver driver = (Driver) Class.forName(driverName).newInstance();
//連接數(shù)據(jù)庫(kù)
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@LONG:1521:orcl", "sysman", "tongfang");
PreparedStatement pstmt = con.prepareStatement(
" insert into reader values(?)");
pstmt.setString(1, str1);
pstmt.setString(2,str2);
pstmt.setString(3,str3);
pstmt.setString(4,str4);
ResultSet res = pstmt.executeQuery();
參考下面主要代碼:
public static void draw()
{
int n=10;
for(int i=1;i=n;i++)//控制行數(shù)
{
for(int k=n-1;k=i;k--)//打印空格
{
System.out.print(" ");
}
for(int j=1;j=2*i-1;j++)//打印*
{
System.out.print("*");
}
System.out.println();
}
for(int i=n-1;i=1;i--)
{
for(int k=i;k=n-1;k++)
{
System.out.print(" ");
}
for(int j=1;j=2*i-1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
public?class?PrintDiamond2{
public?static?void?main?(String?args[]){
int?TotalLayer=9;?
for(int?layer=1;?layer=TotalLayer;?layer++)?{
for(int?space=1;?space=Math.abs((TotalLayer+1)/2-layer);?space++){
System.out.print("?");
}
for(int?b=1;?b=TotalLayer-2*Math.abs((TotalLayer+1)/2-layer);?b++){
System.out.print("*");
}
System.out.println();
}
}
}
用3個(gè)for循環(huán)打出菱形,可以改變TotalLayer的值來(lái)改變菱形的大小
第一個(gè)for循環(huán)規(guī)定多少行
第二個(gè)for循環(huán)規(guī)定每行有多少個(gè)空格
第三個(gè)for循環(huán)規(guī)定每行有多少個(gè)星號(hào)
然后換行,重復(fù)2,3循環(huán)直到完成第一個(gè)循環(huán)
最后菱形完成
可選中1個(gè)或多個(gè)下面的關(guān)鍵詞,搜索相關(guān)資料。也可直接點(diǎn)“搜索資料”搜索整個(gè)問(wèn)題。
java
用 "*"輸出菱形
import java.util.Scanner;
public class Print{
/**
* @param args
*/
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
System.out.println("請(qǐng)輸入菱形的最多#數(shù):");
int l = sin.nextInt();
if(l%2==0) {
System.out.println("由于輸入的是個(gè)偶數(shù),現(xiàn)在已+1轉(zhuǎn)換成奇數(shù)!");
l=l+1;
}
for(int i=1;i=l;i++) {
if(i%2==1) {
for(int j=1;j=(l-i)/2;j++) {
System.out.print(" ");
}
for(int k=1;k=i;k++) {
System.out.print("#");
}
}
else {
System.out.println();
}
}
System.out.println();
for(int i=l;i=1;i--) {
if(i%2==1) {
for(int j=1;j=(l-i)/2;j++) {
System.out.print(" ");
}
for(int k=1;k=i;k++) {
System.out.print("#");
}
}
else {
System.out.println();
}
}
}
}
下面的很好寫,就是把前面for循環(huán)條件里面反過(guò)來(lái)就是了.把++的換成--