Java prints various diamond and java prints Diamond

Source: Internet
Author: User

Java prints various diamond and java prints Diamond

/*** Class description ** @ author was built * @ version Creation Time: 09:50:46 */public class Diamond {public static void getDia (int n) {System. out. println ("First:"); for (int I = 0; I <n/2 + 1; I ++) {for (int j2 = 0; j2 <n/2 + 1-i; j2 ++) {System. out. print ("") ;}for (int j2 = 0; j2 <2 * I + 1; j2 ++) {System. out. print ("+");} System. out. println () ;}for (int I = n/2; I> 0; I --) {for (int j = 0; j <n/2-i + 2; j ++) {System. out. Print ("");} for (int j = 0; j <2 * I-1; j ++) {System. out. print ("+");} System. out. println ();} System. out. println ("Second:"); for (int I = 1; I <= n; I ++) {if (I <= n/2 + 1) {for (int k = 1; k <= n/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-(n/2 + 1); k ++) {System. out. print ("") ;}for (int k = 1; k <= (2*(n/2 + 1)-I); k ++) {System. out. print ("+");} System. out. println () ;}} System. out. println ("Third:"); for (int k = 1; k <= n/2 + 1; k ++) {for (int I = 1; I <= (n/2 + 1-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 = n/2; k> = 1; k --) {for (int I = 1; I <= (n/2 + 1-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 () ;}} public static void main (String [] args) {getDia (9 );}}

The result is as follows:

First:
+
++
++
++
++
++
++
++
+
Second:
+
++
++
++
++
++
++
++
+
Third:
*
**
**
**
**
**
**
**
*


Print diamond with java

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

Java print efficient and concise code with diamond

Public static void main (String args []) {
Java. util. ipvsc = new java. util. Digest (System. in );
System. out. println ("enter an odd number ");
Int line = SC. nextInt ();
While (line % 2 = 0 | line <0 ){
System. out. println ("enter an odd number again ");
Line = SC. nextInt ();
}
Print (1, line );
}

Public static void print (int n, int line ){
If (line % 2 = 0) return;
If (n> = line ){
For (int I = 0; I <n; I ++ ){
System. out. print ("*");
}
System. out. println ();
Return;
}
For (int I = 0; I <(line-n)/2.0; I ++ ){
System. out. print ("");
}
For (int I = 0; I <n; I ++ ){
System. out. print ("*");
}
System. out. println ();
Print (n + 2, line );
For (int I = 0; I <(line-n)/2.0; I ++ ){
System. out. print ("");
}
For (int I = 0; I <n; I ++ ){
System. out. print ("*");
}
System. out. println ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.