然後建立陣列
char[][] conversionTable = {
/* 0 1 2 3 4 5 6 */
/* 0 */{'-','|','|',' ','|','|','-'},
/* 1 */{' ','|',' ',' ','|',' ',' '},
/* 2 */{'-','|',' ','-',' ','|','-'},
/* 3 */{'-','|',' ','-','|',' ','-'},
/* 4 */{' ','|','|','-','|',' ',' '},
/* 5 */{'-',' ','|','-','|',' ','-'},
/* 6 */{'-',' ','|','-','|','|','-'},
/* 7 */{'-','|',' ',' ','|',' ',' '},
/* 8 */{'-','|','|','-','|','|','-'},
/* 9 */{'-','|','|','-','|',' ','-'},
};
row分別代表1~9的數字,而0~6就是我們的約定位置。
import java.util.Scanner;
public class JAVA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] conversionTable = {
/* 0 1 2 3 4 5 6 */
/* 0 */{'-','|','|',' ','|','|','-'},
/* 1 */{' ','|',' ',' ','|',' ',' '},
/* 2 */{'-','|',' ','-',' ','|','-'},
/* 3 */{'-','|',' ','-','|',' ','-'},
/* 4 */{' ','|','|','-','|',' ',' '},
/* 5 */{'-',' ','|','-','|',' ','-'},
/* 6 */{'-',' ','|','-','|','|','-'},
/* 7 */{'-','|',' ',' ','|',' ',' '},
/* 8 */{'-','|','|','-','|','|','-'},
/* 9 */{'-','|','|','-','|',' ','-'},
};
int i, j, k;
while(sc.hasNext()) {
int s = sc.nextInt();
if(s == 0)
break;
//String digitNumber = readToken();
String digitNumber = sc.next();
int n = digitNumber.length();
int digit;
// display digitNumber in s size
// Each digit occupies exactly s+2 columns and 2s+3 rows.
for(i = 0; i < 2*s+3; i++) {
for(j = 0; j < n; j++) {
digit = digitNumber.charAt(j) - '0';
/* upper, middle and lower parts */
if ((i % (s + 1)) == 0) {
System.out.printf(" ");
for (k = 0; k < s; k++) {
System.out.printf("%c", conversionTable[digit][(i / (s + 1)) * 3]);
}
System.out.printf(" ");
}
/* between upper and middle parts */
if (i > 0 && i < (s + 1)) {
System.out.printf("%c", conversionTable[digit][2]);
for (k = 0; k < s; k++) {
System.out.printf(" ");
}
System.out.printf("%c", conversionTable[digit][1]);
}
/* between middle and lower parts */
if (i > (s + 1) && i < (2*s + 2)) {
System.out.printf("%c", conversionTable[digit][5]);
for (k = 0; k < s; k++) {
System.out.printf(" ");
}
System.out.printf("%c", conversionTable[digit][4]);
}
/* if not the last number */
if (j != n-1)
System.out.printf(" ");
}
System.out.println();
}
System.out.println();
}
}
}
我們注意到幾個地方: - 每一個數字都佔有2s+3列跟s+2行(s代表輸入的大小)
- 第一部份是正中間的上中下 i % (s + 1) == 0
- 第二部份是上跟中的中間 s+1
- 第三部份是中跟下的中間 2s+2
- 整個處理分成三個部份
可以參考下圖~會更瞭解為什麼運算式這樣判斷
參考資料:http://snippets.dzone.com/posts/show/5244
No comments:
Post a Comment