CodeGym
CodeGym University
Learning
Course
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
Onome Brownly-Otiede
Level 9
Galway
  • 27.06.2020
  • 577views
  • 4comments

i dont know what to do omg please help

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 { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); String d = f.readLine(); double t = Double.parseDouble(d); if (t % 5 == 0){ String light = "green"; } else if (t % 4 == 0 && t % 4 > 2){ String light = "red"; } else if (t % 3 == 0 && t % 3 > 1.5){ String light = "yellow"; System.out.println(light); } } }
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
anna
Level 15
7 August 2020, 11:33
There is a simple solution: modulo five over all the possibilities! Think what would be the "leftovers" when dividing each of the lights into 5 (the sicle of lights repeats itself every 5 minutes).. Try that: double a=scanner.nextDouble(); if (a%5<3){ System.out.println("green"); } else if(a%5>=4){ System.out.println("red"); } else{ System.out.println("yellow"); }
+1
Mnemonic
Level 9 , london, United Kingdom
3 August 2020, 11:16
Scanner input = new Scanner(System.in); int a = (int) input.nextDouble(); if((a == 0 | a == 1 | a == 2) & a <= 60) System.out.println("green"); else if((a % 5 == 0 | a % 5 == 1 | a % 5 == 2) & a <= 60) System.out.println("green"); else if(a == 3 | a % 5 == 3 & a <= 60) System.out.println("yellow"); else if((a == 4 | a % 5 == 4) & a <= 60) System.out.println("red"); else System.out.println("The number needs to be between 1 and 60");
0
Dyrits
Level 22
2 July 2020, 08:50
Just use the modulo operator with 5 and compare the result. You'll notice a pattern. When the result is less than 3, it is green. When the result is less than 4 it is yellow. Otherwise, it is red.
+1
Kent
Level 9 , Salt Lake City, United States
29 June 2020, 19:02
One of the problems is line 24, your output is part of the last else if statement. Move it outside of the else if statement and you should start getting some output.
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.