fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Ideone
  6. {
  7.  
  8. public static void main(String[] args)
  9. {
  10. int reader;
  11. Scanner kBoard = new Scanner(System.in);
  12. do
  13. {
  14. System.out.println("Insert a number of rows: ");
  15.  
  16. reader = kBoard.nextInt();
  17. printDiamond(reader);
  18.  
  19.  
  20. }while(reader != 0);
  21.  
  22. }
  23.  
  24. public static void printWORD(int n)
  25. {
  26. if(n >= 1)
  27. {
  28. System.out.print("SAMPLE");
  29. printWORD(n - 1);
  30. }
  31. }
  32.  
  33. public static void printTopTriangle(int rows)
  34. {
  35. int x = 1;
  36. for(int j = (rows - 1); j >= 0; j--,x +=2)
  37. {
  38. printSpaces(j);
  39. printWORD(x);
  40. System.out.print("\n");
  41. }
  42. }
  43.  
  44. public static void printSpaces(int n)
  45. {
  46. if(n >= 1)
  47. {
  48. System.out.print(" ");
  49. printSpaces(n - 1);
  50. }
  51. }
  52.  
  53. public static void printBottomTriangle(int rows, int startSpaces)
  54. {
  55. int x = 1 + (2*(rows - 1));
  56. for(int j = startSpaces; j <= (rows) && x > 0; j++,x -=2)
  57. {
  58. printSpaces(j);
  59. printWORD(x);
  60. System.out.print("\n");
  61. }
  62. }
  63. public static void printBottomTriangle(int rows)
  64. {
  65. int x = 1 + (2*(rows - 1));
  66. for(int j = 0; j <= (rows - 1) && x > 0; j++,x -=2)
  67. {
  68. printSpaces(j);
  69. printWORD(x);
  70. System.out.print("\n");
  71. }
  72. }
  73.  
  74. public static void printDiamond(int rows)
  75. {
  76. printTopTriangle((int)rows/2 + 1);
  77. printBottomTriangle((int)rows/2, 1);
  78. }
  79. }
Runtime error #stdin #stdout #stderr 0.11s 380672KB
stdin
12
stdout
Insert a number of rows: 
                                    SAMPLE
                              SAMPLESAMPLESAMPLE
                        SAMPLESAMPLESAMPLESAMPLESAMPLE
                  SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE
            SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE
      SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE
SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE
      SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE
            SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE
                  SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE
                        SAMPLESAMPLESAMPLESAMPLESAMPLE
                              SAMPLESAMPLESAMPLE
                                    SAMPLE
Insert a number of rows: 
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:907)
	at java.util.Scanner.next(Scanner.java:1530)
	at java.util.Scanner.nextInt(Scanner.java:2160)
	at java.util.Scanner.nextInt(Scanner.java:2119)
	at Ideone.main(Main.java:16)