输入一个字符串只包含数字,“-”,“:”,空格等,在控制台输出如下图的样式。
代码:
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+=" -- ";
}
}
}