1

I'm new with java :-(
I need to get value jee repeated with n times (jeejee, jeejeejee etc.), now I get


jee6 !
Test with value 6: 6
jee2 !
Test with value 2: 2
jee3 !
Test with value 3: 3
jee6 !
Test with value 6: 6
jee3 !
Test with value 3: 3



import java.util.Random;

public class Test3{
    public static void main(String[] args){<br/>
        final Random r = new Random();

        for (int i=0; i < 5; i++){
            int n = r.nextInt(6) + 1;
            System.out.println("Test with value " + n + ": " + jeeJee(n));
        }
    }

    public static int jeeJee(int i){
        System.out.println( "jee" + i + " !");
        return i;
    }
}
2
  • 1
    please post the expected output Commented Nov 26, 2019 at 18:24
  • I need to get answer jee with n times (depending what above code says) and !-mark at the end (I forgot that at the beginning) Commented Nov 26, 2019 at 19:01

3 Answers 3

1

Use a loop inside your print function:

public static int jeeJee(int i){

    for(int j = 0; j< i; j++)System.out.print( "jee");
    return i;

}
Sign up to request clarification or add additional context in comments.

Comments

1

Your function jeejee() should print "jee", i number of times. So for that you can use for loop:

 public static int jeeJee(int i){

     System.out.println(i);
     StringBuffer sb = new StringBuffer();

     for(int k=0;k<i;k++) {
         sb.append("jee");
     }
     System.out.println(sb);
     return i;
 }

1 Comment

If you are not in the need of thread safety with your StringBuffer, you choose use a StringBuilder instead. In fact, StringBuffer is synchronized and will have poor performance compare to StringBuilder
1

With Java 11+ you can use

"jee".repeat(n) + " !";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.