Advertisement
Ivakis

Diamond

Oct 17th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class p_11Diamond {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int n = Integer.parseInt(scanner.nextLine());
  8.  
  9. if(n == 1){
  10. System.out.println("*");
  11. return;
  12. }
  13.  
  14. if (n % 2 == 0) {
  15.  
  16. int leftRight = n / 2 - 1;
  17.  
  18. for (int i = 0; i < n / 2; i++) {
  19.  
  20. System.out.print(repeatStr("-", leftRight) + "*");
  21. System.out.print(repeatStr("-", n - 2 * leftRight - 2) + "*"); //
  22. System.out.println(repeatStr("-", leftRight));
  23. leftRight--;
  24. }
  25. leftRight = 1;
  26. int dashes = n - 4;
  27.  
  28. for (int i = 0; i < n / 2 - 1; i++) {
  29.  
  30. System.out.print(repeatStr("-", leftRight) + "*");
  31. System.out.print(repeatStr("-", dashes));
  32. System.out.println( "*" + repeatStr("-", leftRight));
  33.  
  34. leftRight++;
  35. dashes -=2;
  36. }
  37.  
  38. } else {
  39.  
  40. String firstLastRow = repeatStr("-", n /2)
  41. + "*"
  42. + repeatStr("-", n/2);
  43.  
  44. System.out.println(firstLastRow);
  45.  
  46. int leftRight = n / 2 - 1;
  47. int dashes = 1;
  48.  
  49. for (int i = 0; i < n /2; i++) {
  50. System.out.print(repeatStr("-", leftRight) + "*");
  51. System.out.print(repeatStr("-", dashes));
  52. System.out.println( "*" + repeatStr("-", leftRight));
  53.  
  54. leftRight--;
  55. dashes += 2;
  56. }
  57.  
  58. leftRight = 1;
  59. dashes -= 2;
  60. for (int i = 0; i < n / 2 - 1; i++) {
  61.  
  62. System.out.print(repeatStr("-", leftRight) + "*");
  63. System.out.print(repeatStr("-", dashes - 2));
  64. System.out.println("*" + repeatStr("-", leftRight));
  65.  
  66. dashes -=2;
  67. leftRight++;
  68. }
  69. System.out.println(firstLastRow);
  70. }
  71. }
  72.  
  73. static String repeatStr(String strToRepeat, int count) {
  74. String text = "";
  75. for (int i = 0; i < count; i++) {
  76. text += strToRepeat;
  77. }
  78. return text;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement