CodeGym
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/requirements not met?
monipuff
Level 22
  • 08.08.2020
  • 734views
  • 1comment

requirements not met?

Question about the task Crossing the road blindly
Java Syntax,  Level 4,  Lesson 4
Under discussion

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
Example for 3:
yellow
Example for 4:
red
Example for 5:
green

Requirements:
  • The program should read a real number from the keyboard.
  • The program should display text on the screen.
  • If the light is green, display: "green"
  • If the light is yellow, display: "yellow"
  • If the light is red, display: "red"
package com.codegym.task.task04.task0416; /* Crossing the road blindly */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //write your code here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); double t = Double.parseDouble(br.readLine()); double light = t%5; if (t<=7.59){ if (t>=0&&t<=2.59){ System.out.println("green"); } else if (t>=3.0&&t<=3.59){ System.out.println("yellow"); } else if(t>=4.0&&t<=4.59){ System.out.println("red"); } } else if (light>=3.0&&light<=3.59){ System.out.println("yellow"); } else if (light>=4.0&&light<=4.59){ System.out.println("yellow"); } else { System.out.println("green"); } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Agent Smith
Level 38
8 August 2020, 17:05
Try to carefully analyze what you are trying to achieve with your conditionals. 1) What is the possible range of results for a t % 5 where t is a double? Try substituting different t values, for example t < 5 and t > 5 to see what happens. 2) Can you somehow map that range to your program specification (green/yellow/red)? In short, your conditionals look over complicated. For example, what is 7.59? Is it necessary to use here at all? You can make the conditional part of this task with just 3 lines of code. Here is a hint how it looks:
if( ... ) System.out.println("green");
else if( ... ) System.out.println("yellow");
else System.out.println("red");
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym