Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package triangles;
- import java.lang.StringBuffer;
- import java.util.Scanner;
- public class triangles {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- System.out.print("Please input the number of rows: ");
- int rows = s.nextInt();
- System.out.print("Please input the number of columns: ");
- int columns = s.nextInt();
- s.close();
- if((columns % 4) != 1) {
- System.out.println("\"Columns\" must be a multiple of n+4");
- return;
- }
- if(((columns - 1) / 2) + 1 > rows) {
- System.out.println("\"Rows\" must be >= (columns / 2) + 1");
- return;
- }
- int remaining = rows - ((columns + 1) / 2);
- rows = ((columns + 1) / 2);
- //System.out.println(remaining);
- if(remaining % 2 != 0) {
- System.out.println("The extra number or rows must be even (add 1 to input)");
- return;
- }
- if(remaining != 0) {
- StringBuffer buffer = new StringBuffer();
- for(int i = 0; i < columns; i += 1) {
- buffer.append("+ ");
- }
- for(int i = 0; i < remaining / 2; i += 1) {
- System.out.println(buffer.toString());
- }
- }
- for(int i = 1, j = 0; j < rows; i += 2, j+= 1) {
- StringBuffer lineBuffer = new StringBuffer();
- for(int k = 1; k < ((columns - i) / 2) + 1; k += 1) {
- lineBuffer.append(" ");
- }
- for(int l = 0; l < i; l += 1) {
- int m = 0;
- if((l > (rows + 1) / 2)) {
- if(l != rows - 1) {
- }
- } else {
- lineBuffer.append("x ");
- }
- }
- System.out.println(lineBuffer.toString());
- }
- if(remaining != 0) {
- StringBuffer buffer = new StringBuffer();
- for(int i = 0; i < columns; i += 1) {
- buffer.append("+ ");
- }
- for(int i = 0; i < remaining / 2; i += 1) {
- System.out.println(buffer.toString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement