Java 如何使用嵌套 For 循环制作钻石
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19309203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a diamond using Nested For Loops
提问by Anshul Desai
So I was assigned to make a diamond with asterisks in Java and I'm really stumped. Here's what I've come up with so far:
所以我被指派用 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("*");
}
}
}
回答by henderso
I can see what you are trying to do and this is a pretty neat way to think about the diamond.
我可以看到您正在尝试做什么,这是考虑钻石的一种非常巧妙的方式。
You will have some issues with the j counter when i goes negative..look at how to use Math.abs()
当我变为负数时,j 计数器会出现一些问题。看看如何使用 Math.abs()
Also try writing some pseudo code in basic steps with comments to get the pattern clear:
还可以尝试在基本步骤中编写一些带有注释的伪代码,以使模式清晰:
//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
Then, literally substitute variables (j and i) for the numbers.
然后,从字面上用变量(j 和 i)代替数字。
You now have a model. This is often the hardest part in programming..getting the model right. Only jump into coding when you have a good idea for how the model works.
你现在有一个模型。这通常是编程中最难的部分……使模型正确。只有当您对模型的工作原理有一个好主意时才开始编码。
Once you have the variables substituted, you can try to convert the whole thing into an automated loop.
替换变量后,您可以尝试将整个过程转换为自动循环。
回答by ka4eli
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();
}
}
The main problem is parity of diagonal. If it's even you can't properly draw top asterisk. So there is 2 types of diamonds - with even and odd diagonal (with 2 and 1 asterisk at the top).
主要问题是对角线的奇偶校验。如果它甚至你不能正确绘制顶部星号。所以有 2 种类型的钻石 - 偶数和奇数对角线(顶部有 2 和 1 星号)。
回答by CoderAdroidMan4.5
This should work. You probably only need most of the methods and printDiamond(_);
这应该有效。您可能只需要大部分方法和 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);
}
}
回答by Brian
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));
回答by Shiv Kumar Yadav
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");
}
}
}
回答by VirusCD
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();
}
}
}
回答by akhilesh
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--;
}
}
}
}
回答by Amit Nanda
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();
}
}
}
回答by Alok
in order to make a diamond you need to set spaces and stars in shape i have made this simple program using only nested loops since i am a beginner.
为了制作钻石,您需要设置空间和星星的形状,因为我是初学者,所以我只使用嵌套循环制作了这个简单的程序。
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
}
}
}
}
as you can see nos that is number of spaces needs to be decreased till center row and number of stars needs to be increased but after center row its the opposite , i.e spaces increase and stars decrease
如您所见,nos 需要减少空格数,直到中心行和星星数需要增加,但在中心行之后则相反,即空格增加而星星减少
size can be any number i set it to 9 over here so i will have a size 9 star that is 9 rows and 9 columns max... number of space (nos) will be
大小可以是任何数字,我在这里将其设置为 9,因此我将有一个大小为 9 的星,最大为 9 行和 9 列......空间数(nos)将是
9/2 = 4.5
9/2 = 4.5
but java will take it as 4 because int can not store decimal numbers and center row will be
但是java将其视为4,因为int不能存储十进制数并且中心行将是
9/2 + 1 = 5.5
9/2 + 1 = 5.5
which will be taken as 5 in int.
在 int 中将被视为 5。
so first you will make rows.. 9 rows hence
所以首先你会做行..因此有 9 行
(int i=1;i<=size;i++)//size=9
(int i=1;i<=size;i++)//size=9
then
然后
print spaces like i did
像我一样打印空间
(int k =nos; k>=1; k--) //nos being size/2
(int k =nos; k>=1; k--) //nos 为 size/2
then finally stars
然后终于星星
(int j=1; j<= odd;j++)
(int j=1; j<= 奇数;j++)
once line ends... you can adjust stars and spaces using if condition
一旦行结束...您可以使用 if 条件调整星号和空格
回答by KHW1031
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();
}
}
}
}
When making diamonds with while or for loops. I think using 'Math.abs' will be the simplest way to make it.
使用 while 或 for 循环制作钻石时。我认为使用“Math.abs”将是最简单的方法。
You can put number by Scanner, and when input number increases diamonds will get bigger.
您可以通过扫描仪输入数字,当输入数字增加时,钻石会变大。
I used Eclipse to make this program. so, the Space will be differ by your running environment. like another IDE, CMD or Terminal. if diamonds are not in good shape. Just change spaces.
我使用 Eclipse 来制作这个程序。因此,空间将因您的运行环境而异。像另一个 IDE、CMD 或终端。如果钻石形状不好。换个空格就行了。