简体   繁体   中英

Surrounding a string with smileys

I've surrounded the string with smiley's but it's printing too many and everything I try doesn't seem to work.

My problem:

:):):):):):):):):):):):)
:) Surroundthis :)
:):):):):):):):):):):):)

My code:

private static void printWithSmileys(String characterString) {
    loop(characterString);
    if (characterString.length() % 2 == 0) {
        System.out.println(":) " + characterString + " :)");
    } else {
        System.out.println(":) " + characterString + "  :)"); // If string is an odd number, add a space
    }
    loop(characterString); 
}

public static void loop(String characterString) {
    String smile = ":)";
    int length = characterString.length(); // Can test different lengths
    for (int i = 0; i < length; i++) {
        System.out.print(smile);
    }
    System.out.println();
}

Note: If the string is small enough, it will work but only for smaller strings

:):):):):):)
:) Method :)
:):):):):):)

You print out X smileys where X is the length of the string. Instead you should print the length of the string / 2 (since a smiley is 2 characters) + 2 more for the corners.

For method it accidentally works, because 6 smileys are 12 characters, and the word method is 6 characters and becomes 12 too when prefixed and postfixed with a smiley and a space.

This line should fix the issue:

int length = Math.ceil(characterString.length() / 2) + 3;

The 3 is for the two smileys on the corner and one extra to cover the spaces.

Here is the full version of printing characters/symbols surrounded by a frame of smiles or any other characters/symbols. The program functions regardless of whether the number of the characters/symbols in the units constituting the frame and in the units surrounded by the frame is an even or odd number, or the value of the number itself - for instance, "X" surrounded with ":)", "XXX" surrounded with ":)))))))))", etc.

import java.util.*;

public class SimleysFullVersion {

 public static void main(String[] args) {
     Scanner reader = new Scanner (System.in);
     System.out.println("Enter characters: ");
     // charInTheFrame designates the characters/symbols in the frame
     String charInTheFrame = reader.nextLine();
        printWithSmileys(charInTheFrame);
 }

private static void printWithSmileys(String charInTheFrame) {

    Scanner reader = new Scanner (System.in);
    System.out.println("Enter frame characters: ");
    // frameChar designates the characters/symbols that compose the frame
    String frameChar = reader.nextLine();

     int charInTheFrameLength = charInTheFrame.length();
     int frameCharLength = frameChar.length();
     // emptySpace designates the space between the characters/symbols composing the frame and those within the frame
     String emptySpace = "";
     int emptySpaceLength = 0;

     System.out.println("The picture looks like this: ");

/*First Major Possibility*/       
if (frameCharLength > charInTheFrameLength) { 


/*Subgroup 1: First Major Possibility*/   
  if ((frameCharLength - charInTheFrameLength) % 2 == 0) {

     while (emptySpaceLength < (frameCharLength - charInTheFrameLength) / 2) {
        emptySpace += " ";
        emptySpaceLength++;
     }
        printBottomTopRow(3, frameChar);
        System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + frameChar);
        printBottomTopRow(3, frameChar);
  } 

/*Subgroup 2: First Major Possibility*/
  else if (frameCharLength - charInTheFrameLength == 1) {

      while (emptySpaceLength < (2 * frameCharLength - charInTheFrameLength) / 2) {
          emptySpace += " ";
          emptySpaceLength++;
      }
          int bottomTopRow = ((charInTheFrameLength + 4 * (frameCharLength)) / frameCharLength);
          printBottomTopRow(bottomTopRow, frameChar);
          if (charInTheFrameLength%2 !=0){
          System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + " " + frameChar);
          }
            else
             System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + frameChar);
             printBottomTopRow(bottomTopRow, frameChar);
  } 

/*Subgroup 3: First Major Possibility*/          
  else {

     while (emptySpaceLength < (frameCharLength - charInTheFrameLength) / 2) {
         emptySpace += " ";
         emptySpaceLength++;
     }
           int bottomTopRow = ((charInTheFrameLength + 3 * (frameCharLength)) / frameCharLength);
           printBottomTopRow(bottomTopRow, frameChar);
           System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + " " + frameChar);
           printBottomTopRow(bottomTopRow, frameChar);
  }

}


/*Second Major Possibility*/      
if (frameCharLength == charInTheFrameLength){ 

     while (emptySpaceLength < frameCharLength) {
         emptySpace += " ";
         emptySpaceLength++;
     }
        int bottomTopRow = ((charInTheFrameLength + 4 * (frameCharLength)) / frameCharLength);
        printBottomTopRow(bottomTopRow, frameChar);
        System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + frameChar);
        printBottomTopRow(bottomTopRow, frameChar);
}


/*Third Major Possibility*/      
if (frameCharLength < charInTheFrameLength) { 

/*Subgroup 1: Third Major Possibility*/ 
  if (charInTheFrameLength % frameCharLength == 0){

     while (emptySpaceLength < frameCharLength) {
         emptySpace += " ";
         emptySpaceLength++;
     }
         int bottomTopRow = ((charInTheFrameLength + 4 * (frameCharLength)) / frameCharLength);
         printBottomTopRow(bottomTopRow, frameChar);
         System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + frameChar);
         printBottomTopRow(bottomTopRow, frameChar);
  }

/*Subgroup 2: Third Major Possibility*/   
  else if  ((frameCharLength - (charInTheFrameLength % frameCharLength)) == 1){

      while (emptySpaceLength < (frameCharLength+1)/2) {
          emptySpace += " ";
          emptySpaceLength++;
      }
          int bottomTopRow = ((4*frameCharLength + (charInTheFrameLength/frameCharLength)*(frameCharLength)) / frameCharLength);
          printBottomTopRow(bottomTopRow, frameChar);
          if ((frameCharLength+1)%2 !=0){
          System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + " " + frameChar);
          }
            else 
               System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + frameChar);
               printBottomTopRow(bottomTopRow, frameChar);
 }

/*Subgroup 3: Third Major Possibility*/         
else  {

      while (emptySpaceLength < (frameCharLength-(charInTheFrameLength % frameCharLength))/2) {
          emptySpace += " ";
          emptySpaceLength++;
      }
          int bottomTopRow = ((3*frameCharLength + (charInTheFrameLength/frameCharLength)*(frameCharLength)) / frameCharLength);
          printBottomTopRow(bottomTopRow, frameChar);
          if ((frameCharLength-(charInTheFrameLength % frameCharLength))%2 !=0){
          System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + " " + frameChar);
          }
            else 
              System.out.println(frameChar + emptySpace + charInTheFrame + emptySpace + frameChar);
              printBottomTopRow(bottomTopRow, frameChar);
            }
 }

}

    public static void printBottomTopRow(int rowLength, String symbol) {
        for (int i = 0; i < rowLength; i++) {
            System.out.print(symbol);
        }
        System.out.println();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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