标题翻译
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("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 0;
for (j = 0; j <= n; j++)
{
for (i = 0; i < space; i++)
{
System.out.print(" ");
}
System.out.print(" ");
space--;
for (i = j; i <= 2 * j - 1; i++)
{
System.out.print("* ");
}
System.out.println(" ");
}
space = 0;
for (j = n - 1 ; j > 0; j--)
{
for (i = 0; i <= i - n; i++)
{
System.out.print(" ");
}
System.out.print(" ");
space--;
for (i = j; i <= 2 * j - 1; i++)
{
System.out.print(" *");
}
System.out.println(" c ");
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("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();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论