iT邦幫忙

0

ITSA Basic 題目1 矩陣數字顯示 (Java)

  • 分享至 

  • xImage
  •  

題目:矩陣數字顯示
說明:輸入四位數數字,顯示出用星星排列的電子數字

一開始先設定0-9十個數字的星星排列
外迴圈跑五次是因為一個數字有五行(橫的),要印五次
內迴圈跑四次是因為總共會輸入四個數字

所以列印的順序是
第一個數字的第1排顯示 -> 第二個數字的第1排顯示 -> 第三個數字的第1排顯示 -> 第四個數字的第1排顯示
第一個數字的第2排顯示 -> 第二個數字的第2排顯示 -> 第三個數字的第2排顯示 -> 第四個數字的第2排顯示
第一個數字的第3排顯示 -> 第二個數字的第3排顯示 -> 第三個數字的第3排顯示 -> 第四個數字的第3排顯示
第一個數字的第4排顯示 -> 第二個數字的第4排顯示 -> 第三個數字的第4排顯示 -> 第四個數字的第4排顯示
第一個數字的第5排顯示 -> 第二個數字的第5排顯示 -> 第三個數字的第5排顯示 -> 第四個數字的第5排顯示

然後因為輸入的是數字,我們要把這一個四位數數字拆成『四個數字』
方法是用除以 1000 / 100 / 10 / 1 可以取到 千分位 / 百分位 / 十分位 / 個位
例如:
1234 除 1000 得 商:1 餘:234
再拿餘數 234 去除 100
234 除 100 得 商:2 餘:34
再拿餘數 34 去除 10
34 除 10 得 商:3 餘:4
再拿餘數 4 去除 1
4 除 1 得 商:4 餘:0


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		int b = input;
		String number[][][] = {
				{{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*"," "," "," ","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"}}
				,{{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}
				,{{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"},{"*"," "," "," "," "},{"*","*","*","*","*"}}
				,{{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"}}
				,{{"*"," "," "," ","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}
				,{{"*","*","*","*","*"},{"*"," "," "," "," "},{"*","*","*","*","*"},{" "," "," "," ","*"},{"*","*","*","*","*"}}
				,{{"*"," "," "," "," "},{"*"," "," "," "," "},{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"}}
				,{{"*","*","*","*","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}
				,{{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"}}
				,{{"*","*","*","*","*"},{"*"," "," "," ","*"},{"*","*","*","*","*"},{" "," "," "," ","*"},{" "," "," "," ","*"}}
		};
		for(int i=0; i<5; i++) {
			for(int j=3; j>=0; j--) {
				int num = input;
				num = num / (int)(Math.pow(10, j));
				input = input % (int)(Math.pow(10, j));
				for(String s : number[num][i]) {
					System.out.print(s);
				}
				if(j != 0) System.out.print(" ");
			}
			System.out.println();
			input = b;
		}
	}

}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言