用Java编菱形图案程序/**
* 输出各种三角形,菱形,正方形
* @author young
*
*/
public class TrianglePrint {
// 正三角
public static void p1() {
int n = 5;
int a = 0;
int b = 0;
for (int i = 1; i = n; i) {
if (a != (n - i)) {
System.out.print(" ");
a;
i = i - 1;
} else if (b != (2 * i - 1)) {
System.out.print("*");
b;
i = i - 1;
} else if (a == (n - i)b == (2 * i - 1)) {
System.out.println();
a = 0;
b = 0;
}
}
}
// 倒三角
public static void p2() {
int n = 5;
int a = 0;
int b = 0;
for (int i = n; i = 1; i--) {
if (a != (n - i)) {
System.out.print(" ");
a;
i = i1;
} else if (b != (2 * i - 1)) {
System.out.print("*");
b;
i = i1;
} else if (a == (n - i)b == (2 * i - 1)) {
System.out.println();
a = 0;
b = 0;
}
}
}
// 倒三角:用户决定行数
public static void sta(int n) {
for (int i = 0; in; i) {
for (int j = 0; ji; j) {
System.out.print(" ");
}
for (int k = 0; k2 * (n - i) - 1; k) {
System.out.print("*");
}
System.out.println();
}
}
// 输出正方形
public static void zf(int N) {
for (int i = 1; i = N; i) {
for (int j = 1; j = N; j) {
if (j == 1 || j == N || i == 1 || i == N)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
// 输出正菱形
public static void lx(int n) {
for (int i = 0; in - 1; i) {
for (int x = i1; xn; x) {
System.out.print(" ");
}
for (int y = 0; y(i1) * 2 - 1; y) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; in; i) {
for (int x = 0; xi; x) {
System.out.print(" ");
}
for (int y = i; y2 * n - i - 1; y) {
System.out.print("*");
}
System.out.println();
}
}
【用java代码写一个菱形 用java代码写一个菱形代码】 public static void main(String[] args) {
p1(); // 正三角
System.out.println("------------");
p2(); // 倒三角
System.out.println("------------");
sta(5); // 倒三角
System.out.println("------------");
zf(5); // 正方形
System.out.println("------------");
lx(6); // 菱形
}
}
总有一个是你要的
如何用java编写用 "*"输出菱形?参考下面主要代码用java代码写一个菱形:
public static void draw()
{
int n=10;
for(int i=1;i=n;i)//控制行数
{
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();
}
}
用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 demo;
public class Demo01 {
public static void main(String[] args) {
int i, j, k;
for (i = 1; i = 5; i) {
for (j = 1; j = 5 - i; j)
System.out.print(" ");
for (k = 1; k = 2 * i - 1; k)
System.out.print("*");
System.out.println("");
}
for (i = 1; i = 4; i) {
for (j = 1; j = i; j)
System.out.print(" ");
for (k = 1; k = 9 - 2 * i; k)
System.out.print("*");
System.out.println("");
}
}
}
绘制算法:
1、分为两部分 , 上半部分和下半部分
2、输出空格部分换个输出*部分
3、最后一个标签需要换行
Java , 编写菱形,1.这段代码是怎么做到控制星号和空格的位置的?2.上半部分大for循环一次后是什么样的?第一: 上半部分的for循环最后一次结束后,是输出了上半部分的三角 。这个代码其实就是把菱形拆分成了两个三角形,上面的三角形和下面的三角形,分别用两个for来实现,
第二:实现这种输出图形的思路:你要把菱形看成是一个二维数组,其实就是m*n行的二维数组,只不过就是二维数组的某些元素是空格 , 而某些元素是星号* 。这样就组成了菱形 。
用java代码写一个菱形的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于用java代码写一个菱形代码、用java代码写一个菱形的信息别忘了在本站进行查找喔 。
推荐阅读
- java在哪里代码怎么写,java在哪里打代码
- 如何营销新鲜大米产品,销售大米怎么做宣传
- 角色扮演驯兽游戏,角色扮演动物
- linux命令js,Linux命令基础
- 关于netvbvcc的信息
- 网站页面如何做优化推广,网站页面如何做优化推广工作
- chatgpt有点意思的简单介绍
- linux命令搜索路径 linux中搜索
- js页面调用本地文件,js调用本地json文件