匹配数字在控制台输出'-','|'组成的数字

输入一个字符串只包含数字,“-”,“:”,空格等,在控制台输出如下图的样式。

在这里插入图片描述

代码:

import java.util.Date;

import org.junit.Test;

public class ToNumb {
	private static String s1 ="";
	private static String s2 ="";
	private static String s3 ="";
	private static String s4 ="";
	private static String s5 ="";
	public static void input(String s){
		for(int i=0;i<s.length();i++){
			switch(s.charAt(i)){
			case '0':
				n0();
				break;
			case '1':
				n1();
				break;
			case '2':
				n2();
				break;
			case '3':
				n3();
				break;
			case '4':
				n4();
				break;
			case '5':
				n5();
				break;
			case '6':
				n6();
				break;
			case '7':
				n7();
				break;
			case '8':
				n8();
				break;
			case '9':
				n9();
				break;
			case ' ':
				space();
				break;
			case ':':
				diandian();
				break;
			case '/':
				xie();
				break;
			case '-':
				gang();
				break;
			}
			
		}
		
		
	}
	private static void n0(){
		s1 += " --  ";
		s2 += "|  | ";
		s3 += "     ";
		s4 += "|  | ";
		s5 += " --  ";
	}
	private static void n1(){
		s1 += "    ";
		s2 += "|   ";
		s3 += "    ";
		s4 += "|   ";
		s5 += "    ";
	}
	private static void n2(){
		s1 += " --  ";
		s2 += "   | ";
		s3 += " --  ";
		s4 += "|    ";
		s5 += " --  ";
	}
	private static void n3(){
		s1 += " --  ";
		s2 += "   | ";
		s3 += " --  ";
		s4 += "   | ";
		s5 += " --  ";
	}
	private static void n4(){
		s1 += "     ";
		s2 += "|  | ";
		s3 += " --  ";
		s4 += "   | ";
		s5 += "     ";
	}
	private static void n5(){
		s1 += " --  ";
		s2 += "|    ";
		s3 += " --  ";
		s4 += "   | ";
		s5 += " --  ";
	}
	private static void n6(){
		s1 += " --  ";
		s2 += "|    ";
		s3 += " --  ";
		s4 += "|  | ";
		s5 += " --  ";
	}
	private static void n7(){
		s1 += " --  ";
		s2 += "   | ";
		s3 += "     ";
		s4 += "   | ";
		s5 += "     " ;
	}
	private static void n8(){
		s1 += " --  ";
		s2 += "|  | ";
		s3 += " --  ";
		s4 += "|  | ";
		s5 += " --  ";
	}
	private static void n9(){
		s1 += " --  ";
		s2 += "|  | ";
		s3 += " --  ";
		s4 += "   | ";
		s5 += " --  ";
	}
	private static void space(){
		s1 += "    ";
		s2 += "    ";
		s3 += "    ";
		s4 += "    ";
		s5 += "    ";
	}
	private static void xie(){
		s1 += "     .";
		s2 += "    . ";
		s3 += "   .  ";
		s4 += "  .   ";
		s5 += " .    ";
	}
	private static void gang(){
		s1 += "    ";
		s2 += "    ";
		s3 += " -- ";
		s4 += "    ";
		s5 += "    ";
	}
	private static void diandian(){
		s1 += "   ";
		s2 += " . ";
		s3 += "   ";
		s4 += " . ";
		s5 += "   ";
	}

	public static void  print(){
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		System.out.println(s4);
		System.out.println(s5);
	}
	
	@Test
	public void test(){
		Date date = new Date();
		System.out.println(date.toLocaleString());
		ToNumb.input(date.toLocaleString());
		ToNumb.print();
		
		
	}
	

}

代码优化:

import java.util.Date;

import org.junit.Test;

public class ToNumb {
	private static String s1 ="";
	private static String s2 ="";
	private static String s3 ="";
	private static String s4 ="";
	private static String s5 ="";
	
	public static void input(String s){
		for(int i=0;i<s.length();i++){
			String str = Character.toString(s.charAt(i));
			switch(s.charAt(i)){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				num(Integer.parseInt(str));
				break;
			case ' ':
				space();
				break;
			case ':':
				diandian();
				break;
			case '/':
				xie();
				break;
			case '-':
				gang();
				break;
			}
			
		}	
	}


	private static void xie(){
		s1 += "     .";
		s2 += "    . ";
		s3 += "   .  ";
		s4 += "  .   ";
		s5 += " .    ";
	}
	private static void gang(){
		s1 += "    ";
		s2 += "    ";
		s3 += " -- ";
		s4 += "    ";
		s5 += "    ";
	}
	private static void diandian(){
		s1 += "   ";
		s2 += " . ";
		s3 += "   ";
		s4 += " . ";
		s5 += "   ";
	}

	public static void  print(){
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		System.out.println(s4);
		System.out.println(s5);
	}
	public static void num(int n){
		
		if(n==1||n==4){
			s1+="     ";
		}else{
			s1+=" --  ";
		}
		if(n==0||n==4||n==8||n==9){
			s2+="|  | ";
		}else if(n==1||n==5||n==6){
			s2+="|    ";
		}else{
			s2+="   | ";
		}
		if(n==0||n==1||n==7){
			s3+="     ";
		}else {
			s3+=" --  ";
		}
		if(n==0||n==6||n==8){
			s4+="|  | ";
		}else if(n==1||n==2){
			s4+="|    ";
		}else{
			s4+="   | ";
		}
		if(n==1||n==4||n==7){
			s5+="     ";
		}else{
			s5+=" --  ";
		}
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值