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/what issue with green
I.A
Level 7
unknown city
  • 27.11.2018
  • 1838views
  • 7comments

what issue with green

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.*; import java.util.Scanner; public class Solution { public static void main(String[] args) throws Exception { //write your code here //BuffererdReader br=new BuffererdReader(new InputStreamReder(System.in)); // double num=double.parseDouble(br.readLine()); Scanner sc=new Scanner(System.in); double num=sc.nextDouble(); double n=num%5; if((n>=0.10) && (n<3)){ System.out.println("green");} else if((n>=3.00) && (n<4 )){ System.out.println("yellow");} else if((n>=4.00) && (n<5)){ System.out.println("red");} num=num/5; } }
0
Comments (7)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Thang Za Thang
Level 18 , Melbourne, Australia
15 June 2020, 00:22
InputStreamReader sReader = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(sReader); float t = Float.parseFloat(br.readLine()); if (t%5.0 >= 0 && t%5 < 3.0) System.out.println("green"); else if(t%5.0 >= 3.0 && t%5 < 4.0) System.out.println("yellow"); else{ System.out.println("red"); }
0
Hendra
Level 8 , Medan, Indonesia
6 December 2019, 14:44
package com.codegym.task.task04.task0416; /* Crossing the road blindly */
0
Willard Roseberry
Level 11 , Brigham City, United States
25 November 2019, 02:11
try using the % operator to split the 3 min of green with the remainder.
0
Jivesh
Level 8 , Mumbai, India
21 June 2019, 09:36
Only keep this as a condition for GREEN if(n<3)
0
Artem Divertitto Senior Android Developer at United Tech
27 November 2018, 15:22
Green starts from 0)
+2
Michael Martin
Level 2 , Arlington, United States
27 November 2018, 15:08
What if the number read is zero?
0
Noam G
Level 15 , Tel Aviv, Israel
1 July 2019, 06:47
Zero is not a valid amount of minutes.
0
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