What is wrong with the code?
Is Java 8 code acceptable here?
What is wrong with the code?
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Zeeshan Abbasi
27 September 2018, 01:16solution
remove all your conditions, simply write the conditions as given below:
if (t%5.0>=0&&t%5<3)
System.out.println("green");
if(t%5.0>=3&&t%5<4)
System.out.println("yellow");
else
System.out.println("red");
+3
WonderWoman
4 October 2018, 05:52
Wow. Thanks!
0
Roman
10 September 2018, 12:26
"What is wrong with the code?"
Where is your code?
"Is Java 8 code acceptable here?"
Yes, it is.
0
WonderWoman
10 September 2018, 13:58
When I went to the code page, it had disappeared. Luckily, I had saved it in another location:
There is a piece of commented code which was one of the n variants I tried, with no luck.. latest code is uncommented:
package com.codegym.task.task04.task0416;
/*
Crossing the road blindly
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
int b = Integer.parseInt(s);
IntStream redstream = IntStream.of(4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60);
IntStream yellowstream = IntStream.of(3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59);
if (b > 60)
System.out.println("Enter a number less than or equal to 60");
//int n = 0;
boolean contains = false;
if (contains = redstream.anyMatch(n -> n == b))
System.out.println("red");
else if (contains = yellowstream.anyMatch(n -> n == b))
System.out.println("yellow");
else
System.out.println("green");
}
}
/* if (isMultipleOf4(b) == 1)
System.out.println("red");
else if (isNotMultipleOf3(b) == 1)
System.out.println("green");
else
System.out.println("yellow");
}
public static int isNotMultipleOf3(int a){
int b1 = 0;
if (a % 3 != 0)
b1 = 1;
return b1;
}
public static int isMultipleOf4(int a){
int b2 = 0;
if (a % 4 == 0)
b2 = 1;
return b2;
}
*/
0
Md Bilal
10 September 2018, 16:49useful
use double instead of int
+1
WonderWoman
14 September 2018, 04:15
Tried this, did n't work either :(
package com.codegym.task.task04.task0416;
/*
Crossing the road blindly
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
Double b = Double.parseDouble(s);
DoubleStream redstream = DoubleStream.of(4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60);
DoubleStream yellowstream = DoubleStream.of(3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59);
if (b > 60)
System.out.println("Enter a number less than or equal to 60");
//int n = 0;
boolean contains = false;
if (contains = redstream.anyMatch(n -> n == b))
System.out.println("red");
else if (contains = yellowstream.anyMatch(n -> n == b))
System.out.println("yellow");
else
System.out.println("green");
}
}
/* if (isMultipleOf4(b) == 1)
System.out.println("red");
else if (isNotMultipleOf3(b) == 1)
System.out.println("green");
else
System.out.println("yellow");
}
public static int isNotMultipleOf3(int a){
int b1 = 0;
if (a % 3 != 0)
b1 = 1;
return b1;
}
public static int isMultipleOf4(int a){
int b2 = 0;
if (a % 4 == 0)
b2 = 1;
return b2;
}
*/
0
Md Bilal
14 September 2018, 14:17
simply try dividing by 5 if remainder is less than 3 green then if remainder is >= 3 and < 4 then display and so on
0