0

i would like to multiply string by an interger value, for what im working on is a health system for a simple game. so what i did was made an ASCII heart and would like to take this away and add to this depending on what the players health is like.

    String heart = "  _  _ ";
    String heart2 =" ( \/ )";
    String heart3 ="  \  / ";
    String heart4 = "  \/  ";

so to output the player health would be like:

    System.out.println(heart*8);
    System.out.println(heart2*8);
    System.out.println(heart3*8);
    System.out.println(heart4*8);

to equal an output like this

      _  _   _  _   _  _   _  _   _  _   _  _   _  _   _  _
     ( \/ ) ( \/ ) ( \/ ) ( \/ ) ( \/ ) ( \/ ) ( \/ ) ( \/ )
      \  /   \  /   \  /   \  /   \  /   \  /   \  /   \  / 
       \/     \/     \/     \/     \/     \/     \/     \/ 

and to take away a health id simply just multiply if by one less number,

    System.out.println(heart*7);
    System.out.println(heart2*7);
    System.out.println(heart3*7);
    System.out.println(heart4*7);

to have an output of

      _  _   _  _   _  _   _  _   _  _   _  _   _  _  
     ( \/ ) ( \/ ) ( \/ ) ( \/ ) ( \/ ) ( \/ ) ( \/ ) 
      \  /   \  /   \  /   \  /   \  /   \  /   \  /   
       \/     \/     \/     \/     \/     \/     \/   

but from what i'm doing nothing seems to be happening as you cannot multiply string this way, could someone please help me multiply this out. Thanks

Coding_Master
  • 23
  • 2
  • 6
  • `String` doesn't support multiplication-operations in java and most other languages since it's hard to define a consistent and logical multiplication-operation for string (for e.g. should `"abc" * 2` mean "abcabc" or "aabbcc" or just multiply each element in the vector of characters by 2?). This task can be solved easily using for-loops, so theres no need at all for a multiplication-operation anyways. –  Nov 20 '15 at 23:41
  • Check the link @Kenney sent and also use string array – Burak Karasoy Nov 20 '15 at 23:41

2 Answers2

2
public static void multiply(int x, String heart)
{
    for (int i = 0; i < x; i++)
        System.out.print(heart);
    System.out.println(); // need for next line
}

// For x = 2; heart = "<3 "
// multiply(x, heart) would output "<3 <3"

_

// ...

String heart = "  _  _ ";
String heart2 =" ( \\/ )";
String heart3 ="  \\  / ";
String heart4 = "   \\/  ";
int x = 0; // a number you want to print

multiply(x, heart);
multiply(x, heart2);
multiply(x, heart3);
multiply(x, heart4);

// ...
Riley Carney
  • 804
  • 5
  • 18
2

If you try printing line by line instead of the whole pattern at once

class PrintHeartzzz{

    public static void main(String[] args){
      StringBuilder sb =new StringBuilder();
      int n=8;

      prepareLine("  _  _ ",sb,n);
      prepareLine(" ( \\/ )",sb,n);
      prepareLine("  \\  / ",sb,n);
      prepareLine("   \\/  ",sb,n);

      System.out.println(sb);

    }

    private static void prepareLine(String patternToDraw,StringBuilder sb,int nb){
      for(int i=0;i<nb;i++){
        sb.append(patternToDraw);
      }
        sb.append("\n");
    }

}
Tahar Bakir
  • 716
  • 5
  • 14