Having trouble printing to same line

2020-04-13 11:07发布

问题:

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

So let's say the input is 112. Then the output will be

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

My code will have the same output, just not in the same line :(

It will print one number under 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 all at once, not single output with println as my code.. Maybe there is an easy way to solve that, preferably without changing my entire code? I can imagine it could be done with a 2d array as well, but not sure. Hints are very welcome too. And this is no homework.

回答1:

Use String or StringBuilder to store each line and last print all strings.
Logic

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:

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 assumed an input of 1 1 2.

Output

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


回答3:

Suggested logic:

  • place the strings, line by line, in arrays:

    private static final String[] ONE = { "  #  ",
                                          " ##  ",
                                          ... };
    
  • run two nested for 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
      }
    }
    


回答4:

Use string arrays to store the multi-line representation of each number, and then use a map to store all the numbers. A string number can serve as the key, which would return a string array representation for that string 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);
}