public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double num = scanner.nextDouble();
num = num % 5;
if (num >= 0.0 && num < 3.0){
System.out.println("green");
}else if (num >= 3.0 && num < 4.0){
System.out.println("yellow");
}else if (num >= 4.0 && num < 5.0){
System.out.println("red");
}else {
System.out.println("green");
}
scanner.close();
}
}
This is my solution guys hope it helps even i had to do 10 attempts to solve it .
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Mike
26 September 2019, 19:41
good code, last else can be deleted as mentioned, thank you
0
Guadalupe Gagnon
2 May 2019, 15:35
This isn't a question. You do nothing to help anyone by posting solutions. I recommend deleting this post.
+2
nagabenang
25 June 2019, 03:30
he's indirectly helps me with how to input double using bufferedreader and my solution still differs from his so I think it's okay.
0
Swapnil
2 May 2019, 15:27
I have a question why did you do modulo operation with 5? Since 3+1+1 = 5? So if it was 3+2+1 = 6, you will do modulo operation with 6?
0
Deepak Chandra
10 May 2019, 07:40
because green light should be on again after 5 minutes and its a five minute cycle
yeah so 3+1+1 = 5 thats why i used 5 for modulo.
+1
Nicolas Huet
2 May 2019, 11:16
Did you try without the last else? Why did you put it?
0
Deepak Chandra
2 May 2019, 13:03
Actually i was trying different ways to solve the problem but here the last else block is not necessary ,i forgot to remove that block .Anyway thanks for pointing out. that last else block will come into play if you dont use modulus operation at all in your code .
+3