无法打印到同一行

时间:2017-04-12 09:55:07

标签: java printing output

我尝试编写一个代码,在控制台中输入一个整数,然后输入的整数显示更大,由字母组成(如ascii art)。

所以,让我们说输入是112。然后输出

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

我的代码将具有相同的输出,只是不在同一行:(

它会在另一个下打印一个数字。从我的代码中你可以看到原因:

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

我不得不一次打印所有内容,而不是单println输出作为我的代码.. 也许有一种简单的方法可以解决这个问题,最好不要改变我的整个代码?我可以想象它也可以用二维数组完成,但不确定。提示也非常受欢迎。这不是功课。

4 个答案:

答案 0 :(得分:3)

肮脏但有效:

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

我跳过扫描程序代码并假设输入1 1 2

<强>输出

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

答案 1 :(得分:2)

使用String或StringBuilder存储每一行​​,最后打印所有字符串 逻辑

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 :(得分:2)

建议逻辑:

  • 逐行将字符串放在数组中:

    private static final String[] ONE = { "  #  ",
                                          " ##  ",
                                          ... };
    
  • 运行两个嵌套for循环:

    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
      }
    }
    

答案 3 :(得分:2)

使用字符串数组存储每个数字的多行表示,然后使用地图存储所有数字。字符串编号可以作为键,它将返回该字符串编号的字符串数组表示。

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