2015-05-13 121 views
1

我打印了一個等邊三角形,並且在某一行的中間我想打印一個單詞(作爲輸入)。我的代碼如下:在Java中打印一個單詞

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; 
    } 

輸出:

  * 
     * * 
     * * * 
     * * * * 
     * * * * * 
    * * * * * * 
    A A A A A A A 
    * * * * * * * * 
    * * * * * * * * * 
* * * * * * * * * * 

,但我想輸出是這樣的:

  * 
     * * 
     * * * 
     * * * * 
     * * * * * 
    * * * * * * 
    * L R E Z A * 
    * * * * * * * * 
    * * * * * * * * * 
* * * * * * * * * * 
+0

單詞的長度是否等於我們想要打印的rownumber? –

+0

@akhil_mittal等於或小於它 –

+0

輸入詞將有空格像'L R E Z A'或不像'LREZA'? –

回答

0

你可以試試folllowing代碼,因爲它似乎爲輸入而工作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(" "); 
     } 
    } 

處理輸入字符串時有一些要注意的地方。我們需要嘗試集中輸入,並且還需要在每個輸入字母后放置一個空格。我從這個程序中得到的輸出是:

    * 
        * * 
        * * * 
       * * * * 
       * * * * * 
       * * * * * * 
       * * * * * * * 
      * * * * * * * * 
      * * * * * * * * * 
      * * * * * * * * * * 
      * * * * * * * * * * * 
     * * * * * * * * * * * * 
     * * * * * * * * * * * * * 
     * * * * * * * * * * * * * * 
     * * * * * L R E Z A * * * * * 
    * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 

這是我所預計的。

+0

但它不適用於height = 10和inputRow = 7 ..它給我outOfRange異常 –

+0

實際上,對於大小爲n的輸入,所需的空間爲2n(n代表輸入,n代表空間)我已經在頂部添加了這個條件。此外,您可能需要根據需要在這裏和那裏進行一些更改,但是此代碼將爲您提供如何處理此問題的想法。 –