Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
if-then语句和int的Java字符串问题_Java_String_If Statement_Int - Fatal编程技术网

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

if-then语句和int的Java字符串问题,java,string,if-statement,int,Java,String,If Statement,Int,我试图将当前时间输入到类似L.E.D.的输出中,并且每次都能够手动生成;但是我很难自动显示当前时间 在当前输出中,我有四个单独字符串的时间和一个L.E.D.格式的时间输出: 0 -7 5 6 _ | | |_| __ . __ | __| |_| __| | |__ . | __| 下面的代码解释得更好。很抱歉给你带来了困惑 import java.util.Calendar; import j

我试图将当前时间输入到类似L.E.D.的输出中,并且每次都能够手动生成;但是我很难自动显示当前时间

在当前输出中,我有四个单独字符串的时间和一个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);
}

}

一个可读性更高的版本也可以做到这一点(如图所示,编写一个通读代码的版本会更容易:)

产生:

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

这个问题对我来说有点模糊。你能告诉我们你有什么样的错误吗?实际的问题是什么?你说你“有困难”。但是,如果你不告诉我们困难是什么,就很难帮助你解决困难。(这有点像告诉你的医生你有症状)。这是一个很好的解决方案,但你可能想在
0th
位置添加
0
,然后将
放在末尾?您甚至可以使用for循环,而不是在
println()
中复制/粘贴这三行。
    __    .        __ 
|   __|       |_| |__|
|  |__    .     |  __|