CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Andrew Nguyen
Level 23
Toronto
  • 18.04.2020
  • 591views
  • 2comments

Task requirements are not met

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

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 { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); double t = Double.parseDouble(reader.readLine()); if (t>4) t=t%4; if (t>60) t = t%60; if (t>0 && t<3) System.out.print("green"); else if (t>=3 && t <4) System.out.print("yellow"); else System.out.print("red"); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Gellert Varga
Level 23 , Szekesfehervar, Hungary
18 April 2020, 20:08
Hi!:) 1.) Line 15: if t>4 t=t%4; I think you think the cycle of the lights is four minutes. But! Green 3 minutes, yellow 1 minute, red 1 minute. It's 5 minutes total. Then the same thing starts all over again, which is 5 minutes again and again. 2.) Line 15: if t>4 t=t%4; So from now, the value of "t" will be ALWAYS between 0 and 4. It can be never more than 4! So, the Line 16 test (if t>60) loses its meaning. It's not needed. 3.) Line 18: If t>0... But what if t=0.0? Then it should be green... 4.) An advice: If you test your program a lot, with different input values, it will always help a lot to detect what the problem is.
0
Andrew Nguyen
Level 23 , Toronto, Canada
18 April 2020, 23:27
Thank you. That helps a lot
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • 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 Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.