Given a string and a nonnegative int n return a larger strin

Given a string and a non-negative int n, return a larger string that is n copies of the original string.


stringTimes(\"Hi\", 2) \"HiHi\"
stringTimes(\"Hi\", 3) \"HiHiHi\"
stringTimes(\"Hi\", 1) \"Hi\"

Must work the following problem using a while loop or do while.

Solution

StringTimesTest.java


public class StringTimesTest {

  
   public static void main(String[] args) {
      
System.out.println(stringTimes(\"Hi\", 2) );
System.out.println(stringTimes(\"Hi\", 3) );
System.out.println(stringTimes(\"Hi\", 1) );
   }
   public static String stringTimes(String s, int n){
       String returnString = \"\";
       int i=0;
       while(i<n){
           returnString = returnString + s;
           i++;
       }
       return returnString;
   }

}

Output:

HiHi
HiHiHi
Hi

Given a string and a non-negative int n, return a larger string that is n copies of the original string. stringTimes(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site