简体   繁体   English

java中的三角形数字

[英]triangle numbers in java

I am new to Java and now I want to learn better for loop. 我是Java新手,现在我想学习更好的循环。 I made some examples , but I don't know how to do a triangle that look like this: for n=6: 我做了一些例子,但我不知道如何做一个看起来像这样的三角形:对于n = 6:

111111 
22222
3333
444
55
6

My code until now: 我的代码到现在为止:

class Pyramid
{
public static void main (String[] args)
{
   int i,n=9,j; 
   for(i=1;i<=n;i++)
   {
       for(j=1;j<=i;j++)  {          
System.out.print(i); }      
System.out.print("\n");        
}}}

But what I managed to do it looks like this: 但我设法做到这一点看起来像这样:

1
22
333
4444
55555
666666

How to make it in reverse order ? 如何以相反的顺序进行?

Your issue is that your outer for loop was going from 6 to 1 , so you need to reverse that. 你的问题是你的外部for循环从6变为1 ,所以你需要反转它。

Change 更改

for(i=n;i>=1;i--) {

To

for(i = 1; i <= n; i++) { 

Further explanation, so you understand what is happening inside a for loop: 进一步说明,以便了解for循环中发生的事情:

A for loop operates on three clauses: where you start, the condition that the loop runs, and what to do after it runs. for循环对三个子句for操作:启动的地方,循环运行的条件以及运行后要执行的操作。

------v
for(i = 1; i <= n; i++) {

This is the assignment. 这是作业。 You set a variable to a number, which is where the loop starts. 您将变量设置为数字,这是循环开始的位置。 In this case, we start with i = 1 , since we want to print only one 1 on the first line. 在这种情况下,我们从i = 1开始,因为我们想在第一行上只打印一个1 In the third clause, we will increment it (read: add one to it), and run the loop again. 在第三个子句中,我们将增加它(读取:向其添加一个),然后再次运行循环。

--------------v
for(i = 1; i <= n; i++) {

This is the condition. 这是条件。 The loop will run whenever this condition evaluates to true . 只要此条件的计算结果为true ,循环就会运行。 In other words, if n = 6 , this loop will run when i <= 6 . 换句话说,如果n = 6 ,则当i <= 6时,该循环将运行。

--------------------v
for(i = 1; i <= n; i++) {

This is what will happen each time the loop is executed. 这是每次循环执行时会发生的情况。 After it runs through once when i = 1 , we will increment i , so now i = 2 . 当它在i = 1时运行一次后,我们将增加i ,所以现在i = 2 This will happen until the condition ( i <= n ) evaluates to false , ie when i = 7 . 这将发生直到条件( i <= n )评估为false ,即当i = 7 If the condition is false , the loop will terminate. 如果条件为false ,则循环将终止。

We can use is a function int numberForRow(int row) that will perform a suitable transformation. 我们可以使用一个函数int numberForRow(int row)来执行一个合适的转换。 Then the function can be used like r = numberForRow(i); print(r) 然后该函数可以像r = numberForRow(i); print(r) r = numberForRow(i); print(r) . r = numberForRow(i); print(r) It needs to map this: 它需要映射这个:

row (i) -> display number (r)
6          1
5          2
4          3
3          4
2          5
1          6

I think you can write it :) 我想你可以写:)

Look at the relationship between the input (i) and output (r) - it might be useful to note that they always add up to the same value so a little bit of math should do the trick. 看看输入(i)和输出(r)之间的关系 - 注意它们总是加起来相同的值可能是有用的,所以一点点的数学应该可以做到。

(While a function isn't strictly required I find that such functions can help break down a problem and, especially in this case, illustrate a transformation well - it also works in case of a "more advanced" transformation, such as was in the original question ;-) (虽然一个函数不是严格要求的,但我发现这些函数可以帮助解决问题,特别是在这种情况下,很好地说明了转换 - 它也适用于“更高级”转换的情况,例如原始问题;-)

public class Pyramid {

    public static void main (String[] args)
    {
        int i,n=9,j; 
        for(i=1;i<=n;i++)
        {
            //for(j=1;j<=i;j++)  { 
            for(j=n;j>=i;j--) {
                System.out.print(i);
            }      
            System.out.print("\n");        
        }
    }
}

This should help. 这应该有所帮助。

Can be done using below method: 可以使用以下方法完成:

public class Main {

    public static void main(String[] args) {

        int n = 6;
        int m =n;

        for (int i = 1; i <= n; i++,m--) {
            for (int j = 1; j <= m; j++) {
                System.out.print(i);
            }
            System.out.println();
        }

    }
}

If you want to print the triangular numbers then use the following code 如果要打印三角形数字,请使用以下代码

`public class Triangular{
 public static void main(String[] args) {
    int i = 0;
    int j =0;
    int count =0;
    for (i=1;i<=10;i++){
        count = 0;  // This is a program to print triangular numbers in JAVA
        for(j=1;j<=i;j++){
            count = count + j;
        }
            System.out.println(count);
        }
    }
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM