fork(1) download
  1. class Main{
  2. public static void main(String[] a){
  3. rowofStars(0,7); // Expected: " "
  4. rowofStars(1,7); // Expected: " * "
  5. rowofStars(2,7); // Expected: " * * "
  6. rowofStars(3,7); // Expected: " *** "
  7. rowofStars(4,7); // Expected: " ** ** "
  8. rowofStars(5,7); // Expected: " ***** "
  9. rowofStars(6,7); // Expected: "*** ***"
  10. rowofStars(7,7); // Expected: "*******"
  11. }
  12.  
  13. static void rowofStars(int num, int length){
  14. // Create a character array with the given length:
  15. char[] charArray = new char[length];
  16.  
  17. // Fill this entire array with spaces initially:
  18. java.util.Arrays.fill(charArray, ' ');
  19.  
  20. // Fill the middle of the array with the appropriate amount of '*'
  21. // num amount of times for odd, and num+1 amount of times for even
  22. java.util.Arrays.fill(charArray, length/2 - num/2, length/2 + num/2 + 1, '*');
  23.  
  24. // If the num was even, change the center spot back to a space:
  25. if(num%2 == 0)
  26. charArray[length/2] = ' ';
  27.  
  28. // Convert the character-array to a String
  29. String result = new String(charArray);
  30.  
  31. // And output the String:
  32. System.out.println(result);
  33. }
  34. }
Success #stdin #stdout 0.05s 27776KB
stdin
Standard input is empty
stdout
       
   *   
  * *  
  ***  
 ** ** 
 ***** 
*** ***
*******