2012-01-13 78 views
-3

我正在努力完成一项任务。我明白,这完全是我的错,但我在课堂上落后了,并且正在为这份任务而努力。Java家庭作业 - 打印三角形图案?

我的目标是打印以下模式:

* 
** 
*** 
**** 
***** 
****** 
******* 
******** 
********* 
********** 

使用(嵌套)for循环。

任何人都可以告诉我如何去做这件事吗?我设法打印了一个星号的方块,但我无法弄清楚如何制作一个三角形。

在此先感谢您的帮助。

+1

后,打印平方码 - 我们会告诉你如何从中做出三角形,它比你想象的更简单! – dasblinkenlight 2012-01-13 14:47:42

+2

我真的建议你开始编写自己的东西,然后如果你坚持,回来这里有一些代码,以便我们可以给你提示和建议。 – talnicolas 2012-01-13 14:48:06

+0

只要阅读http://en.wikipedia.org/wiki/For_loop,并尝试自己解决这个问题 – 4ndrew 2012-01-13 14:50:35

回答

7

首先找出需要打印多少行。这是你的第一个for循环。然后在每一行上,您需要打印多少个星号(假设您在线i,多少个星号在线i)?首先回答这些问题,程序应该很容易。

+1

完美的答案:究竟该问题的提问者需要学会问自己(和找到答案)。 – 2012-01-13 15:45:00

-1

这里是一个大纲。

for (i = 1; i < 11; i++) { 
    String toPrint = ""; 
    for (j = 1; j <= i; j++ { 
     // create string of asterisks here 
    } 
    // print a line here 
} 

,因为这是家庭作业,你应该自己做的休息环

for(i=0;i<10;i++){ 
    for(int j=0;j<i;j++) 
     System.out.print("*"); 
    System.out.println(); 
} 

希望帮助

+0

为什么downvote? – hvgotcodes 2012-01-13 15:05:53

+0

我没有downvote - 但我会猜它是通过角色作成不必要的字符串,而不是印刷字符:) – nevelis 2014-07-11 06:12:16

-2

使用!

+0

已经纠正它PLZ删除votedown :( – MozenRath 2012-01-19 13:25:43

-1

解决方案1:

for(int i=1; i< 10; ++i) { 
    for (int j = 0; j<i; ++j) { 
    System.out.print("*"); 
    } 
    System.out.println(""); 
} 

Soulution 2:

String s = "*"; 

for (int i = 1; i< 10; i++) { 
    System.out.println(s); 
    s = s + "*"; 
} 

您的选择。

+1

请检查你的代码再有错误 – MozenRath 2012-01-13 14:56:17

+0

对不起,语法错误 - 把咖啡洒在键盘shift键是粘 – 2012-01-13 15:03:11

+0

初始化'i'为1,并使用'I <10'将使其循环9次 – eboix 2012-01-13 15:06:57

0

好的,所以你基本上必须打印出与行号一样多的星号,对吧?

我不能给你代码,因为这是一项家庭作业,但我可以给你伪代码。

start with variable i at 1, loop while i is less than or equal to 10, increment i 
    // The line of code that you just wrote will execute once per line. 
    // Now you can print out your asterisks. 
    // Make another loop and execute it once per asterisk. That's i times, right? 
    start with variable j at 1, loop while j is less than or equal to i, increment j 
     print out an asterisk 
    end loop 
end loop 
+0

+1没有给予答案,而是给出一个有用的答案。 – 2012-01-13 15:00:17

+0

这个答案真的帮了我很多。非常感谢!当然也有其他人,但这是最能帮助我的答案,我得到了工作代码! – bckwth 2012-01-13 15:19:10

1

查看以下内容。它不完全符合你需要做的事情,但它会帮助你开始。

for (int x = 1; x <= 7; x++) { 
     for (int y = x; y <= 7; y++) { 
      System.out.print("(" + x + ", " + y + ")"); 
      if (y == 7) { 
       System.out.print("\n"); 
      } 
     } 
    } 
0

对于这些类型的问题,您需要2个循环。第一个循环用于迭代,第二个用于打印恒星。这里第一个u需要从用户那里获得输入,并将其存储在一个变量中,假设'n',并且第一个循环应该迭代到n。

for(i=1;i<=n;i++) 
{for(j=1;j<=i;j++) 
{ System.out.print("*"); 
} 
System.out.println(""); 
} 
-1
class Program 
{ 
    static void Main(string[] args) 
    { 
     String var = ""; 
     String exp_Str = ""; 
     for (int i = 1; i < 8; i++) 
     { 
      for (int j = 1; j < i; j++) 
      { 
       if (i > j) 
       { 


        var = var + j; 
        //Console.WriteLine(j+""); 
       } 

      } 
      Console.WriteLine(var); 
      var = ""; 

     } 

     Console.ReadLine(); 
    } 
} 
+1

我已经修复了你的格式。你会添加一些解释给你的答案吗? – Maerlyn 2014-07-01 15:50:01

0

解决方案:

IntStream.rangeClosed(0, MAX) 
      .forEach(i -> IntStream.rangeClosed(0, i) 
        .mapToObj(j -> j == i ? "*\n" : "*") 
        .forEach(System.out::print) 
      );