刚学了java的if和for,写了个打印菱形的代码,感觉就打印一个菱形 , 要不要写这么多代码呀,还能优化不?代码如下:
public class App {
public static void main(String[] args) {
int n = 9;
for (int i = 0; in; i) {
int m = Math.abs((n / 2) - i);
for (int j = 0; jm; j) {
System.out.print(" ");
}
【java菱形代码 java菱形语法】m = n - Math.abs((n / 2) - i) * 2;
for (int j = 0; jm; j) {
System.out.print("*");
}
System.out.println();
}
}
}
运行结果:
代码如下:
public class App {
public static void main(String[] args) {
int n = 9;
for (int i = 0; in; i) {
int m = Math.abs((n / 2) - i);// 计算每行前的空格数量
for (int j = 0; jm; j) {
System.out.print(" ");
}
m = n - Math.abs((n / 2) - i) * 2; // 计算每行需要打印的*号
for (int j = 0; jm; j) {
if (j == 0 || j == (m - 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
运行结果:
输入一个整数N,打印出有N*2-1行的菱形,java代码import java.util.Scanner;
public class test{
public static void main(String[] args) {
System.out.print("请输入一个整数:");
int num = new Scanner(System.in).nextInt();
num=num*2-1;
for(int i=1;i=num;i)
{
for(int j=1;j=num-i;j)
System.out.print(" ");
for(int k=1;k=2*i-1;k)
System.out.print('*');
System.out.println();
}
for(int i=num-1;i0;i--)
{
for(int j=1;j=num-i;j)
System.out.print(" ");
for(int k=1;k=2*i-1;k)
System.out.print('*');
System.out.println();
}
}
}
用java编写菱形for(i=1;i=4;i)
{
这个小java菱形代码的for是打印每一行前边java菱形代码的空格
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)
代码java菱形代码: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 for循环输出打印菱形 详细的步骤注释 求指教*
***
*****
*******
*********
*******
*****
***
*
方法很多java菱形代码 , 可以i=0开始的!java菱形代码我现在是 从1开始
public class Zuoye {
public static void main(String[] args) {
//首先把菱形看成上下,上五下四,所以第一个for有5次,第二个for4次
for(int i=1;i=5;i)
{
//将空格和*分开看,看" "的变化i=1时,他是4 , 2的时候是3找规律
for(int j=1;j=5-i;j)
System.out.print(" ");
for(int k=1;k=2*i-1;k)//找规律,i是 1 3 5 7 基数嘛
System.out.print('*');
//换一行
System.out.println();
}
for(int i=1;i=4;i)
{
for(int j=1;j=i;j)//空格 1 2 3 4 so
System.out.print(" ");
for(int k=7;k=2*i-1;k--)//* 7 5 3 1倒着来的基数
System.out.print('*');
System.out.println();
}
}
}
希望你可以举一反三,能打印活的菱形
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; isize / 21; i) {
for (int j = size / 21; ji1; j--) {
System.out.print("");// 输出左上角位置的空白
}
for (int j = 0; j2 * i1; j) {
if (j == 0 || j == 2 * i) {
System.out.print("* ");// 输出菱形上半部边缘
} else {
System.out.print("");// 输出菱形上半部空心
}
}
System.out.println("");//换行
}
for (int i = size / 21; isize; i) {
for (int j = 0; ji - size / 2; j) {
System.out.print("");// 输出菱形左下角空白
}
for (int j = 0; j2 * size - 1 - 2 * i; j) {
if (j == 0 || j == 2 * (size - i - 1)) {
System.out.print("* ");// 输出菱形下半部边缘
} else {
System.out.print("");// 输出菱形下半部空心
}
}
System.out.println("");//换行
}
}
}
效果如图:
java中如何用星号如何输出个空心的菱形import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
int length=1;//存储菱形的边长的变量
Scanner sc =new Scanner(System.in);//输入数据的扫描对象
System.out.println("请输入菱形的边长:");
length = sc.nextInt();//获取输入的边长数值
/*打印菱形的上半部分三角形 行数为length行*/
for (int i=1; i=length;i) {//需要打印的行数
for(int j=1;j=length-i;j){//打印左边空白的区域,随行递减1
System.out.print(" ");
}
for(int k=1;k=2*i-1;k){//打三角形的区域
if(k==1||k==2*i-1){//空心的获取 除每行第一个以及最后一个 外其余为空白
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
/*打印菱形的下半部分三角形中间行公用 故行数为length-1*/
for (int i = length-1;i0;i--){
for(int j=1;j=length-i;j){
System.out.print(" ");
}
for(int k=1;k=2*i-1;k){
if(k==1||k==2*i-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
java菱形代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java菱形语法、java菱形代码的信息别忘了在本站进行查找喔 。
推荐阅读
- 抖音直播带货用运营吗,抖音直播带货需要什么设备,条件
- ios10怎么ios12正式版,苹果102系统怎么升级到12
- 华为鸿蒙原充数据线多少钱,鸿蒙 充电器
- 磐安sap产品成本计算,sap成本核算流程图
- python函数seek python函数self关键字
- 新媒体实习经历如何量化,新媒体实践经历
- 游戏控制开发,控制游戏操作说明
- 拍摄背景题材是什么,拍摄背景布置
- go语言sum文件 go语言代码文件后缀