Java 根据用户输入绘制ASCII艺术心脏
我有个问题,希望你能帮我解决 Java程序应该根据用户给出的输入N打印ASCII红心 信息:Java 根据用户输入绘制ASCII艺术心脏,java,loops,for-loop,ascii,ascii-art,Java,Loops,For Loop,Ascii,Ascii Art,我有个问题,希望你能帮我解决 Java程序应该根据用户给出的输入N打印ASCII红心 信息: 要打印的字符:♡ ♥ 心脏顶部和底部的打印可以单独完成 解决方案必须基于for循环 N决定心脏的顶部:1。顶部倾斜的外侧有N颗心。2.顶部的扁平部分有N颗心。3.两个扁平零件之间的间隙为N 示例: 我的当前代码: publicstaticvoidmain(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); System.out.println(“输入要打印的红心数”);
- 要打印的字符:♡ ♥李>
- 心脏顶部和底部的打印可以单独完成
- 解决方案必须基于for循环
- N决定心脏的顶部:1。顶部倾斜的外侧有N颗心。2.顶部的扁平部分有N颗心。3.两个扁平零件之间的间隙为N
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入要打印的红心数”);
int userInput=scan.nextInt();
printTop(用户输入);
}
publicstaticvoidprinttop(int-userInput){
字符串行=”;
int-width=2*(userInput-1)+3*userInput;
对于(int height=0;height
想法:
- 心脏的第一行是基于:
2*(userInput-1)+3*userInput
- 每行彩色心形必须增加2
- 每行的中心线必须减少2
- 侧面的透明心形必须每行减少1
- 我怎样才能让不同类型的心脏在每一条线上完成他们的“工作”
import java.util.Scanner;
public class A {
public static final char COLORED = '♥';
public static final char WHITE = '♡';
public static void printTop(int userInput) {
StringBuffer row = new StringBuffer();
int width = 2 * (userInput - 1) + 3 * userInput;
int outerHearts = userInput - 1; // white hearts at the outside
int heartsSection1 = userInput; // left colored section or complete colored line from the widthest point
int heartsSection2 = userInput; // right color section or 0 from the widthest point
int innerHearts = userInput; // white hearts between 2 colored sections
boolean upperPart = true;
while (heartsSection1 > 0) {
for (int i = 0; i < outerHearts; i++) {
row.append(WHITE);
}
for (int i = 0; i < heartsSection1; i++) {
row.append(COLORED);
}
for (int i = 0; i < innerHearts; i++) {
row.append(WHITE);
}
for (int i = 0; i < heartsSection2; i++) {
row.append(COLORED);
}
for (int i = 0; i < outerHearts; i++) {
row.append(WHITE);
}
row.append("\n");
if (upperPart) {
if (outerHearts > 0) {
outerHearts--;
innerHearts = Math.max(0, innerHearts - 2);
if (outerHearts == 0) {
heartsSection1 = width;
heartsSection2 = 0;
} else {
heartsSection1 += 2;
heartsSection2 += 2;
}
} else {
// line with only colored hearts. From now on the hearts reduce
upperPart = false;
}
}
if (!upperPart) {
outerHearts++;
heartsSection1 -= 2;
}
if (heartsSection1 <= 0)
break;
}
System.out.println(row.toString());
}
public static void main(String args[]) {
System.out.println("Enter the number of hearts you want to print");
Scanner scan = new Scanner(System.in);
int userInput = scan.nextInt();
printTop(userInput);
}
}
import java.util.Scanner;
公共A类{
公共静态最终字符集♥';
公共静态最终字符♡';
公共静态void printTop(int userInput){
StringBuffer行=新的StringBuffer();
int-width=2*(userInput-1)+3*userInput;
int-outerherts=userInput-1;//外部为白心
int heartsSection1=userInput;//从最宽点开始的左彩色部分或完整彩色线
int heartsSection2=userInput;//右颜色部分或最宽点的0
int innerHearts=userInput;//两个彩色部分之间的白色心脏
布尔上半部分=真;
而(heartsSection1>0){
for(int i=0;i0){
外部艺术——;
innerHearts=Math.max(0,innerHearts-2);
如果(外部艺术==0){
heartsSection1=宽度;
heartsSection2=0;
}否则{
心脏节1+=2;
heartsSection2+=2;
}
}否则{
//只有彩色的心线。从现在起,心线减少
上部=假;
}
}
如果(!上部){
外部艺术++;
心脏节1-=2;
}
如果(heartsSection1Nice挑战:
import java.util.Scanner;
public class A {
public static final char COLORED = '♥';
public static final char WHITE = '♡';
public static void printTop(int userInput) {
StringBuffer row = new StringBuffer();
int width = 2 * (userInput - 1) + 3 * userInput;
int outerHearts = userInput - 1; // white hearts at the outside
int heartsSection1 = userInput; // left colored section or complete colored line from the widthest point
int heartsSection2 = userInput; // right color section or 0 from the widthest point
int innerHearts = userInput; // white hearts between 2 colored sections
boolean upperPart = true;
while (heartsSection1 > 0) {
for (int i = 0; i < outerHearts; i++) {
row.append(WHITE);
}
for (int i = 0; i < heartsSection1; i++) {
row.append(COLORED);
}
for (int i = 0; i < innerHearts; i++) {
row.append(WHITE);
}
for (int i = 0; i < heartsSection2; i++) {
row.append(COLORED);
}
for (int i = 0; i < outerHearts; i++) {
row.append(WHITE);
}
row.append("\n");
if (upperPart) {
if (outerHearts > 0) {
outerHearts--;
innerHearts = Math.max(0, innerHearts - 2);
if (outerHearts == 0) {
heartsSection1 = width;
heartsSection2 = 0;
} else {
heartsSection1 += 2;
heartsSection2 += 2;
}
} else {
// line with only colored hearts. From now on the hearts reduce
upperPart = false;
}
}
if (!upperPart) {
outerHearts++;
heartsSection1 -= 2;
}
if (heartsSection1 <= 0)
break;
}
System.out.println(row.toString());
}
public static void main(String args[]) {
System.out.println("Enter the number of hearts you want to print");
Scanner scan = new Scanner(System.in);
int userInput = scan.nextInt();
printTop(userInput);
}
}
import java.util.Scanner;
公共A类{
公共静态最终字符集♥';
公共静态最终字符♡';
公共静态void printTop(int userInput){
StringBuffer行=新的StringBuffer();
int-width=2*(userInput-1)+3*userInput;
int-outerherts=userInput-1;//外部为白心
int heartsSection1=userInput;//从最宽点开始的左彩色部分或完整彩色线
int heartsSection2=userInput;//右颜色部分或最宽点的0
int innerHearts=userInput;//两个彩色部分之间的白色心脏
布尔上半部分=真;
而(heartsSection1>0){
for(int i=0;i0){
外部艺术——;
innerHearts=Math.max(0,innerHearts-2);
如果(外部艺术==0){
heartsSection1=宽度;
heartsSection2=0;
}否则{
心脏节1+=2;
heartsSection2+=2;
}
}否则{
//只有彩色心形的线条。从零开始