2013-10-11 30 views
1

因此,我被分配到使用Java中的星号制作钻石,并且我非常难过。下面是我想出迄今:如何使用嵌套循环制作钻石

public class Lab1 
{ 
    public static void main(String[] args) 
    { 
     for(int i = 5; i > -5; i--) 
     { 
     for(int j = 0; j < i; j++) 
     { 
      System.out.print(" "); 
     } 
     for(int j = 0; j >= i; j--) 
     { 
      System.out.print(" "); 
     } 
     System.out.println("*"); 
     } 
    } 
} 

***output***

+10

这听起来像一个编程的锻炼,和一个好。这是你必须摔跤的东西,直到你做对了。这就是编程的全部内容。有人给你答案会把你带走。独自发现它会更好。 – Kon

+0

我同意Kon,但是,如果你在这里遇到死路一条,那么可以问一个具体的问题是可以接受的。一个具体的问题通常会这样说:这是我的问题,这是我认为应该发生的事情,以下是发生的情况,这里是相关的代码。但是Kon又是对的 - 解决问题是编程的重要组成部分,在寻求帮助之前,您可以通过挣扎一会儿来获得更多。 – jeff

回答

0

我可以看到你正在尝试做的,这是思考的钻石一个漂亮整洁的方式。

你将不得不与J柜台的一些问题时,我如何使用Math.abs去negative..look()

也可以尝试写在基本步骤一些伪代码注释,以获得花纹清晰:

//print 5 spaces + 1 star 
//print 4 spaces + 2 stars 
//print 3 spaces + 3 stars 
//print 2 spaces+ 4 stars 
. 
. 
. 
//print 5 spaces + 1 star 

然后,从字面上用数字替换变量(j和i)。

你现在有一个模型。这通常是编程中最难的部分..让模型正确。只有当你对模型的工作原理有一个好主意时,才能进入编码领域。

将变量替换后,您可以尝试将整个事件转换为自动循环。

1
public class Diamond { 

//Size of the diamond 
private int diagonal; 

public Diamond(int diagonal) { 
    this.diagonal = diagonal; 
} 

public void drawDiamond() { 
    int n = diagonal; 
    for (int i = n/2; i >= -n/2; i--) { 
     for (int k = 0; k < i; k++) { 
      System.out.print(" "); 
     } 
     for (int j = 1; j <= (n - i * 2) && i >= 0; j++) { 
      System.out.print("*"); 
     } 
     for (int k = 1; k <= -i && i < 0; k++) { 
      System.out.print(" "); 
     } 
     for (int j = (n/2) * 2 + 2 * i; j >= -(n % 2 - 1) && i < 0; j--) { 
      System.out.print("*"); 
     } 
     System.out.println(); 
    } 
} 

public static void main(String[] args) { 
    Diamond a = new Diamond(21); //You pass diamond size here in the constructor 
    a.drawDiamond(); 
} 
} 

主要问题是对角线的奇偶性。 如果它甚至不能正确绘制顶部星号。所以有两种类型的钻石 - 偶数和奇数对角线(顶部有2个和1个星号)。

0

这应该工作。你可能只需要大部分的方法和printDiamond(_);

import java.util.Scanner; 
public class StarsTry 
{ 

    public static void main(String[] args) 
    { 
     int reader; 
     Scanner kBoard = new Scanner(System.in); 
     do 
     { 
      System.out.println("Insert a number of rows: "); 

       reader = kBoard.nextInt(); 
       printDiamond(reader); 


     }while(reader != 0); 

    } 

    public static void printStars(int n) 
    { 
     if(n >= 1) 
     { 
      System.out.print("*"); 
      printStars(n - 1); 
     } 
    } 

    public static void printTopTriangle(int rows) 
    { 
     int x = 1; 
     for(int j = (rows - 1); j >= 0; j--,x +=2) 
     { 
      printSpaces(j); 
      printStars(x); 
      System.out.print("\n"); 
     } 
    } 

    public static void printSpaces(int n) 
    { 
     if(n >= 1) 
     { 
      System.out.print(" "); 
      printSpaces(n - 1); 
     } 
    } 

    public static void printBottomTriangle(int rows, int startSpaces) 
    { 
     int x = 1 + (2*(rows - 1)); 
     for(int j = startSpaces; j <= (rows) && x > 0; j++,x -=2) 
     { 
      printSpaces(j); 
      printStars(x); 
      System.out.print("\n"); 
     } 
    } 
    public static void printBottomTriangle(int rows) 
    { 
     int x = 1 + (2*(rows - 1)); 
     for(int j = 0; j <= (rows - 1) && x > 0; j++,x -=2) 
     { 
      printSpaces(j); 
      printStars(x); 
      System.out.print("\n"); 
     } 
    } 

    public static void printDiamond(int rows) 
    { 
     printTopTriangle((int)rows/2 + 1); 
     printBottomTriangle((int)rows/2, 1); 
    } 
} 
+0

我的不好。我没有彻底地读过这个问题,所以这不是在嵌套循环中。抱歉!无论如何希望它有帮助! –

3
for (int i = 0; i < 5; i++) 
      System.out.println(" *********".substring(i, 5 + 2*i)); 

    for (int i =5; i>0;i--) 
     System.out.println("  **********".substring(i-1,5+(2*i)-3)); 
+0

这是最好的。我想知道,为什么它不是公认的答案 –

0
import static java.lang.System.out; 
import java.util.Scanner; 
public class Diamond { 

    public static void main(String[] args) { 

    Scanner sc=new Scanner(System.in); 
    int row=sc.nextInt(); 
    sc.close(); 
    Diamond d=new Diamond(); 
    d.upperDiamond(row); 
    d.lowerDiamond(row-2); 

    } 
    public void upperDiamond(int a){ 

    for(int i=0;i<a;i++){ 
     for(int j=a-1;j>i;j--) 
      out.print(" "); 
     for(int k=0;k<2*i-1;k++) 
     out.print("*"); 
     out.print("\n"); 
     } 
    } 
    public void lowerDiamond(int b){ 

     for(int i=0;i<b;i++){ 
     for(int j=0;j<=i;j++) 
     out.print(" "); 
     for(int k=0;k<2*(b-i)-1;k++) 
      out.print("*"); 
     out.print("\n"); 
     } 
    } 

} 
0
import java.util.Scanner; 


public class Diamond { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner in=new Scanner(System.in); 
     int input=in.nextInt(); 
     int min=1; 
     for(int i=0;i<input;i++){ 
      for(int j=input-1;j>i;j--){ 
       System.out.print(" "); 
      } 
      for(int k=0;k<min;k++){ 
       if(k%2==0){ 
        System.out.print("*"); 
       }else{ 
        System.out.print("."); 
       } 

      } 
      min+=2; 
      System.out.println(); 
     } 
     int z=input+input-3; 
     for(int i=1;i<input;i++){ 
      for(int j=0;j<i;j++){ 
       System.out.print(" "); 
      } 
      for(int k=0;k<z;k++){ 
       if(k%2==0){ 
        System.out.print("*"); 
       }else{ 
        System.out.print("."); 
       } 
      } 
      z-=2; 
      System.out.println(); 

     } 

    } 

} 
1
public class MyDiamond 
{ 
    public static void main(String[] args) 
    { 
     int numRows=151;//Length of the pyramid that we want.151 is just an example 
     int midrow = (numRows+1)/2;//midrow is the middle row and has numRows number of * 

     int diff=0; 
     for(int i=1;i<numRows+1;i++) 
     { 
     for(int j=1;j<numRows+1;j++) 
     { 
      if(((midrow-diff)<=j && (j<=midrow+diff))) 
      { 
       System.out.print("*"); 
      }else 
      { 
       System.out.print(" "); 
      } 

     } 
     System.out.println(); 
     if(i<midrow) 
     { 
     diff++; 
     }else 
     { 
      diff--; 
     } 
     } 




    } 

} 
-1
package practice; 

import java.util.Scanner; 

public class Practice { 


    public static void main(String[] args) { 

    for(int i=0;i<=10;i++) 
    { 
     if(i<=5) 
     { 
     for(int k=1;k<=5-i;k++) 
     { 
      System.out.print(" "); 
     } 
     for(int j=0;j<=i;j++) 
     { 
      System.out.print(" *"); 
     } 
     } 
     if(i>5) 
     { 
     for(int k=0;k<=i-6;k++) 
     { 
      System.out.print(" "); 
     } 
     for(int j=0;j<=10-i;j++) 
     { 
      System.out.print(" *"); 
     } 
     } 
     System.out.println(); 
    } 
    } 

} 
3

为了让您需要设置形状空间和明星我都由于我只使用嵌套循环使这个简单的程序钻石初学者。

public class Diamond { 
    public static void main(String[] args) { 
     int size = 9,odd = 1, nos = size/2; // nos =number of spaces 
     for (int i = 1; i <= size; i++) { // for number of rows i.e n rows 
      for (int k = nos; k >= 1; k--) { // for number of spaces i.e 
               // 3,2,1,0,1,2,3 and so on 
       System.out.print(" "); 
      } 
      for (int j = 1; j <= odd; j++) { // for number of columns i.e 
               // 1,3,5,7,5,3,1 
       System.out.print("*"); 
      } 
      System.out.println(); 
      if (i < size/2+1) { 
       odd += 2; // columns increasing till center row 
       nos -= 1; // spaces decreasing till center row 
      } else { 
       odd -= 2; // columns decreasing 
       nos += 1; // spaces increasing 

      } 
     } 
    } 
} 

,你可以看到号是需要的空间数量将减少至中心行和星数需要增加,但中心后,排它的反面,即空间的增加和星星减少

大小 可以是任意数字我将它设置为9在这里,所以我将有一个大小9星是9行9列...最大的空间(NOS)数量将

9/2 = 4.5

但java会把它当作4因为INT不能存储的十进制数和中心行会

9/2 + 1 = 5.5

这将在INT被视为5。

所以首先你会做成行..9行因此

(INT I = 1;我< =大小;我++)//大小= 9

然后

打印空间,如我没有

(INT K =号; k > = 1; K--)//号为尺寸/ 2

然后最终星

(INT J = 1;Ĵ< =奇数; J ++)

一次行结束......你可以调整使用,如果条件

0
import java.util.Scanner; 

public class MakeDiamond { 
public static void main(String[] args) { 

    Scanner sc = new Scanner(System.in); 

    while (true) { 

     System.out.println("Let's Creat Diamonds"); 
     System.out.println("If number increases Diamonds gets bigger. Please input number lager than 1 : "); 

     int user_input = sc.nextInt(); //gets user's input 
     System.out.println(""); 

     int x = user_input; 
     int front_space = -5; 

     for (int i = 0; i < 2 * user_input + 1; i++) { 
      for (int a = front_space; a < Math.abs(i - user_input); a++) { 
       System.out.print(" "); //Change a bit if diamonds are not in good shape 
      } 

      if (i < user_input + 1) { 
       for (int b = 0; b < 2 * i + 1; b++) { 
        System.out.print("* "); //Change a bit if diamonds are not in good shape 
       } 

      } else if (i > user_input) { 
       for (int c = 0; c < 2 * x - 1; c++) { 
        System.out.print("* "); //Change a bit if diamonds are not in good shape 
       } 
       x--; 
      } 
      System.out.print('\n'); 
     } 

     System.out.println("\nRun Again? 1 = Run, 2 = Exit : "); 

     int restart = sc.nextInt(); 
     System.out.println(""); 

     if (restart == 2) { 
      System.out.println("Exit the Program."); 
      System.exit(0); 
      sc.close(); 
     } 
    } 
    } 
} 

如果是在同时或循环钻石星和空间。 我认为使用'Math.abs'将是最简单的方法。

您可以通过扫描仪输入数字,当输入数量增加时,钻石会变大。

我用Eclipse来制作这个程序。 因此,空间会因您的运行环境而有所不同。像另一个IDE,CMD或终端。如果钻石状态不佳。只是改变空间。

-1
class Inc_Dec 
{ 
    public static void main(String[] args) 
    { 
     int le=11;int c=0;int j1=(le/2)+1;int j2=le-j1; 
     for(int i=1;i<=le;i++) 
     { 
      if(c<j1) 
      { 
      for(int k=(j1-i);k>0;k--) 
      { 
       System.out.print(" "); 
      } 
      for(int j=1;j<=i;j++) 
      { 
        System.out.print("*"+" "); 
      } 
      c++; 
      System.out.println(); 
      } 
      else 
      { 
       for(int k=(i-j1);k>0;k--) 
       { 
        System.out.print(" "); 
       } 
       for(int j=(le-i+1);j>0;j--) 
       { 
        System.out.print("*"+" "); 
       } 
       System.out.println(); 
      } 
     } 
    } 
} 
+3

为什么OP要“试试这个”?一个**好的答案**将总是解释做了什么以及为什么这样做,不仅是为了OP,而且是为了将来访问SO的人可能会发现这个问题并且正在阅读你的答案。 –

0

enter image description here

**Note :- Using Count Global variable we can manage space as well as star increment and decrement** 


import java.util.*; 
public class ParamidExample 
{ 
    public static void main(String args[]) 
    { 
     System.out.println("Enter a number"); 
     Scanner sc=new Scanner(System.in); 
     int no=sc.nextInt(); 

     int count=1; 
     for(int i=1;i<=2*no-1;i++) 
     { 
      for(int j=count;j<=no;j++) 
      { 
       System.out.print(" "); 
      } 
      for(int k=1;k<=count*2-1;k++) 
      { 
       System.out.print("* "); 
      } 
     if(i<no) 
      count++; 
     else 
      count--; 
       System.out.println(""); 
     } 
    } 
} 
+0

在答案中报告代码而不是在图像中会很有用。 – Dexter