[英]Printing a word in the middle of a line in Java
I printed an equilateral triangle and in the middle of a certain row I want to print a word (taken as input). 我打印了一个等边三角形,在某行的中间,我想打印一个单词(作为输入)。 My code is as follows:
我的代码如下:
for (int rows=1; rows <= getHeight(); rows++)
{
char charToPrint = '*';
if(rows == getRowNum()){
for(int i=0;i<getTextLabel().length();i++){
charToPrint = getTextLabel().charAt(i);
}
}
for (int spaces=1; spaces <= number_of_stars; spaces++)
{
System.out.print(" ");
}
for (int star=1; star <= rows; star++)
{
System.out.print(charToPrint);
System.out.print(" ");
}
System.out.println("");
number_of_stars = number_of_stars - 1;
}
Output: 输出:
*
* *
* * *
* * * *
* * * * *
* * * * * *
A A A A A A A
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
but I wanted the output to be like this: 但我希望输出如下所示:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* L R E Z A *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
You can try the folllowing code as it seems to work for input LREZA
: 您可以尝试以下代码,因为它似乎可用于输入
LREZA
:
public static void main(String[] args) {
int userRowNumber = 15;
int height = 20;
int number_of_stars = height;
String charToPrint;
String inputWord = "LREZA";
int inputSize = inputWord.length();
if(userRowNumber < (inputSize*2) ) {
System.out.println("Not enough space in row to display input text");
return;
}
for (int rows=1; rows <= height; rows++)
{
charToPrint = "*";
if(rows == userRowNumber)
{
int startWordFrom = (userRowNumber - inputWord.length())/2;
printSpace(number_of_stars);
for (int spaces=0; spaces <rows; spaces++)
{
if(spaces >= startWordFrom && spaces <(inputSize*2) ) {
System.out.print(inputWord.charAt(spaces-startWordFrom));
System.out.print(" ");
} else {
System.out.print("*");
System.out.print(" ");
}
}
System.out.println("");
number_of_stars--;
rows++;
}
printSpace(number_of_stars);
printCharacter(charToPrint,rows);
System.out.println("");
number_of_stars = number_of_stars - 1;
}
}
public static void printSpace(int number_of_stars) {
for (int spaces=1; spaces <= number_of_stars; spaces++)
{
System.out.print(" ");
}
}
public static void printCharacter(String charToPrint, int numberOfTimes) {
for (int star=1; star <= numberOfTimes; star++)
{
System.out.print(charToPrint);
System.out.print(" ");
}
}
There are certain points to observe while handling the input string. 在处理输入字符串时,有一些要注意的地方。 We need to try to centralize the input and also need to put a space after every alphabet of input.
我们需要尝试集中化输入,还需要在每个输入字母后放置一个空格。 The output I got out of this program is:
我从该程序得到的输出是:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * L R E Z A * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
which is I assume expected. 我认为这是预期的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.