无法打印到同一行

无法打印到同一行

本文介绍了无法打印到同一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

所以假设输入是 112 。那么输出将是

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不知何故必须一次打印所有,而不是单个输出与 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.

推荐答案

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

逻辑

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

这篇关于无法打印到同一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:34