java 简单的java代码,打印星星
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10549688/
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
easy java code,printing stars
提问by ERJAN
here is java code that prints triangles one below each other, naming them A, B, C, D; my question is how to print them on*the same level*
这是 Java 代码,它打印一个彼此下方的三角形,将它们命名为 A、B、C、D;我的问题是如何将它们打印在 *同一级别*
public class ex_5_10 {
public static void main(String args[]){
// (A)
System.out.println("(A)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if(column > row) continue ;
System.out.print("*");
}
System.out.println() ;
}
//*********************************
// (B)
System.out.println("(B)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if(column < row) continue ;
System.out.print("*");
}
System.out.println() ;
}
//********************************
//(C)
System.out.println("(C)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if( column < row ) System.out.print(" ") ;
else System.out.print("*");
}
System.out.println() ;
}
// (D)
System.out.println("(D)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 10 ; column >= 0 ; column--){
if( column > row ){ System.out.print(" ") ; }
else {System.out.print("*"); }
}
System.out.println() ;
}
}
}
}
so the java code above will print this:
所以上面的java代码将打印:
*
**
***
***
**
*
***
**
*
*
**
***
now i need to print same figures on the same level!
现在我需要在同一级别打印相同的数字!
* *** *** *
** ** ** **
*** * * ***
please help me! i m looking for your answer! thanks in advance
请帮我!我在寻找你的答案!提前致谢
回答by Aligus
Why don't you want to print like this?
你为什么不想这样打印?
System.out.println("* *** *** *") ;
System.out.println("** ** ** **") ;
System.out.println("*** * * ***") ;
UPDATE: OK.
更新:好的。
If this is homework for loops. Just create matrix (array of arrays) and print by assigned elements of the matrix.
如果这是循环的作业。只需创建矩阵(数组数组)并按矩阵的指定元素打印。
Then print out the matrix line by line.
然后逐行打印出矩阵。
You should use offsets for each printing cycles. Fox example, for second in pseudo code (not java code!):
您应该为每个打印周期使用偏移量。Fox 示例,第二个伪代码(不是 java 代码!):
//*********************************
// (B)
matrix[0][offset] = "B";
for(int row = 0 ; row < height ; row++){
for(int column = 0 ; column < width ; column++){
if(column < row) continue ;
matrix[row][column+offset] = "*";
}
}
offset += width + space_length;
回答by SKi
You could also try somethibng like this:
你也可以尝试这样的事情:
for(int row = 0 ; row < 10 ; row++){
// A
for(int column = 0 ; column < 10 ; column++){
if(column > row) continue ;
System.out.print("*");
}
System.out.print(" ");
// B
for(int column = 0 ; column < 10 ; column++){
if(column < row) continue ;
System.out.print("*");
}
System.out.print(" ");
// C
for(int column = 0 ; column < 10 ; column++){
if( column < row ) System.out.print(" ") ;
else System.out.print("*");
}
System.out.print(" ");
//D
for(int column = 0 ; column < 10 ; column++){
if(column > row) continue ;
System.out.print("*");
}
System.out.println() ;
}
Maybe you can simplify it more by changing 4 inner loops to one loop. But it is your homework...
也许您可以通过将 4 个内循环更改为一个循环来进一步简化它。但这是你的功课...
回答by yehenTennakoon
enter code hereclass y{
public static void main(String args[])
{
for(int x=1;x<=3;x++)
{
for(int y=1;y<=x;y++)
{
System.out.print("*");
}
for(int sp1=1;sp1<=(4-x);sp1++)
{
System.out.print(" ");
}
for(int se=1;se<=(4-x);se++)
{
System.out.print("*");
}
for(int sp2=1;sp2<(2*x);sp2++)
{
System.out.print(" ");
}
for(int p=1;p<x;p++)
{
System.out.print(" ");
}
for(int stt=1;stt<=(4-x);stt++)
{
System.out.print("*");
}
for(int spth=1;spth<=(4-x);spth++)
{
System.out.print(" ");
}
for(int stfr=1;stfr<=x;stfr++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}
回答by Andrzej Doyle
There's no single-line answer to this.
对此没有单行答案。
You need to bear in mind that when you call println()
, there will be a newline output at the end of your string, so nothing further will appear on that line.
您需要记住,当您调用 时println()
,字符串末尾会有一个换行符输出,因此该行上不会再出现任何内容。
With that in mind, you'll need to split the logic, so that you assemblethe outputs for the various "figures" first, and then once you've processed them, you outputthe result in a way that makes sense (so the first line of every character, then the second etc.). Instead of calling print while handling each figure, put the results into an appropriate data structure.
考虑到这一点,您需要拆分逻辑,以便首先组合各种“数字”的输出,然后在处理完它们后,以有意义的方式输出结果(因此每个字符的第一行,然后是第二行等等)。不要在处理每个图形时调用 print,而是将结果放入适当的数据结构中。
For bonus marks, you'll need to word-wrap when enough data is supplied to span the width of the display...
对于奖励标记,当提供足够的数据以跨越显示的宽度时,您需要自动换行...
回答by GingerHead
If you want to play with the results you can do the following: (It's a homework)
如果你想玩结果你可以做以下事情:(这是一个家庭作业)
you can notice that in t he loops there are the following conditions:
您可以注意到在循环中存在以下条件:
if(column < row) continue ; System.out.print("*");
if(column < row) continue ; System.out.print("*");
if( column < row ) System.out.print(" ") ; else
System.out.print("*");if( column > row ){ System.out.print(" ") ; } else
{System.out.print("*"); }
if(column < row) continue ; System.out.print("*");
if(column < row) continue ; System.out.print("*");
if( column < row ) System.out.print(" ") ; else
System.out.print("*");if( column > row ){ System.out.print(" ") ; } else
{System.out.print("*"); }
Just mix and re-arrange them, and see the out-coming results.
And also move the insides of loops for the columns that are internal of one loop for the rows, and add offsets to the columns.
This time you need to use one block of loops and not 4.
只需混合并重新排列它们,然后查看即将产生的结果。并且还为行的一个循环内部的列移动循环的内部,并为列添加偏移量。
这次您需要使用一个循环块而不是 4 个。
Have fun.
玩得开心。
回答by Subir Kumar Sao
You can put the code of all different figures in the same loop and then put calculated spaces between each figures. Find the patterns of the spaces between the figures.
您可以将所有不同图形的代码放在同一个循环中,然后在每个图形之间放置计算出的空格。找出图形之间空格的模式。
回答by Jimi Kimble
You would move the internal for loops for the columns inside of one loop for the rows, and add offsets to the columns. The logic for the internal if would then all be column - offset.
您可以将列的内部 for 循环移动到行的一个循环内,并为列添加偏移量。内部 if 的逻辑将全部是列 - 偏移量。
回答by sabbibJAVA
public class ex_5_10 {
public static void main(String args[]){
// (A)
System.out.println("(A)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if(column > row) continue ;
System.out.print("*");
}
System.out.print() ;
}
//*********************************
// (B)
System.out.println("(B)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if(column < row) continue ;
System.out.print("*");
}
System.out.print() ;
}
//********************************
//(C)
System.out.println("(C)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 0 ; column < 10 ; column++){
if( column < row ) System.out.print(" ") ;
else System.out.print("*");
}
System.out.print() ;
}
// (D)
System.out.println("(D)") ;
for(int row = 0 ; row < 10 ; row++){
for(int column = 10 ; column >= 0 ; column--){
if( column > row ){ System.out.print(" ") ; }
else {System.out.print("*"); }
}
System.out.print() ;
}
}
回答by hitesh141
int rows = 10;
整数行 = 10;
for(int i=0; i<rows ; i++)
{
for (int j = 0; j <i; j++) {
System.out.print("*");
}
System.out.println();
}