编程语言
首页 > 编程语言> > java-将字符串显示为框

java-将字符串显示为框

作者:互联网

我只是无法弄清楚如何获得想要的结果.这是我的代码

 import java.util.Scanner;
 public class StringInABox
 {
 public static void main(String[]args)
 {
      Scanner scan = new Scanner(System.in);

      System.out.println("Enter a phrase or word!");
      String Phrase = scan.nextLine();
      String CapPhrase = Phrase.toUpperCase();
      int PhraseLength = CapPhrase.length();
      String SidePhrase = CapPhrase.substring(1);
      int SidePhraseL = SidePhrase.length()-1;

      for (int Letter = 0; Letter<PhraseLength; Letter++)
           System.out.print(CapPhrase.charAt(Letter)+" ");
      System.out.println();

      for (int Letter = 0; Letter<SidePhraseL; Letter++)
          System.out.println(SidePhrase.charAt(Letter));

      for (int Letters = SidePhraseL-1; Letters>=0; Letters--)
      { for (int Space=0; Space <= PhraseLength*2-3;Space++)
           System.out.print(" ");
      System.out.println(SidePhrase.charAt(Letters));}


      for (int Letter = PhraseLength-1; Letter>=0; Letter--)
          System.out.print(CapPhrase.charAt(Letter)+" ");
      }
  }         

结果应该是这样的:

   H E L P 
   E     L
   L     E
   P L E H

但是我只能得到:

   H E L P 
   E
   L
         L
         E
   P L E H

我的想法已经用完了.我是一个初学者,因此不应该使用高级编码.

解决方法:

您非常接近,但是这里的问题是for循环的顺序.要获得所需的输出,如下所示:

E          L
L          E

您需要在同一循环中打印这两个字符,即在同一行上打印时.因此,您需要分别存储E和L值的IndexFromFront和IndexFromBack变量.

初始循环后,打印字符H E L P

for (int Letter = 0; Letter < PhraseLength; Letter++)
   System.out.print(CapPhrase.charAt(Letter) + " ");
System.out.println();

您需要从第一个索引(即1)开始访问E,IndexFromBack的值为PhraseLength-1-(IndexFromFront).现在您已经有了要从原始字符串打印的值的索引,您需要获得正确的间距,即2 *(PhraseLength-1)-1,因为每个字符后都有一个空格.所以你的第二个循环来打印行

E          L
L          E

应该如下:

for (int Letter = 1; Letter < PhraseLength-1; Letter++) {
    int IndexFromFront = Letter;
    int IndexFromBack = PhraseLength-1-Letter;
    // Print character from the start of string at given Index 
    System.out.print(CapPhrase.charAt(IndexFromFront));
    // Print required spaces
    for (int Space = 0; Space < 2*(PhraseLength-1)-1; Space++) {
        System.out.print(" ");
    }
    // End space print
    // Print character from the end of string matching the required index
    System.out.print(CapPhrase.charAt(IndexFromBack));
    // Printing on this line is complete, so print a new line.
    System.out.println();
}

最后一行,即给定输入字符串的反行,可以使用循环进行打印,如下所示:

for (int Letter = PhraseLength - 1; Letter >= 0; Letter--)
   System.out.print(CapPhrase.charAt(Letter) + " ");
}

因此,最终的代码如下:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class StringInABox {
 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);

  System.out.println("Enter a phrase or word!");
  String Phrase = scan.nextLine();
  String CapPhrase = Phrase.toUpperCase();
  int PhraseLength = CapPhrase.length();

  for (int Letter = 0; Letter < PhraseLength; Letter++)
   System.out.print(CapPhrase.charAt(Letter) + " ");
  System.out.println();

  for (int Letter = 1; Letter < PhraseLength - 1; Letter++) {
   int IndexFromFront = Letter;
   int IndexFromBack = PhraseLength - 1 - Letter;
   System.out.print(CapPhrase.charAt(IndexFromFront));
   // Print required spaces
   for (int Space = 0; Space < 2 * (PhraseLength - 1) - 1; Space++) {
    System.out.print(" ");
   }
   // End space print
   System.out.print(CapPhrase.charAt(IndexFromBack));
   System.out.println();
  }

  for (int Letter = PhraseLength - 1; Letter >= 0; Letter--)
   System.out.print(CapPhrase.charAt(Letter) + " ");
 }
}

这是HELPINGYOUOUT的示例输出

Enter a phrase or word!
H E L P I N G Y O U O U T 
E                       U
L                       O
P                       U
I                       O
N                       Y
G                       G
Y                       N
O                       I
U                       P
O                       L
U                       E
T U O U O Y G N I P L E H 

编辑:更具可读性的代码

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class StringInABox {

    public static void printSpaces(int phraseLength) {
        for (int space = 0; space < 2 * (phraseLength - 1) - 1; space++) {
            System.out.print(" ");
        }
    }

    public static void printTopRow(String capPhrase, int phraseLength) {
        for (int letter = 0; letter < phraseLength; letter++)
            System.out.print(capPhrase.charAt(letter) + " ");
        System.out.println();
    }

    public static void printIntermediateBoxRows(String capPhrase, int phraseLength) {
        for (int letter = 1; letter < phraseLength - 1; letter++) {
            int indexFromFront = letter;
            int indexFromBack = phraseLength - 1 - letter;
            System.out.print(capPhrase.charAt(indexFromFront));
            // Print required spaces
            printSpaces(phraseLength);
            // End space print
            System.out.print(capPhrase.charAt(indexFromBack));
            System.out.println();
        }
    }

    public static void printLastRow(String capPhrase, int phraseLength) {
        for (int letter = phraseLength - 1; letter >= 0; letter--)
            System.out.print(capPhrase.charAt(letter) + " ");
    }


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter a phrase or word!");
        String phrase = scan.nextLine();
        String capPhrase = phrase.toUpperCase();
        int phraseLength = capPhrase.length();

        // Print the box
        printTopRow(capPhrase, phraseLength);
        printIntermediateBoxRows(capPhrase, phraseLength);
        printLastRow(capPhrase, phraseLength);
    }
}

标签:whitespace,string,java
来源: https://codeday.me/bug/20191025/1927498.html