Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. package triangles;
  2.  
  3. import java.lang.StringBuffer;
  4. import java.util.Scanner;
  5.  
  6. public class triangles {
  7.     public static void main(String[] args) {
  8.         Scanner s = new Scanner(System.in);
  9.        
  10.         System.out.print("Please input the number of rows: ");
  11.         int rows = s.nextInt();
  12.        
  13.         System.out.print("Please input the number of columns: ");
  14.         int columns = s.nextInt();
  15.        
  16.         s.close();
  17.        
  18.         if((columns % 4) != 1) {
  19.             System.out.println("\"Columns\" must be a multiple of n+4");
  20.             return;
  21.         }
  22.        
  23.         if(((columns - 1) / 2) + 1 > rows) {
  24.             System.out.println("\"Rows\" must be >= (columns / 2) + 1");
  25.             return;
  26.         }
  27.        
  28.         int remaining = rows - ((columns + 1) / 2);
  29.         rows = ((columns + 1) / 2);
  30.         //System.out.println(remaining);
  31.        
  32.         if(remaining % 2 != 0) {
  33.             System.out.println("The extra number or rows must be even (add 1 to input)");
  34.             return;
  35.         }
  36.        
  37.         if(remaining != 0) {
  38.             StringBuffer buffer = new StringBuffer();
  39.             for(int i = 0; i < columns; i += 1) {
  40.                 buffer.append("+ ");
  41.             }
  42.            
  43.             for(int i = 0; i < remaining / 2; i += 1) {
  44.                 System.out.println(buffer.toString());
  45.             }
  46.            
  47.         }
  48.        
  49.         for(int i = 1, j = 0; j < rows; i += 2, j+= 1) {
  50.             StringBuffer lineBuffer = new StringBuffer();
  51.            
  52.             for(int k = 1; k < ((columns - i) / 2) + 1; k += 1) {
  53.                 lineBuffer.append("  ");
  54.             }
  55.            
  56.             for(int l = 0; l < i; l += 1) {
  57.                 int m = 0;
  58.                        
  59.                 if((l > (rows + 1) / 2)) {
  60.                    
  61.                     if(l != rows - 1) {
  62.                        
  63.                     }
  64.                 } else {
  65.                     lineBuffer.append("x ");
  66.                 }
  67.             }
  68.            
  69.             System.out.println(lineBuffer.toString());
  70.            
  71.         }
  72.        
  73.         if(remaining != 0) {
  74.             StringBuffer buffer = new StringBuffer();
  75.             for(int i = 0; i < columns; i += 1) {
  76.                 buffer.append("+ ");
  77.             }
  78.            
  79.             for(int i = 0; i < remaining / 2; i += 1) {
  80.                 System.out.println(buffer.toString());
  81.             }
  82.            
  83.         }
  84.        
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement