网站建设资讯

NEWS

网站建设资讯

java程序输出菱形代码 java输入菱形

用java编写菱形

for(i=1;i=4;i++)

创新互联建站专业为企业提供桃山网站建设、桃山做网站、桃山网站设计、桃山网站制作等企业网站建设、网页设计与制作、桃山企业网站模板建站服务,十余年桃山做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

{

这个小的for是打印每一行前边的空格

for(j=1;j=4-i;j++)

System.out.print(" ");

这个是打印星星的

for(j=1;j=2*i-1;j++)

System.out.print("*");

这个是打印完一行的换行

System.out.println("");

}

下边的是一样的  只不过每一行的空格和星星数   和上边是翻过来的

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: 无效的列索引

无效的列索引

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();

//连接数据库

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();

怎么用java打印菱形?

java打印菱形方法:

package circulationDemo;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("请输入要打印的行数:");

int num = input.nextInt();

t1(num);// 正直角

split();

t2(num);// 倒直角

split();

t3(num);// 等腰三角

split();

t4(num);// 等腰倒三角++

split();

t5(num);// 等腰倒三角--

split();

t6(num);// 实心菱形

split();

t7(num);// 空心菱形(1)

split();

t8(num);// 空心菱形(2)

split();

t9(num);// 空心菱形(3)

}

private static void t9(int num) {

for (int k = 1; k = num; k++) {

for (int i = 1; i = (num - 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 = num-1; k = 1; k--) {

for (int i = 1; i = (num - 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();

}

}

private static void t8(int num) {

// 上半部分

for (int i = 1; i = num; i++) {

// 打印上半部分空格

for (int j = 1; j = num - i; j++) {

System.out.print(" ");

}

// 打印原本实心的*部分

for (int j = 1; j = 2 * i - 1; j++) {

// 仅在一行的开头和末尾打印*

if (j == 1 || j == 2 * i - 1) {

System.out.print("*");

} else {

System.out.print(" ");

}

}

System.out.println();

}

// 下半部分

for (int i = 1; i = num - 1; i++) {

for (int j = 1; j = i; j++) {

System.out.print(" ");

}

for (int j = 1; j = 2 * (num - i - 1) + 1; j++) {

if (j == 1 || j == 2 * (num - i - 1) + 1) {

System.out.print("*");

} else {

System.out.print(" ");

}

}

System.out.println();

}

}

private static void t7(int num) {

// 打印上半部分

for (int i = 1; i = num; i++) {

// 打印空格

for (int j = 1; j = (num - i); j++) {

System.out.print(" ");

}

// 打印*的个数

if (i == 1) {

System.out.print("*");

} else {

System.out.print("*");

// 打印菱形里的空格

for (int j = 1; j = (2 * i - 3); j++) {

System.out.print(" ");

}

System.out.print("*");

}

System.out.println();

}

// 打印下半部分

for (int i = num - 1; i  0; i--) {

// 打印空格

for (int j = 1; j = (num - i); j++) {

System.out.print(" ");

}

// 打印*的个数

if (i == 1) {

System.out.print("*");

} else {

System.out.print("*");

// 打印菱形里的空格

for (int j = 1; j = (2 * i - 3); j++) {

System.out.print(" ");

}

System.out.print("*");

}

System.out.println();

}

}

private static void t6(int num) {

// 上半部分

for (int i = 1; i = num; i++) {

for (int j = 1; j = num - i; j++) {

System.out.print(" ");

}

for (int j = 1; j = 2 * i - 1; j++) {

System.out.print("*");

}

System.out.println();

}

// 下半部分

for (int i = 1; i = num - 1; i++) {

for (int j = 1; j = i; j++) {

System.out.print(" ");

}

for (int j = 1; j = 2 * (num - 1 - i) + 1; j++) {

System.out.print("*");

}

System.out.println();

}

}

private static void t5(int num) {

for (int i = num; i  0; i--) {

for (int j = 1; j = num - i; j++) {

System.out.print(" ");

}

for (int j = 1; j = 2 * i - 1; j++) {

System.out.print("*");

}

System.out.println();

}

回答于 2022-12-14

java打印空心菱形

Java利用for循环输出空心菱形的实例代码

编写程序,在控制台上输出空心菱形,对角距离为6.

代码如下:

public class Diamond {

public static void main(String[] args) {

printHollowRhombus(6);

}

public static void printHollowRhombus(int size) {

if (size % 2 == 0) {

size++;// 计算菱形大小

}

for (int i = 0; i size / 2 + 1; i++) {

for (int j = size / 2 + 1; j i + 1; j--) {

System.out.print("  ");// 输出左上角位置的空白

}

for (int j = 0; j 2 * i + 1; j++) {

if (j == 0 || j == 2 * i) {

System.out.print("* ");// 输出菱形上半部边缘

} else {

System.out.print("  ");// 输出菱形上半部空心

}

}

System.out.println("");     //换行

}

for (int i = size / 2 + 1; i size; i++) {

for (int j = 0; j i - size / 2; j++) {

System.out.print("  ");// 输出菱形左下角空白

}

for (int j = 0; j 2 * size - 1 - 2 * i; j++) {

if (j == 0 || j == 2 * (size - i - 1)) {

System.out.print("* ");// 输出菱形下半部边缘

} else {

System.out.print("  ");// 输出菱形下半部空心

}

}

System.out.println("");     //换行

}

}

}

效果如图:


网站名称:java程序输出菱形代码 java输入菱形
URL网址:http://cdweb.net/article/dogeico.html