[英]I'm having trouble making a diamond shape with loops
您有一个n
输入,它代表菱形将具有的行的一半。 我能够制造钻石的上半部分,但对下半部分感到非常沮丧。 我似乎无法理解。 我不是在这里问我需要的特定代码,但是您能指出正确的方向并给我一些如何编写此代码的提示/技巧吗? 另外,如果我打算以错误的方式使用该程序,请随时告诉我并告诉我如何使用该程序。
底部的菱形代表输入5 。 n-1代表每个星号左侧的空格。 谢谢您的帮助!
public static void printDiamond(int n)
{
for(int i=0;i<n;i++)
{
for(int a=0;a<(n-(i+1));a++)
{
System.out.print(" ");
}
System.out.print("*");
for(int b=0; b<(i*2);b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
}
** What I need ** What I have currently
*--* *--*
*----* *----*
*------* *------*
*--------* *--------*
*--------*
*------*
*----*
*--*
**
public static void main(String[] args) {
int n = 10;
for (int i = 1 ; i < n ; i += 2) {
for (int j = 0 ; j < n - 1 - i / 2 ; j++)
System.out.print(" ");
for (int j = 0 ; j < i ; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7 ; i > 0 ; i -= 2) {
for (int j = 0 ; j < 9 - i / 2 ; j++)
System.out.print(" ");
for (int j = 0 ; j < i ; j++)
System.out.print("*");
System.out.print("\n");
}
}
产量
*
***
*****
*******
*********
*******
*****
***
*
只需反转循环:
for(int i=n-1;i>=0;i--)
{
for(int a=0;a<(n-(i+1));a++)
{
System.out.print(" ");
}
System.out.print("*");
for(int b=0; b<(i*2);b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
由于您已经形成了一半的钻石,因此只需反过来再次运行循环,例如:
public static void printDiamond(int n)
{
for (int i = 0; i < n; i++)
{
for (int a = 0; a < (n - (i + 1)); a++)
{
System.out.print(" ");
}
System.out.print("*");
for (int b = 0; b < (i * 2); b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
for (int i = n-1; i >= 0; i--)
{
for (int a = 0; a < (n - (i + 1)); a++)
{
System.out.print(" ");
}
System.out.print("*");
for (int b = 0; b < (i * 2); b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
}
每当我看到一种对称时,递归就会响起。 我只为您和其他感兴趣的人发布更多信息。 开始时,递归可能更难掌握,但是由于您已经有了基于循环的解决方案,因此与递归进行对比将清楚地概述优缺点。 我的建议,不要错过机会参加它:)
递归解决方案:
static int iteration = 0;
public static void printDiamond(int n) {
int numberOfBlanks = n - iteration;
int numberOfDashes = iteration * 2;
String blank = new String(new char[numberOfBlanks]).replace("\0", " ");
String dash = new String(new char[numberOfDashes]).replace("\0", "-");
String star = "*";
String row = blank + star + dash + star + blank;
// printing the rows forward
System.out.println(row);
iteration++;
if (iteration < n) {
printDiamond(n);
}
// printing the rows backward
System.out.println(row);
}
首先,不要与奇怪的新String(new char [numberOfBlanks])。replace(“ \\ 0”,“”)混淆; 用Java构造一个带有重复字符的字符串是一个巧妙的技巧,例如new String(new char [5])。replace(“ \\ 0”,“ +”); 将创建以下字符串+++++
递归位说明。 直到递归停止,第二个println才会运行。 停止条件由迭代<n定义。 直到该点为止,将打印直到该点为止的行。 所以像这样:
iteration 1. row = **
iteration 2. row = *--*
iteration 3. row = *----*
iteration 4. row = *------*
iteration 5. row = *--------*
递归停止,然后执行其余代码,但顺序相反。 所以只打印第二个println,并且row变量的值如下
continuing after 5 iteration row = *--------*
continuing after 4 iteration row = *------*
continuing after 3 iteration row = *----*
continuing after 2 iteration row = *--*
continuing after 1 iteration row = **
我没有深入研究背后的机制,没有足够的资源,这只是为了吸引您。 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.