简体   繁体   English

使文字分组

[英]Making text stay in a group

I have a fairly simple question regarding Java. 我有一个关于Java的相当简单的问题。 I am currently learning Java and one of the homework questions has me stumped. 我目前正在学习Java,而一项家庭作业问题使我感到困惑。 The goal is to create a triangle with the little figure. 目标是创建一个带有小图形的三角形。 I believe I have the idea down however, I cannot get the whole figure of the guy to move to the right. 我相信我有这个主意,但是,我无法使这个家伙的整个角色向右移动。

The code I have so far is: 到目前为止,我的代码是:

public class LittleGuy {
    public static final int NUMBER_OF_GUYS = 5; // determines the number of guys.
        public static void main(String[] args) {
            for (int line = 1; line <= NUMBER_OF_GUYS; line++){
                for (int j = 1; j <= (-5 * line + 25); j++){// uses the algorithm.
                    System.out.print(" ");
       }
        guy();

    }
}
public static void guy(){
    System.out.print("  o  ******\n /|\\ *\n / \\ *");
    System.out.println();
    }
}

Basically, the body and the head aren't supposed to be separated. 基本上,不应将身体和头部分开。 I'm having trouble keeping them together. 我很难将它们保持在一起。 I am assuming that it is a fairly simple fix I am either unaware of or completely over looking. 我假设这是一个相当简单的修复程序,我要么不知道,要么完全忽略了。 Any information or thought would be greatly appreciated. 任何信息或思想将不胜感激。 Thanks again. 再次感谢。

You are only printing out the spaces before the first line of your little guy, inside the guy() method, you are still printing the rest of the guy on the first line. 您只是在小家伙的第一行之前打印出空格,在guy()方法内部,您仍在第一行中打印其余家伙。

I'd suggest making a printSpaces(int numSpaces) method which prints numSpaces spaces, and changing the guy method to take the number of spaces to print at the beginning of each line. 我建议创建一个printSpaces(int numSpaces)方法来打印numSpaces空格,并更改guy方法以获取要在每行开头打印的空格数。

public class LittleGuy {

public static final int NUMBER_OF_GUYS = 5; // determines the number of guys.
    public static void main(String[] args) {

        for (int line = 1; line <= NUMBER_OF_GUYS; line++){
            int counter=0;
            for (int j = 1; j <= (-5 * line + 25); j++){// uses the algorithm.
               System.out.print(" ");
               counter++;
   }
    guy(counter);

}
}
public static void guy(int count){

System.out.print("  o  ******");
System.out.print("\n");

for(int i=0;i<count;i++)
System.out.print(" ");    

System.out.print("/|\\ *");
System.out.print("\n");

for(int i=0;i<count;i++)
System.out.print(" ");

System.out.print("/ \\ *");

System.out.println();
}

} }

The problem was : In the guy() method you use \\n to print the guy. 问题是:在guy()方法中,您使用\\n打印该家伙。 But it will not take care of the white-spaces you printed in-order to achieve your algorithm (inside the for loop). 但这不会照顾您为了实现算法而打印的空白(在for循环内)。

How it was solved : place a counter in the for loop to count how many white-spaces were printed and use that counter in the guy() method to print required white-spaces. 解决方法:在for循环中放置一个计数器,以计算打印了多少空白,并在guy()方法中使用该计数器打印所需的空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM