Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Mirrored Number Bowtie ASCII Art using loops

Hello. I am new to Java and I am trying to make ASCII art using loops.

This is the shape I am trying to do:

Code
1           1
12         21
123       321
1234     4321
12345   54321
123456 654321
12345   54321
1234     4321
123       321
12         21
1           1

This is my code I am using so far:

Code
public class Bowtie {
   public static void main(String[] args)
   {
    for (int i = 0; i < 7; i++) { //this is the first loop. it will go 5 times and increase by 1 each time
           
           for (int k = 1; k <= i; k++) { //prints out numbers
               System.out.print(k);
}

           for (int j = 0; j < i; j++) { //attempting to print spaces
               System.out.print(" ");
           }
           System.out.println();
           }
           
    for (int main = 1; main < 6; main++) { //this is the lower loop. it will go 5 times and increase by 1 each time
           for (int x = 5; x < main; x++)
           
            System.out.print(main - 1);

           System.out.println();
}

}
}

My code prints this:
Code
1
12
123
1234
12345
123456

Anybody think they can complete this? I am exhausted here. I would greatly appreciate it!

Thanks.
PHP Code

<?php 
private static final int max = 6;
 public static 
void main(String[] args) {
 for (
int i = 1; i < max; i++) {
 print(
i);
 }
 for (
int i = max; i > 0; i--) {
 print(
i);
 }
 } 

I wish I had something better to do with my time...

Version 1: [Image: ysilye.jpg]
PHP Code

<?php 
private static void print(int i){
 
StringBuilder sb = new StringBuilder();
 for (
int k = 1; k <= i; k++) {
 
sb.append(k);
 }
 
String a = sb.toString();
 
String b = sb.reverse().toString();
 
System.out.println(String.format("%-"+max+"s " + "%"+max+"s", a,b));
 } 

Version 2: [Image: xiuavr.jpg]
PHP Code

<?php 
private static void print(int i){
 for (
int k = 1; k <= i; k++) {
 
System.out.print(k);
 }
 for (
int k = 0; k <= max*2 - i*2; k++) {
 
System.out.print(" ");
 }
 for (
int k = i; k > 0; k--) {
 
System.out.print(k);
 }
 
System.out.println();
 } 

Version 3: [Image: tjpazx.jpg]
PHP Code

<?php 
private static final String s = "123456654321";
 private static 
void print(int i){
 
System.out.print(s.substring(0,i));
 for (
int k = 0; k <= max*2 - i*2; k++) {
 
System.out.print(" ");
 }
 
System.out.println(s.substring(s.length() - i,s.length()));
 } 

Version 4: [Image: ypkpku.jpg]
PHP Code

<?php 
private static void print(int i){
 
String s = 
 
 i == 1 ? "1           1" 
 
: i == 2 ? "12         21"
 
: i == 3 ? "123       321"
 
: i == 4 ? "1234     4321"
 
: i == 5 ? "12345   54321"
 
: i == 6 ? "123456 654321"
 
: "";
 
System.out.println(s);
 } 

Version 5:[Image: jdieqw.jpg]
PHP Code

<?php 
private static void print(int i){
 
boolean flag = i < (i = (int) Math.abs(i));
 
int j = 1 + (max - i);
 if (
flag){
 while (
j > 0) { System.out.print(" "); j--; }
 switch (
i) {
 case 
6: System.out.print("6");
 case 
5: System.out.print("5");
 case 
4: System.out.print("4");
 case 
3: System.out.print("3");
 case 
2: System.out.print("2");
 case 
1: System.out.print("1"); 
 }
 
System.out.println();
 } else {
 switch (
i) {
 case 
6: System.out.print("6"); 
 case 
5: System.out.print("5");
 case 
4: System.out.print("4");
 case 
3: System.out.print("3");
 case 
2: System.out.print("2");
 case 
1: System.out.print("1");
 }
 while (
j > 0) { System.out.print(" "); j--; }
 print(-
i);
 }
 } 

Version 6: [Image: pbxjmw.jpg]
PHP Code

<?php 
private static void print(int i) {
 
System.out.print(new Random(
 
i == 1 ? 994376  :
 
i == 2 ? 470256  :
 
i == 3 ? 55550   :
 
i == 4 ? -315108 :
 
i == 5 ? -903398 :
 
i == 6 ? 410690  : 0
 
).nextInt(754321));
 
int j = 1 + (max - i)*2;
 while (
j > 0) { System.out.print(" "); j--; }
 
System.out.println(new Random(
 
i == 1 ? 994376 :
 
i == 2 ? 352081 :
 
i == 3 ? 495113 :
 
i == 4 ? 542577 :
 
i == 5 ? 886230 :
 
i == 6 ? 841059 : 0
 
).nextInt(754321));
 } 
(Mar 3, 2018 02:51 AM)GenericSkid Wrote:  
PHP Code

<?php 
private static final int max = 6;
 public static 
void main(String[] args) {
 for (
int i = 1; i < max; i++) {
 print(
i);
 }
 for (
int i = max; i > 0; i--) {
 print(
i);
 }
 } 

I wish I had something better to do with my time...

Thank you so much, I will try to learn what you did here. I appreciate it a lot, I'll be back if I have more problems!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by n00b.caApr 4, 2020 12:39 PM 3 201 Apr 4, 2020 12:39 PM
Last Post: n00b.ca
[C#] by Fun VidosJan 27, 2020 02:36 AM 2 181 Jan 27, 2020 02:36 AM
Last Post: Fun Vidos
[Java] by YukinoNov 12, 2019 05:50 PM 6 431 Nov 12, 2019 05:50 PM
Last Post: Accord