我正在嘗試將帶有 # 字符繪圖表示的數字列印到控制臺。但是,例如,當我嘗試輸入兩個數字時,它們會列印在彼此下方而不是彼此相鄰的位置。
static void print(int mat[][])
{
for (int i = 0; i < 5; i ) {
for (int j = 0; j < 5; j ) {
if (i % 2 == 0) {
if (mat[i][j] == 1)
System.out.print("#");
else
System.out.print(" ");
}
else {
if (mat[i][j] == 1)
System.out.print("#");
else
System.out.print(" ");
}
}
System.out.println();
}
}
5位數字的矩陣定義如下:
int mat[][] = { { 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 1 },
{ 1, 1, 0, 1, 1 } };
下面的螢屏截圖是我嘗試將 55 輸入到我的程式時的輸出(預期輸出是將兩個數字彼此相鄰繪制,并且它們之間有一個空格):

uj5u.com熱心網友回復:
public class Main {
public static void main(String... args) {
print("0123456789");
}
private static final String[][] DIGITS = {
{ "#####", " #", "#####", "#####", "# #", "#####", "# ", "#####", "#####", "#####" },
{ "# #", " # #", " #", " #", "# #", "# ", "# ", " #", "# #", "# #" },
{ "# #", "# #", "#####", "#####", "#####", "#####", "#####", " #", "#####", "#####" },
{ "# #", " #", "# ", " #", " #", " #", "# #", " #", "# #", " #" },
{ "#####", " #", "#####", "#####", " #", "#####", "#####", " #", "#####", " #" } };
public static void print(String val) {
for (int row = 0; row < DIGITS.length; row ) {
for (int i = 0; i < val.length(); i ) {
if (i > 0)
System.out.print(' ');
int digit = Character.getNumericValue(val.charAt(i));
System.out.print(DIGITS[row][digit]);
}
System.out.println();
}
System.out.println();
}
}
輸出:
##### # ##### ##### # # ##### # ##### ##### #####
# # # # # # # # # # # # # # #
# # # # ##### ##### ##### ##### ##### # ##### #####
# # # # # # # # # # # # #
##### # ##### ##### # ##### ##### # ##### #
uj5u.com熱心網友回復:
這是執行您想要執行的操作的代碼。但我認為應該有另一種方法來獲得更通用的方法。
static void matPrint(int mat[][])
{
int n = 10 ; // How many times you want to print side by side?
for (int i = 0; i < 5; i ) {
for (int j = 0; j < (mat[0].length*n) (n-1); j ) {
if(j%6==5 && j!=0) {
System.out.print(" ");
continue ;
}
if (i % 2 == 0) {
if (mat[i][j%(mat[0].length 1)] == 1)
System.out.print("#");
else
System.out.print(" ");
}
else {
if (mat[i][j%(mat[0].length 1)] == 1)
System.out.print("#");
else
System.out.print(" ");
}
}
System.out.println();
}
}
uj5u.com熱心網友回復:
我建議使用 3d 陣列。
int mat[][][] =
{
{ //insert 0th pattern and followed by 1 to 9.
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 1 },
{ 1, 1, 0, 1, 1 }
},
{
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 1 },
{ 1, 1, 0, 1, 1 }
}
};
int num = // user input;
print(mat,num);
static void print(int mat[][][],int num)
{
int len = Integer.toString(num).length();
int arr[] = new int[len];
for(int i=len-1;i>=0;i--)
{
arr[i] = num%10;
num = num/10;
}
// above are for splitting the num into array.
// 5483 to {5,4,8,3}
for (int i = 0; i < 5; i ) {
for(int k = 0;k < len;k ){
// this for-loop is for iterating one row of all the numbers
// 1st row of 5, 4, 8, 3 and then 2nd row and so on.
for (int j = 0; j < 5; j ) {
if (mat[arr[k]][i][j] == 1)
System.out.print("#");
else
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318420.html
標籤:爪哇
下一篇:方法update(Integer,Integer)對于CircularLinkedList<Integer>型別是不明確的
