+ 3
(Answered) Problem with java code
I had a problem on the cheer creator code coach challenge. This is my code: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); String a = "Ra!"; String repeated = a.repeat(x); if (x > 10) { System.out.println("High Five"); } else if (x <= 0) { System.out.println("shh"); } else if (x <= 10) { System.out.println(repeated); } } } Only test case 5 is failing. Please help.
11 Antworten
+ 1
Would this work? System.out.println (a.repeat(x));
+ 1
Thanks it's working👍👍
0
Read task: shh is <1
0
Ok thanks
0
Its still failing
0
Yes, this way it fails. But I'm not sure why. There must be something wrong with these ELSE IF statements
0
Now I god it.
Negativ values throws a exception on a. repeat(x)
😉
0
How can i fix that
0
Think about it.
Now, you do the repeat for every input. That's not good for negativ input. Maybe there is a better place in the code, to do that?
0
Yes, this should work. At the right place.
Tipp: Copy your code to your playground (copy&paste). Write here code and test it. And then copy back to code coach and try it. Playground is better for testing. You can see outputs and error message.
0
Ok will try