Could not print this line

I'm trying to write code where you enter an integer in the console and then the entered integer is greater, made up of letters (like ascii art).

So let the input signal 112

. Then the way out will be

   #       #     #####  
  ##      ##    #     # 
 # #     # #          # 
   #       #     #####  
   #       #    #       
   #       #    #       
 #####   #####  ####### 

      

My code will have the same result, just not on the same line :(

It will print one number below the other. From my code, you can see why:

import java.util.Scanner;
public class Tester {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String any = input.nextLine();
        String[] sArray = any.split("");

        for(int i=0; i<sArray.length; i++){
            if(sArray[i].equals("1")){
                System.out.println("  #  ");
                System.out.println(" ##  ");
                System.out.println("# #  ");
                System.out.println("  #  ");
                System.out.println("  #  ");
                System.out.println("  #  ");
                System.out.println("#####");
            }
            if(sArray[i].equals("2")){
                System.out.println(" ##### ");
                System.out.println("#     #");
                System.out.println("      #");
                System.out.println(" ##### ");
                System.out.println("#      ");
                System.out.println("#      ");
                System.out.println("#######");
            }
        }
    }
}

      

I somehow have to print everything at once, not just one output with println

as my code .. Maybe there is an easy way to solve this problem, preferably without changing all the code? I can imagine it can be done with a 2d array too, but I'm not sure. Hints are also welcome. And this is not homework.

+3


source to share


4 answers


Use String or StringBuilder to store each line and print all lines last.
Logics



import java.util.Scanner;
public class Tester {
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    String any = input.nextLine();
    String[] sArray = any.split("");
    String str1="";String str2="";String str3="";String str4="";String str5="";String str6="";String str7="";
    for(int i=0; i<sArray.length; i++){
        if(sArray[i].equals("1")){
            str1+="  #  ";
            str2+=" ##  ";
            str3+="# #  ";
            str4+="  #  ";
            str5+="  #  ";
            str6+="  #  ";
            str7+="#####";
        }
        if(sArray[i].equals("2")){
            str1+=" ##### ";
            str2+="#     #";
            str3+="      #";
            str4+=" ##### ";
            str5+="#      ";
            str6+="#      ";
            str7+="#######";
        }

    }
   System.out.println(str1);
    System.out.println(str2);
   System.out.println(str3);
 System.out.println(str4);
 System.out.println(str5);
  System.out.println(str6); System.out.println(str7);
}
}

      

+2


source


Dirty but works:

private static final Map<Integer, String[]> art = new HashMap<Integer, String[]>() {{
    put(1, new String[] {
            "   #   ",
            "  ##   ",
            " # #   ",
            "   #   ",
            "   #   ",
            "   #   ",
            " ##### " });
    put(2, new String[] {
            " ##### ",
            "#     #",
            "      #",
            " ##### ",
            "#      ",
            "#      ",
            "#######" });
    }};

public static void main(String[] args) {
    int[] input = { 1, 1, 2 };
    for (int row = 0; row < 7; row++) {
        for (int num : input) {
            System.out.print(art.get(num)[row] + " ");
        }
        System.out.println();
    }
}

      

I skipped the scanner code and accepted login 1 1 2

.



Output

   #       #     #####  
  ##      ##    #     # 
 # #     # #          # 
   #       #     #####  
   #       #    #       
   #       #    #       
 #####   #####  ####### 

      

+3


source


Recommended logic:

  • put lines, line by line, into arrays:

    private static final String[] ONE = { "  #  ",
                                          " ##  ",
                                          ... };
    
          

  • do two nested loops:

    for (int i = 0; i < heightOfPrintedDigits; i++) {
      for (String number : sArray) {
        ... //use print here but finish with an empty println("") to insert a new line
      }
    }
    
          

+2


source


Use arrays of strings to store the multi-line representation of each number, then use a map to store all the numbers. The string number can serve as a key that will return the string array representation for that line number.

final int NUM_HEIGHT = 7;
String any = "1 1 2";
String[] one = new String[] {
    "  #  ",
    " ##  ",
    "# #  ",
    "  #  ",
    "  #  ",
    "  #  ",
    "#####"};
String[] two = new String[] {
    " #####  ",
    "#     # ",
    "      # ",
    " #####  ",
    "#       ",
    "#       ",
    "####### "};
Map<String, String[]> map = new HashMap<>();
map.put("1", one);
map.put("2", two);

String[] numbers = any.split("\\s");
for (int i=0; i < NUM_HEIGHT; ++i) {
    StringBuilder line = new StringBuilder();
    for (String number : numbers) {
        line.append(map.get(number)[i]);
        line.append("  ");
    }
    System.out.println(line);
}

      

+2


source







All Articles