Java三角印刷第二部分

时间:2015-02-01 21:32:57

标签: java

我最近提交了一个问题,询问如何开始打印空心和实心三角形。我已经让这些方法正确显示。我现在有两个新问题,我似乎遇到了。

首先,当我试着说" Triangle s1 = new Triangle('&')"输出仅显示每个字符中的一个。

任何想法??

/** 
Class Summary: 
Author: Ian Monson
*/

public class Triangle {

// Declare & intialize data fields
private static char defaultChar = '*';
private static int defaultSize = 10;

private static char triangleChar;
private static int triangleSize;

private static int triangleCount = 0;


// Constructors 
public Triangle() {
  this(defaultSize, defaultChar);
}

public Triangle(int s) {
   this(s, defaultChar);
}

public Triangle(char n) {
  this(n, defaultSize);
}

public Triangle(int size, char character) {         

  triangleChar = character;
  triangleSize = size;

  triangleCount++; 
}

// Accessors and Mutators
 public int getSize() {
  return triangleSize;
 }

 public char getChar() {
  return triangleChar;
 }

public void setSize(int size) {
  triangleSize = size;
}

public void setChar(char character) {
  triangleChar = character;
}

// Main methods for displaying solid triangles
public void displaySolidLL() {
  for (int row = 0; row <= triangleSize; row++) {
     for (int col = 0; col <= row; col++) {
        System.out.print(triangleChar);
     }         
     System.out.println();
  }
 newLine(2);
}

public void displaySolidLR() {
   // Declare some local variables
  int row, col1, col2, noOfCol;
  noOfCol = triangleSize - 1; 

  for (row = 0; row <= triangleSize; row++) {
     for (col1 = 0; col1 <= noOfCol; col1++) {
        System.out.print(" ");
     }

     noOfCol--;

     for (col2 = 0; col2 <= row; col2++) {
        System.out.print(triangleChar); 
     }
     System.out.println();
   }
  newLine(2);
}

public void displaySolidUL() {
  for (int row = triangleSize; row >= 1; row--) {
     for (int col = 1; col <= row; col++) {
        System.out.print(triangleChar);
     }
     System.out.println();
  }
  newLine(2);
}

public void displaySolidUR() {
  for (int row = 0; row < triangleSize; row++) {
     for (int col = 0; col < row; col++) {
        System.out.print(" ");
     }

     for (int x = row; x < triangleSize; x++) {
        System.out.print(triangleChar);
     }
     System.out.println();
  }
  newLine(2);
}


// Main methods for displaying hollow triangles

public void displayHollowLL() {
  for (int row = 0; row < triangleSize - 1; row++) {
     System.out.print(triangleChar);
     for (int col = 0; col < row - 1; col++) {
        System.out.print(" ");
     }

     if (row == 0) {
        System.out.println();
     } 
     else {
        System.out.print(triangleChar);
        System.out.println();
     }

  }

  for (int y = 0; y < triangleSize; y++) {
     System.out.print(triangleChar);
  }
 newLine(2);
}

public void displayHollowLR() {
  for (int x = 1; x < triangleSize; x++) {
     if (x == 1) {
        for (int y = triangleSize; y > x + 1; y--) {
           System.out.print(" ");
        }
        System.out.print(triangleChar);
     } 
     else if (x == triangleSize - 1) {
        for (int y = 0; y < triangleSize - 1; y++) {
           System.out.print(triangleChar);
        }


     } 
     else {
        for (int y = triangleSize; y > x + 1; y--) {
           System.out.print(" ");
        }
        System.out.print(triangleChar);

        for  (int z = 0; z < x - 2; z++) {
           System.out.print(" ");
        }
        System.out.print(triangleChar);
     }
     System.out.println();
  }
  newLine(2); 
}


public void displayHollowUL() {
  for (int row = 0; row <= triangleSize; row++) {
     if (row == 1) {
        for (int col = 0; col < triangleSize; col++) {
           System.out.print(triangleChar);
        }

     } 
     else if (row == triangleSize) {
        System.out.print(triangleChar);
     } 
     else if (row > 1) {
        System.out.print(triangleChar);
        for (int y = triangleSize; y > row + 1; y--) {
           System.out.print(" ");
        }
        System.out.print(triangleChar);
     } 
     System.out.println();
  }
  newLine(2);
}

public void displayHollowUR() {
  for (int row = 0; row < triangleSize; row++) {
     if (row == 1) {
        for (int col = 0; col < triangleSize; col++) {
           System.out.print(triangleChar);
        }
        System.out.println();
     } 
     else if (row > 1) {
        for (int y = 0; y < row - 1; y++) {
           System.out.print(" ");
        }

        System.out.print(triangleChar);

        for (int z = triangleSize; z > row + 1; z--) {
           System.out.print(" ");
        }

        System.out.print(triangleChar);
        System.out.println();
     }

  }


  for (int j = 0; j < triangleSize - 1; j++) {
     System.out.print(" ");
  }
  System.out.print(triangleChar);

  newLine(2);
}


// Other utility classes

private void displaySolidLine(int n) {


}

private void displayHollowLine(int n) {


}

private void displayBlankLine (int n) {


}


// Printing out new lines
public static void newLine(int numLines) {
  for (int i = 0; i < numLines; i++) {
     System.out.println();
  }
}

// Display triangle count
public static void getTriangleCount() {
  System.out.println("Total number of triangle objects created: " + triangleCount);
  System.out.println();
}
}

1 个答案:

答案 0 :(得分:1)

您可以通过简单的if else构造来执行第一条规则。

public Triangle(int size, char character){
    if ((size<=0) || (size>50)){
        triangleSize=defaultSize;
    } else triangleSize=size;
    if (character==''){ // assuming by blank you mean blank, if it means blank space, just replace by a ' '
        triangleChar=defaultChar;
    }else triangleChar=character;
}

对于第二条规则,

public Triangle(int size){
        if ((size<=0) || (size>50)){
            triangleSize=defaultSize;
        } else triangleSize=size;
        triangleChar=defaultChar;
    }

public Triangle(char character){
        triangleSize=defaultSize;
        if (character==''){ // assuming by blank you mean blank, if it means blank space, just replace by a ' '
            triangleChar=defaultChar;
        }else triangleChar=character;
    }