绘制星号的菱形

huangapple 未分类评论49阅读模式
标题翻译

Drawing diamond of asterisks

问题

import java.util.Scanner;

public class Diamonds {
    public static void main(String args[]) {
        int n, i, j, space = 0;
        System.out.print("Enter the number of rows: ");
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        space = n - 1;
        
        for (j = 0; j <= n; j++) {
            for (i = 0; i < space; i++) {
                System.out.print(" ");
            }
            for (i = j; i <= 2 * j - 1; i++) {
                System.out.print("* ");
            }
            System.out.println();
            space--;
        }
        
        space = 1;
        for (j = n - 1; j > 0; j--) {
            for (i = 0; i < space; i++) {
                System.out.print(" ");
            }
            for (i = j; i <= 2 * j - 1; i++) {
                System.out.print("* ");
            }
            System.out.println();
            space++;
        }
    }
}
英文翻译

I have this program in which you input a number and it prints it into a diamond. I have the top half aligned and figured out, just cant get the bottom half aligned. It's supposed to go up in the number of asterisks and then go back down to one.

import java.util.Scanner;
public class Diamonds
{
    public static void main(String args[])
    {
        int n, i, j, space = 0;
        System.out.print(&quot;Enter the number of rows: &quot;);
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        space = n - 0;
        for (j = 0; j &lt;= n; j++)
        {
            for (i = 0; i &lt; space; i++)
            {
                System.out.print(&quot; &quot;);
            }
                System.out.print(&quot;   &quot;);
            space--;
            for (i = j; i &lt;= 2 * j - 1; i++)
            {
                System.out.print(&quot;* &quot;);
            }
            System.out.println(&quot;   &quot;);
        }
        space = 0;
        for (j = n - 1  ; j &gt; 0; j--)
        {
            for (i = 0; i &lt;= i - n; i++)
            {
                System.out.print(&quot;   &quot;);
            }

            System.out.print(&quot; &quot;);

            space--;

            for (i = j; i &lt;= 2 * j - 1; i++)
            {
                System.out.print(&quot; *&quot;);
            }
            System.out.println(&quot;  c &quot;);
            space=n-1;
        }
    }
}

答案1

得分: -1

我将您的输入放入了一个选项面板,并清理了其他部分的代码:

public static void main(String [] args) {
    try {
        String amt = JOptionPane.showInputDialog("Enter number of rows:");
        int n = Integer.valueOf(amt);
        int space = n;
        for (int j = 0; j <= n; j++) {
            StringBuilder builder = new StringBuilder();
            IntStream.range(0, space).forEach(i -> builder.append(' '));
            builder.append("   ");
            space--;
            IntStream.range(j, 2*j).forEach(i -> builder.append("* "));
            System.out.println(builder);
        }
        space++;
        for (int j = n - 1; j > 0; j--) {
            space++;
            StringBuilder builder = new StringBuilder();
            IntStream.range(0, space).forEach(i -> builder.append(' '));
            builder.append("   ");
            IntStream.range(j, 2*j).forEach(i -> builder.append("* "));
            System.out.println(builder);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
英文翻译

I moved your input into an option pane and cleaned up some of the other code:

public static void main(String [] args) {
	try {
        String amt = JOptionPane.showInputDialog(&quot;Enter number of rows:&quot;);
        int n = Integer.valueOf(amt);
        int space = n;
        for (int j = 0; j &lt;= n; j++) {
        	StringBuilder builder = new StringBuilder();
        	IntStream.range(0, space).forEach(i -&gt; builder.append(&#39; &#39;));
            builder.append(&quot;   &quot;);
            space--;
            IntStream.range(j, 2*j).forEach(i -&gt; builder.append(&quot;* &quot;));
            System.out.println(builder);
        }
        space++;
        for (int j = n - 1; j &gt; 0; j--) {
        	space++;
        	StringBuilder builder = new StringBuilder();
        	IntStream.range(0, space).forEach(i -&gt; builder.append(&#39; &#39;));
            builder.append(&quot;   &quot;);
            IntStream.range(j, 2*j).forEach(i -&gt; builder.append(&quot;* &quot;));
            System.out.println(builder);
        }
	} catch (Exception e) {
		e.printStackTrace();
	}
}

huangapple
  • 本文由 发表于 2020年3月17日 02:28:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/60711372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定