I have a problem that I hope you can help with.
The Java program should print hearts in ASCII-art style, based on the input N
given by a user.
Information:
- Characters to print: ♡ ♥
- The printing of the top and bottom part of the heart can be done separately.
- The solution must be based on for-loops.
N
determines the top part of the heart:- The slanted outer sides at the top have
N
hearts in them. - The flat parts at the top have
N
hearts in them. - The gap between the two flat parts is
N
hearts wide.
- The slanted outer sides at the top have
Examples:
My current code:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of hearts you want to print");
int userInput = scan.nextInt();
printTop(userInput);
}
public static void printTop(int userInput) {
String row = "";
int width = 2 * (userInput - 1) + 3 * userInput;
for (int height = 0; height < userInput; height++) {
for (int i = 0; i < userInput - 1; i++) {
row += "♡";
}
for (int i = 0; i < userInput; i++) {
row += "♥";
}
for (int i = 0; i < userInput; i++) {
row += "♡";
}
for (int i = 0; i < userInput; i++) {
row += "♥";
}
for (int i = 0; i < userInput - 1; i++) {
row += "♡";
}
row += "\n";
}
System.out.println(row);
}
Thoughts:
- The first line of the heart is based on:
2 * (userInput - 1) + 3 * userInput
- The colored hearts must increase by 2 for each line.
- The center hearts must be reduced by 2 for each line.
- The transparent hearts on the side must be reduced by 1 for each line.
Questions:
- How can I get the different types of hearts to do their "job" on each line?