English 中文(简体)
Java Homework - 印刷三角模式?
原标题:Java Homework - Printing a triangle pattern?
  • 时间:2012-01-13 14:46:09
  •  标签:
  • java
  • loops

I m struggling with an assignment. I understand that it is entirely my fault, but I ve fallen behind in my classes and am struggling with this assignment.

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

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

使用(封套) lo。

没有人能够向我介绍我如何做到这一点? 我曾设法印刷了一平方米的星号,但我担心如何制造三角。

事先得到帮助。

问题回答

第一图是你需要印刷多少条线。 您的第一份<代码>for。 然后,就每一行而言,你需要印刷多少星号(假设在<条码>i上,有多少星号在<条码>上? 首先回答这些问题,方案应易于处理。

审查如下: 这并不完全是你需要做的事,而是帮助你开始。

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("
");
            }
        }
    }

奥凯,因此,你基本上必须把尽可能多的星号作为线号,正确吗?

我不允许给你密码,因为这是家务劳动,但我可以给你假装。

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

你们需要为这类问题提供两种机会。 第1次循环用于再生,第2次用于印刷星体。 在这里,第1版需要从用户那里获得投入,并储存在可变的ppose n中,而第1版则可以储存到N。

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

一、导 言

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

既然这是家庭工作,你就应当做其他工作。

解决办法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 + "*";
}

你的选择。

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();
    }
}

a. 使用 lo

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

hope that helps!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签