[英]Rhombus with letters - Java
I am new to programming and started with learning c# and now java.我是编程新手,从学习 c# 开始,现在学习 java。 I came across a task creating a rhombus where the user inputs the height (odd numbers only) and the char for the rhombus.
我遇到了一个创建菱形的任务,用户输入菱形的高度(仅奇数)和字符。 I created a for loop for the height and another loop for the characters.
我为高度创建了一个 for 循环,为字符创建了另一个循环。 Here is my output:
这是我的输出:
h: 7
c: k
k
jkj
ijkji
hijkjih
ghijkjihg
But I want the output to be:但我希望输出是:
h: 7
c: k
k
jkj
ijkji
hijkjih
ijkji
jkj
k
How can I develop my logic to apply it to my code.我如何开发我的逻辑以将其应用于我的代码。 Here is my code:
这是我的代码:
Scanner in = new Scanner(System.in);
System.out.print("h: ");
int h = in.nextInt();
System.out.print("c: ");
char c = in.next().charAt(0);
if(h%2==0){
System.out.println("Invalid number!");
return;
}
int count = 1;
int space = 1;
for (int i = 2; i < h; i++)
{
for (int spc = h - space; spc > 0; spc--)
{
System.out.print(" ");
}
if (i < h)
{
space++;
}
else {
space--;
}
for (int j = 0; j < count; j++)
{
System.out.print(c);
if (j < count/2)
{
c++;
}
else {
c--;
}
}
if (i < h)
{
count = count + 2;
}
else {
count = count - 2;
}
System.out.println();
}
Any help is highly appreciated.任何帮助都受到高度赞赏。
Your code contains the following flaws:您的代码包含以下缺陷:
count
and space
variables depend on the values of i
and h
, which makes it very hard to keep track of and understand. count
和space
变量取决于i
和h
的值,这使得跟踪和理解变得非常困难。 You should avoid hidden dependencies in your code in generalc
all the time.c
的值。 It makes it very hard to keep track of.i = 2
, count/2
, incrementing by 2
i = 2
, count/2
,递增2
You have one loop which increments i
.你有一个循环增加
i
。 What you need is a second loop which decrements the value of i
.您需要的是第二个循环,它减少
i
的值。 And you should also use the same approach for printing of the characters (2 loops for both sides).并且您还应该使用相同的方法来打印字符(两边各有 2 个循环)。 Let me show you:
让我演示给你看:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// load parameters
System.out.print("h: ");
int h = in.nextInt();
System.out.print("c: ");
char c = in.next().charAt(0);
// validate parameters
if (h % 2 == 0) {
System.out.println("Invalid number!");
return;
}
for(int i = 0; i <= h/2; i++) {
printSpaces((h+1) / 2 - i - 1);
printLine(c, i);
System.out.println();
}
for(int i = h/2-1; i >= 0; i--) {
printSpaces((h+1) / 2 - i - 1);
printLine(c, i);
System.out.println();
}
}
private static void printLine(char character, int sideWidth) {
for (int j = sideWidth; j >= 0; j--)
System.out.print((char) (character - j));
for (int j = 1; j <= sideWidth; j++)
System.out.print((char) (character - j));
}
private static void printSpaces(int numberOfSpaces) {
for (int i = 0; i < numberOfSpaces; i++) {
System.out.print(" ");
}
}
which gives you the desired output.这为您提供了所需的输出。
public class Rhombusstar
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter N : ");
int n=sc.nextInt();
System.out.print("Enter Symbol : ");
char c = sc.next().charAt(0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=n;j++)
{
System.out.print(c);
}
System.out.println();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.