`

Java使用字符模拟5段显示器(火柴棍)的数字

    博客分类:
  • JAVA
阅读更多

问题:

question

 

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class MatchNumberGenerator {
	public static int[] matchNumberGenerator(int a){
		int result[] = null;
		switch(a){
		//"_"-->2 " "-->0 "|"-->1
		case 0:
			result = new int[]{0,2,0,1,0,1,1,0,1,1,0,1,1,2,1};
			//"' _ '| |'| |'| |'|_|".split(",");
			break;
		case 1:
			result = new int[]{0,0,0,0,0,1,0,0,1,0,0,1,0,0,1};
			//"'   '  |'  |'  |'  |".split(",");
			break;
		case 2:
			result = new int[]{0,2,0,0,0,1,0,2,1,1,0,0,1,2,0};
			//"' _ '  |' _|'|  '|_ ".split(",");
			break;
		case 3:
			result = new int[]{0,2,0,0,0,1,0,2,1,0,0,1,0,2,1};
			//"' _ '  |' _|'  |' _|".split(",");
			break;
		case 4:
			result = new int[]{0,0,0,1,0,1,1,2,1,0,0,1,0,0,1};
			//"'   '| |'|_|'  |'  |".split(",");
			break;
		case 5:
			result = new int[]{0,2,0,1,0,0,1,2,0,0,0,1,0,2,1};
			//"' _ '|  '|_ '  |' _|".split(",");
			break;
		case 6:
			result = new int[]{0,2,0,1,0,0,1,2,0,1,0,1,1,2,1};
			//"' _ '|  '|_ '| |'|_|".split(",");
			break;
		case 7:
			result = new int[]{0,2,0,0,0,1,0,0,1,0,0,1,0,0,1};
			//"' _ '  |'  |'  |'  |".split(",");
			break;
		case 8:
			result = new int[]{0,2,0,1,0,1,1,2,1,1,0,1,1,2,1};
			//"' _ '| |'|_|'| |'|_|".split(",");
			break;
		case 9:
			result = new int[]{0,2,0,1,0,1,1,2,1,0,0,1,0,2,1};
			//"' _ '| |'|_|'  |' _|".split(",");
			break;
		}
		return result;
	}

	public static void printNumber(int[] numberArray, int arrayIndex, int arrayIndexEnd){
		for(int i = arrayIndex; i < arrayIndexEnd;i++){
			//System.out.print(numberArray[i]);
			switch(numberArray[i]){
			//"_"-->2 " "-->0 "|"-->1
			case 0:
				System.out.print(" ");
				break;
			case 1:
				System.out.print("|");
				break;
			case 2:
				System.out.print("_");
				break;
			}
		}
		System.out.print(" ");
	}
	
	public static boolean isNumber(String args) {
		  
		  return args.matches("-?\\d+\\.?\\d*");
		    
		 }
	public static void main(String[] args) {
		System.out.println("Please enter the number:");
		Scanner sc = new Scanner(System.in);
		String[] inputString = sc.nextLine().split("");
		//System.out.println(inputString);
		List<int[]> inputIntList = new LinkedList<int[]>();
		for(String temp: inputString){
			if(isNumber(temp))
				inputIntList.add(matchNumberGenerator(Integer.parseInt(temp)));
		}
		//System.out.println(inputIntList);
		Iterator<int[]> iterator = null;
		for(int i = 0; i < 5; i++){
			iterator = inputIntList.iterator();
			while(iterator.hasNext()){
				printNumber(iterator.next(),i*3,(i+1)*3);
			}
			System.out.println();
		}

	}

}
 

输出

输出

 

  • 大小: 21.3 KB
  • 大小: 7.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics