Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1.  
  2. public class DrawTriangles
  3. {
  4. private int stars, lines, spaces;
  5.  
  6. // This method draws the first triangle
  7. public void drawTriangle1(int choice)
  8. {
  9. for (lines = 1; lines <= choice; lines++)
  10. {
  11. for (stars = 1; stars <= lines; stars++)
  12. {
  13. System.out.print('*');
  14. System.out.print(' ');
  15.  
  16. } // end inner loop
  17.  
  18. System.out.println();
  19.  
  20. } // end outer loop
  21.  
  22. System.out.println();
  23.  
  24. } // end drawTriangle1
  25.  
  26.  
  27. // This method draws the second triangle
  28. public void drawTriangle2(int choice)
  29. {
  30. for (lines = choice; lines >= 1; lines--)
  31. {
  32. for (stars = 1; stars <= lines; stars++)
  33. {
  34. System.out.print('*');
  35. System.out.print(' ');
  36.  
  37. } // end inner loop
  38.  
  39. System.out.println();
  40.  
  41. }
  42.  
  43. System.out.println();
  44.  
  45. } // end drawTriangle2
  46.  
  47.  
  48. // This method draws the third triangle
  49. public void drawTriangle3(int choice)
  50. {
  51. for (lines = 1; lines <= choice; lines++)
  52. {
  53. for (spaces = choice; spaces > lines; spaces--)
  54. {
  55. System.out.print(' ');
  56. }
  57. for (stars = 1; stars <= lines; stars++)
  58. {
  59. System.out.print('*');
  60.  
  61. } // end inner loop
  62.  
  63. System.out.println();
  64.  
  65. } // end outer loop
  66.  
  67. System.out.println();
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74. // This method draws the fourth triangle
  75. public void drawTriangle4(int choice)
  76. {
  77. for (lines = 1; lines <= choice; lines++)
  78. {
  79. for (spaces = 1; spaces <= lines; spaces++) // spaces loop is the same for both 3 and
  80. {
  81. System.out.print(' ');
  82. } // end inner loop
  83. for (stars = choice; stars >= lines; stars--)
  84. {
  85. System.out.print('*');
  86. }
  87.  
  88. System.out.println();
  89.  
  90. }
  91.  
  92. System.out.println();
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement