交通信号灯程序。Java。循环。

huangapple go评论56阅读模式
英文:

Traffic light program. Java. Loops

问题

代码太复杂,似乎无法解决问题。请看一下并给我建议。
请确保不要提供解决方案,因为我宁愿自己修复,而不是让问题变得不清楚。
下面是代码:

任务:
行人交通灯的工作方式如下:
每小时初,绿灯亮三分钟,
然后信号灯黄亮一分钟,
接着红灯亮一分钟。
然后绿灯再亮三分钟,以此类推。
使用键盘输入一个表示从小时开始已过去的分钟数的实数t。
确定在指定时间交通灯的颜色。
按以下方式显示结果:
如果灯是绿色,则为“green”;
如果灯是黄色,则为“yellow”;
如果灯是红色,则为“red”。

示例:
对于2.5 - 绿色。
对于3 - 黄色。
对于5 - 绿色。
以此类推,在60分钟循环内。

我的代码:

public static void main(String[] args) throws NumberFormatException, IOException {
		
	BufferedReader sum = new BufferedReader(new InputStreamReader(System.in));
    double a;
        
    a = Double.parseDouble(sum.readLine());
        
    while(true) {
        calcYellow(a, a); break;
    }
    while(true) {
        calcRed(a, a); break;
    }
    while(true) {
        calcGreen(a, a, a, a); break;
    }
}
		
static void calcYellow(double a, double b) {
    double a1 = 3.0;
    double b1 = 3.5;
    for (a = a1 + 4; a < 60; a++);
    for (b = b1 + 4; b < 60; b++);
    if ((a % 3.0 == 0) || (a % 3.5 == 0));
    if ((b % 3.0 == 0) || (b % 3.5 == 0));
		
    System.out.println("Yellow");
}
	
static void calcRed(double a, double b) {
    double a1 = 4.0;
    double b1 = 4.5;
    for (a = a1 + 4; a < 60; a++);
    for (b = b1 + 4; b < 60; b++);
    if ((a % 4.0 == 0) && (a % 4.5 == 0));
    if ((b % 4.0 == 0) && (b % 4.5 == 0));
		
    System.out.println("Red");
}
	
static void calcGreen(double a, double b, double c, double d) {
    double a1 = 1.0;
    double b1 = 1.5;
    double c1 = 2.0;
    double d1 = 2.5;
    for (a = a1 + 4; a < 60; a++);
    for (b = b1 + 4; b < 60; b++);
    for (c = c1 + 4; c < 60; c++);
    for (d = d1 + 4; d < 60; d++);
		
    System.out.println("Green");
}

非常感谢您提供的建议!

英文:

My code is getting complicated and it doesn't seem to be getting resolved, please, have a look and give me advice.
Please, make sure you do not share a solution as I'd rather fix it by me, instead of leaving it unclear.
Here we go:

TASK:
The pedestrian traffic light is programmed as follows:
at the beginning of each hour, the green signal is on for three minutes,
then the signal is yellow for one minute,
and then it is red for one minute.
Then the light is green again for three minutes, etc.
Use the keyboard to enter a real number t that represents the number of minutes that have elapsed since the beginning of the hour.
Determine what color the traffic light is at the specified time.
Display the result as follows:
"green" if the light is green,
"yellow" if the light is yellow, and
"red" if the light is red.

EXAMPLE:
For 2.5 - Green.
For 3 - Yellow.
For 5 - Green.
and so on, within 60 minutes loop.

MY CODE:

     public static void main(String[] args) throws NumberFormatException, IOException {
	
	 BufferedReader sum = new BufferedReader(new InputStreamReader(System.in));
     double a;
    
    a = Double.parseDouble(sum.readLine());
    
    while(true) {
     calcYellow(a, a); break;}
    while(true) {
     calcRed(a, a); break;}
    while(true) {
     calcGreen(a, a, a, a); break;}

    }
	
    static void calcYellow(double a, double b) {
	  double a1 = 3.0;
	  double b1 = 3.5;
	for (a=a1+4; a&lt;60; a++);
	for (b=b1+4; b&lt;60; b++);
	  if ((a%3.0==0) || (a%3.5==0));
	  if ((b%3.0==0) || (b%3.5==0));
	
	System.out.println(&quot;Yellow&quot;);
      }

    static void calcRed(double a, double b) {
	  double a1 = 4.0;
	  double b1 = 4.5;
	for (a=a1+4; a&lt;60; a++);
	for (b=b1+4; b&lt;60; b++);
	if ((a%4.0==0) &amp;&amp; (a%4.5==0));
	if ((b%4.0==0) &amp;&amp; (b%4.5==0));
	
	System.out.println(&quot;Red&quot;);
    }

    static void calcGreen(double a, double b, double c, double d) {
	double a1 = 1.0;
	double b1 = 1.5;
	double c1 = 2.0;
	double d1 = 2.5;
	for (a=a1+4; a&lt;60; a++);
	for (b=b1+4; b&lt;60; b++);
	for (c=c1+4; c&lt;60; c++);
	for (d=d1+4; d&lt;60; d++);
	
	System.out.println(&quot;Green&quot;);
}		
	}

Any advice will be highly appreciated.
TY in Advance!

答案1

得分: 1

以下是翻译好的代码部分:

public static void main(String[] args) throws IOException {
    BufferedReader sum = new BufferedReader(new InputStreamReader(System.in));
    double enteredTime;
    enteredTime = Double.parseDouble(sum.readLine());

    double colorTime = enteredTime % 5;

    if (colorTime >= 0 && colorTime < 3) {
        System.out.println("绿色");
    } else if (colorTime >= 3 && colorTime < 4) {
        System.out.println("黄色");
    } else if (colorTime >= 4 && colorTime < 5) {
        System.out.println("红色");
    }
}

希望这对你有所帮助。如果有其他需要,请随时告诉我。

英文:

Solution could be as easy as this:

 public static void main(String[] args) throws IOException {
        BufferedReader sum = new BufferedReader(new InputStreamReader(System.in));
        double enteredTime;
        enteredTime = Double.parseDouble(sum.readLine());

        double colorTime = enteredTime % 5;

        if (colorTime &gt;= 0 &amp;&amp; colorTime &lt; 3) {
            System.out.println(&quot;Green&quot;);
        } else if (colorTime &gt;= 3 &amp;&amp; colorTime &lt; 4) {
            System.out.println(&quot;Yellow&quot;);
        } else if (colorTime &gt;= 4 &amp;&amp; colorTime &lt; 5) {
            System.out.println(&quot;Red&quot;);
        }


    }

You could expand this with the 60 minutes loop.

答案2

得分: 1

你可以创建一个颜色数组,模拟它们的顺序,并使用取余运算计算数组中的索引。

此外,当时间戳为负数时需要处理一种特殊情况,即需要在“过去”定义交通灯颜色:从1分钟“前”,它是红色的;从2分钟到1分钟,它是黄色的,依此类推。

private static final String[] COLORS_BY_MINUTE = {"Green", "Green", "Green", "Yellow", "Red"};

public static String getColor(double minute) {
    double colorIndex = minute % COLORS_BY_MINUTE.length;
    // 处理“向后”/负时间戳
    if (colorIndex < 0) {
        colorIndex += COLORS_BY_MINUTE.length;
    }
    return COLORS_BY_MINUTE[(int) colorIndex];
}

// 测试
double[] tests = { -3.5, -2.5, -1.5, -1, 0, 1.5, 2.5, 3, 3.8, 4.5, 5.5, 62.5 };
Arrays.stream(tests)
      .forEach(t -> System.out.printf("%4.1f -> %s%n", t, getColor(t)));

输出:

-3.5 -> Green
-2.5 -> Green
-1.5 -> Yellow
-1.0 -> Red
 0.0 -> Green
 1.5 -> Green
 2.5 -> Green
 3.0 -> Yellow
 3.8 -> Yellow
 4.5 -> Red
 5.5 -> Green
62.5 -> Green
英文:

You could create an array of colors imitating their sequence and calculate the index in the array using remainder operation.

Also, an edge case may need to be handled when the timestamp is negative, that is, you need to define the traffic light color in the past: from 1 minute "ago" it was red, from 2 till 1 minutes it was yellow and so on.

private static final String[] COLORS_BY_MINUTE = {&quot;Green&quot;, &quot;Green&quot;, &quot;Green&quot;, &quot;Yellow&quot;, &quot;Red&quot;};

public static String getColor(double minute) {
    double colorIndex = minute % COLORS_BY_MINUTE.length;
    // handle &quot;backward&quot;/negative timestamp
    if (colorIndex &lt; 0) {
        colorIndex += COLORS_BY_MINUTE.length;
    }
    return COLORS_BY_MINUTE[(int) colorIndex];
}

// tests
double[] tests = { -3.5, -2.5, -1.5, -1, 0, 1.5, 2.5, 3, 3.8, 4.5, 5.5, 62.5 };
Arrays.stream(tests)
      .forEach(t -&gt; System.out.printf(&quot;%4.1f -&gt; %s%n&quot;, t, getColor(t)));

Output:

-3.5 -&gt; Green
-2.5 -&gt; Green
-1.5 -&gt; Yellow
-1.0 -&gt; Red
 0.0 -&gt; Green
 1.5 -&gt; Green
 2.5 -&gt; Green
 3.0 -&gt; Yellow
 3.8 -&gt; Yellow
 4.5 -&gt; Red
 5.5 -&gt; Green
62.5 -&gt; Green

huangapple
  • 本文由 发表于 2020年9月15日 16:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63897554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定