+ 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.

10th Oct 2020, 5:45 PM
Hyperion
Hyperion - avatar
11 Réponses
+ 1
Would this work? System.out.println (a.repeat(x));
10th Oct 2020, 7:37 PM
Hyperion
Hyperion - avatar
+ 1
Thanks it's working👍👍
10th Oct 2020, 7:58 PM
Hyperion
Hyperion - avatar
0
Read task: shh is <1
10th Oct 2020, 6:24 PM
Coding Cat
Coding Cat - avatar
0
Ok thanks
10th Oct 2020, 6:32 PM
Hyperion
Hyperion - avatar
0
Its still failing
10th Oct 2020, 6:36 PM
Hyperion
Hyperion - avatar
0
Yes, this way it fails. But I'm not sure why. There must be something wrong with these ELSE IF statements
10th Oct 2020, 7:07 PM
Coding Cat
Coding Cat - avatar
0
Now I god it. Negativ values throws a exception on a. repeat(x) 😉
10th Oct 2020, 7:16 PM
Coding Cat
Coding Cat - avatar
0
How can i fix that
10th Oct 2020, 7:21 PM
Hyperion
Hyperion - avatar
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?
10th Oct 2020, 7:25 PM
Coding Cat
Coding Cat - avatar
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.
10th Oct 2020, 7:44 PM
Coding Cat
Coding Cat - avatar
0
Ok will try
10th Oct 2020, 7:53 PM
Hyperion
Hyperion - avatar