[英]I'm not sure how to add spaces to the small triangles to make it into a larger triangle
因此,基本上,當用戶輸入模塊8等於0的數字時,我的程序將打印這種類型的三角形。因此,較大的三角形是由以4為底的較小三角形組成的。這就是我輸入數字時三角形的樣子16:
* * * * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * *
* * * *
* * * * * * * * * * * *
* * * * * * * * *
* * * * * *
* * *
* * * * * * * *
* * * * * *
* * * *
* *
* * * *
* * *
* *
*
但是我的結果是這樣的:
* * * * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * *
* * * *
* * * * * * * * * * * *
* * * * * * * * *
* * * * * *
* * *
* * * * * * * *
* * * * * *
* * * *
* *
* * * *
* * *
* *
*
我不太確定如何添加空格。 有人可以幫我嗎? 這是我的代碼:
...
System.out.println("Please enter the length:");
int length = scan.nextInt();;
//length must be longer than 2
if (length <= 1){
System.out.println("Sorry :(! Length must be 2 or higher");
}
//if user enter a number that can divide by 8 and has no remainder, then
//loop to print out a beautiful triangle
else if(length%8==0) {
int numOfTriangle = length/4;
System.out.println("Here is your beautiful triangle :D \n");
for (int rowOfTri = numOfTriangle; rowOfTri > 0; rowOfTri--){
for(int i = 0; i < rowOfTri; i++){
printBase();
}
System.out.println();
for(int i = 0; i < rowOfTri; i++){
printThree();
}
System.out.println();
for(int i = 0; i < rowOfTri; i++){
printTwo();
}
System.out.println();
for(int i = 0; i < rowOfTri; i++){
printOne();
}
System.out.println();
}
System.out.println();
}
//any other number will print a normal triangle
else{
System.out.println("Here is your beautiful triangle :D \n");
for (int i = 1; i <= length; i++){
for (int j = 1; j <= length; j++){
if (j < i){
System.out.print(" ");
} else {
System.out.print("*");
System.out.print(" ");
}
}
System.out.println();
}
}
//asking users if they want to print again
System.out.println("\nDo you want to print another triangle? Type Yes or No.");
//scan for string answer
String againChoice = scan.next();
//if answer start with Y or y then answer is Yes.
if(againChoice.startsWith("Y") || againChoice.startsWith("y")){
printAgain = true;
}
//if answer start with N or n then answer is No.
else if(againChoice.startsWith("N") || againChoice.startsWith("n")){
printAgain = false;
System.out.println("Bye!");
}
// set input to none again
} while (printAgain == true);
}
//methods to print a triangle
public static void printBase(){
System.out.print("* * * * ");
}
public static void printThree(){
System.out.print(" * * * ");
}
public static void printTwo(){
System.out.print(" * * ");
}
public static void printOne(){
System.out.print(" * ");
}
public static void printSpace(){
System.out.print(" ");
}
}
我已經像這樣更改了您的打印代碼,它可以正常工作:
else if (length % 8 == 0) {
int layer = 0;
int numOfTriangle = length / 4;
System.out.println("Here is your beautiful triangle :D \n");
for (int rowOfTri = numOfTriangle; rowOfTri > 0; rowOfTri--) {
for (int i = 0; i < layer; i++) {
printSpace();
}
for (int i = 0; i < rowOfTri; i++) {
printBase(layer);
}
System.out.println();
for (int i = 0; i < layer; i++) {
printSpace();
}
for (int i = 0; i < rowOfTri; i++) {
printThree(layer);
}
System.out.println();
for (int i = 0; i < layer; i++) {
printSpace();
}
for (int i = 0; i < rowOfTri; i++) {
printTwo(layer);
}
System.out.println();
for (int i = 0; i < layer; i++) {
printSpace();
}
for (int i = 0; i < rowOfTri; i++) {
printOne(layer);
}
System.out.println();
layer++;
}
System.out.println();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.