nikiman8888

Loops/Diamond

Feb 17th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Diamond {
  4. public static void main(String[] args) {
  5.  
  6. Scanner console = new Scanner(System.in);
  7.  
  8. int n = Integer.parseInt(console.nextLine());
  9. int leftRightLines = (n - 1) / 2;
  10. if (n == 1) {
  11. System.out.println("*");
  12. }
  13. if (n == 2) {
  14. System.out.println("**");
  15. }
  16. if (n % 2 != 0 && n > 1) {
  17. System.out.println(method("-", (n - 1) / 2) + "*" + method("-", (n - 1) / 2));
  18. for (int i = 1; i <= (n - 1) / 2; i++) {
  19. System.out.println(method("-", leftRightLines - i) + "*" + method("-", i) + method("-", i - 1) +
  20. "*" + method("-", leftRightLines - i));
  21. }
  22. for (int i = 1; i <= (n - 1) / 2 - 1; i++) {
  23. System.out.println(method("-", i) + "*" + method("-", (n - 4) - (2 * (i - 1))) + "*" + method("-", i));
  24.  
  25. }
  26. System.out.println(method("-", (n - 1) / 2) + "*" + method("-", (n - 1) / 2));
  27.  
  28. }
  29. if (n % 2 == 0 && n > 2) {
  30. System.out.println(method("-", (n - 1) / 2) + "**" + method("-", (n - 1) / 2));
  31. for (int i = 1; i <= (n - 1) / 2; i++) {
  32. System.out.println(method("-", leftRightLines - i) + "*" + method("-", i) + method("-", i) +
  33. "*" + method("-", leftRightLines - i));
  34. }
  35.  
  36. for (int i = 1; i <= (n - 1) / 2 - 1; i++) {
  37. System.out.println(method("-", i) + "*" + method("-", (n - 4) - (2 * (i - 1))) + "*" + method("-", i));
  38. }
  39. System.out.println(method("-", (n - 1) / 2) + "**" + method("-", (n - 1) / 2));
  40.  
  41. }
  42. }
  43.  
  44.  
  45. public static String method(String text, int count) {
  46. String output = "";
  47. for (int i = 0; i < count; i++) {
  48. output = output + text;
  49. }
  50. return output;
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment