Java if-then 语句和整数的字符串问题

Java string issue with if-then statement and ints

我正在尝试将当前时间放入一个 L.E.D.-like 输出中,并且每次都能够手动生成;但我很难自动显示当前时间。

在当前输出中,我有四个单独字符串的时间和 L.E.D 中的时间输出。格式:

0
-7
5
6
  _  
 | | 
 |_| 
      __    .        __  
   |  __|       |_|  __| 
   | |__    .     |  __| 

我下面的代码解释得更好。抱歉造成混淆。

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class main {


/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //get current hour
    DateFormat dateFormat = new SimpleDateFormat("HH");
    Date date = new Date();     
    String hour = dateFormat.format(date); 
    int hr = Integer.parseInt(hour);

    //Separate hour into two int's
    int f = hr-12;
    int y = f / 10;
    int u = f % 10;

    //get current minutes
    DateFormat dfm = new SimpleDateFormat("mm");
    Date dm = new Date();
    String min = dfm.format(dm); 
    int mn = Integer.parseInt(min);

    //Separate minutes into two int's
    int a = mn;
    int i = a / 10;
    int o = a % 10;

    //print the time respectively
    System.out.println(y);
    System.out.println(u);
    System.out.println(i);
    System.out.println(o);


    //characters to write the numbers
    String text = "  .  ";
    String t = "_";
    String l = "|";
    String sp = " ";
    String br = "\n";

    //top middle and bottom characters

    String t0 = sp+sp+t+sp+sp;
    String m0 = sp+l+sp+l+sp;
    String b0 = sp+l+t+l+sp;

    //Ex: System.out.println(t0+br+m0+br+b0); would print the number 0 in led output
    System.out.println(t0+br+m0+br+b0);


    String t1 = sp+sp+sp+sp+sp;
    String m1 = sp+sp+sp+l+sp;
    String b1 = sp+sp+sp+l+sp;

    String t2 = sp+t+t+sp+sp;
    String m2 = sp+t+t+l+sp;
    String b2 = l+t+t+sp+sp;

    String t3 = sp+t+t+sp+sp;
    String m3 = sp+t+t+l+sp;
    String b3 = sp+t+t+l+sp;

    String t4 = sp+sp+sp+sp+sp;
    String m4 = sp+l+t+l+sp;
    String b4 = sp+sp+sp+l+sp;

    String t5 = sp+sp+t+t+sp;
    String m5 = sp+l+t+sp+sp;
    String b5 = sp+t+t+l+sp;

    String t6 = sp+sp+t+sp+sp;
    String m6 = sp+l+t+sp+sp;
    String b6 = sp+l+t+l+sp;

    String t7 = sp+t+t+sp+sp;
    String m7 = sp+sp+sp+l+sp;
    String b7 = sp+sp+sp+l+sp;

    String t8 = sp+sp+t+sp+sp;
    String m8 = sp+l+t+l+sp;
    String b8 = sp+l+t+l+sp;

    String t9 = sp+sp+t+sp+sp;
    String m9 = sp+l+t+l+sp;
    String b9 = sp+sp+sp+l+sp;


    // My attempt to change the int's from the current date into variables for their output

    if (y == 0) {
        String a1 = t0;
        String a2 = m0;
        String a3 = b0;

    } else if (y == 1) {
        String a1 = t1;
        String a2 = m1;
        String a3 = b1;

    } else if (y == 2) {
        String a1 = t2;
        String a2 = m2;
        String a3 = b2;

    } else if (y == 3) {
        String a1 = t3;
        String a2 = m3;
        String a3 = b3;

    } else if (y == 4) {
        String a1 = t4;
        String a2 = m4;
        String a3 = b4;

    } else if (y == 5) {
        String a1 = t5;
        String a2 = m5;
        String a3 = b5;

    } else if (y == 6) {
        String a1 = t6;
        String a2 = m6;
        String a3 = b6;

    } else if (y == 7) {
        String a1 = t7;
        String a2 = m7;
        String a3 = b7;

    } else if (y == 8) {
        String a1 = t8;
        String a2 = m8;
        String a3 = b8;

    } else if (y == 9) {
        String a1 = t9;
        String a2 = m9;
        String a3 = b9;

    } else {
        String too = "F";
    }

    if (u == 0) {
        String x1 = t0;
        String x2 = m0;
        String x3 = b0;

    } else if (u == 1) {
        String x1 = t1;
        String x2 = m1;
        String x3 = b1;

    } else if (u == 2) {
        String x1 = t2;
        String x2 = m2;
        String x3 = b2;

    } else if (u == 3) {
        String x1 = t3;
        String x2 = m3;
        String x3 = b3;

    } else if (u == 4) {
        String x1 = t4;
        String x2 = m4;
        String x3 = b4;

    } else if (u == 5) {
        String x1 = t5;
        String x2 = m5;
        String x3 = b5;

    } else if (u == 6) {
        String x1 = t6;
        String x2 = m6;
        String x3 = b6;

    } else if (u == 7) {
        String x1 = t7;
        String x2 = m7;
        String x3 = b7;

    } else if (u == 8) {
        String x1 = t8;
        String x2 = m8;
        String x3 = b8;

    } else if (u == 9) {
        String x1 = t9;
        String x2 = m9;
        String x3 = b9;

    } else {
        String x1 = t9;
        String x2 = m9;
        String x3 = b9;
    }


    //The set up to print 12:43 in led format
    //i would like to remove this so i can display the current time but i get errors
    String a1 = t1;
    String x1 = t2;
    String c1 = text;
    String d1 = t4;
    String e1 = t3;

    String a2 = m1;
    String x2 = m2;
    String c2 = "     ";
    String d2 = m4;
    String e2 = m3;

    String a3 = b1;
    String x3 = b2;
    String c3 = text;
    String d3 = b4;
    String e3 = b3;


    //using the setup to display the time
    String time= a1+x1+c1+d1+e1+br+a2+x2+c2+d2+e2+br+a3+x3+c3+d3+e3;

    //printing the time
    System.out.println(time);
}

}

一个更易读的版本如何做同样的事情(想想写一个通读你的代码的版本会更容易:)...

    String s = " ";
    String[][] d = new String[][] {
        new String[] {" __ ", "    ", " __ ", " __ ", "    ", " __ ", " __ ", " __ ", " __ ", " __ ", "  . "},
        new String[] {"|  |", "  | ", " __|", " __|", " |_|", "|__ ", "|__ ", "  / ", "|__|", "|__|", "    "},
        new String[] {"|__|", "  | ", "|__ ", " __|", "   |", " __|", "|__|", " /  ", "|__|", " __|", "  . "}
    };
    int hour = 12;
    int min = 7;
    System.out.println(
        d[0][hour/10] + s + d[0][hour%10] + s + d[0][10] + s + d[0][min/10] + s + d[0][min%10] + "\n" +
        d[1][hour/10] + s + d[1][hour%10] + s + d[1][10] + s + d[1][min/10] + s + d[1][min%10] + "\n" +
        d[2][hour/10] + s + d[2][hour%10] + s + d[2][10] + s + d[2][min/10] + s + d[2][min%10] + "\n"
        );

产生:

    __    .        __ 
|   __|       |_| |__|
|  |__    .     |  __|