什么是打印下面提到的号码模式的逻辑

问题描述 投票:0回答:5
class Num {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++) {
            for(int j=1;j<=i;j++) {
                if(j==1) {
                    System.out.print(i);
                }
                else if(j==2) {
                    System.out.print(" "+(i+j+2));
                }
                else {
                    System.out.print(" "+(i+j+4));
                }
            }
            System.out.println(" ");
        }
    }
}

输出:

1
2 6
3 7 10 
4 8 11 12
5 9 12 13 14

预期:

1
2 6
3 7 10 
4 8 11 13
5 9 12 14 15

我尝试了这么多,这个逻辑上来

when j=1 then i
when j=2 then i+j+2
when j=3 then i+j+4
when j>=4 then i+j+5

总共有4个条件在这里,我怎么得到循环做嵌套。任何其他逻辑也是明显的。

java numbers logic
5个回答
2
投票
int lineCount = 5;
for (int i = 1; i <= lineCount; i++) {
    int value = i;
    for (int j = 1; j <= i; j++) {
        System.out.print(value +  " ");
        value += lineCount -j;
    }
    System.out.println("");
}

0
投票

你忘了你的J = 3的情况下

for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                if (j == 1) {
                    System.out.print(" " + i);
                } else if (j == 2) {
                    System.out.print(" " + (i + j + 2));
                } 
                else if(j == 3){
                    System.out.print(" " + (i + j + 4));
                }
                else {
                    System.out.print(" " + (i + j + 5));
                }
            }
        }

0
投票

我不是一个Java开发人员,所以送你在C#中的解决方案。你将不得不更换“使用系统;”一些import语句,用适当的或者是System.out.print的println语句Console.WriteLine。

using System;

class Program
{
    static void Main()
    {
        PrintNumbers(1, 5); 
    }

    static void PrintNumbers(int level, int MaxLevel)
    {
        if( MaxLevel < level )
        {
            return;
        }

        int num = 0;
        for(int i = 0, diff=MaxLevel; i < level; i++)
        {
          num += (i == 0)?level:(diff-i);
          Console.Write("{0} ",num);
        }
        Console.WriteLine("");
        PrintNumbers(level+1, MaxLevel);
    }
}

0
投票

我建议,并相信它会更好,我们的代码是动态的,这意味着它会在user.Suppose的任何输入工作,如果这个问题是从5行变为6行,你必须改变的结构,以及添加另外,如果结构。其次,它不会是一个好主意,让尽可能多的if结构w.r.t您需要的列。

  Take the no. of rows the user wants to a variable say 'noofrows'. Then its just the same as your code with little changes:
        int noofrows;
    Scanner s=new Scanner(System.in);
    System.out.println("\nGive the no. of rows needed:\n");
    noofrows=(int)s.nextInt();
    System.out.println("Number give is:"+noofrows);
    int emptyspaces;
     for(int i = 1; i <= noofrows; i++) 
        {
        for (int j = 1; j <= i; j++) 
        {
            emptyspaces=(j)*(j-1)/2;   //counting the no. of emptspace which is an arithmetic progression 
        System.out.print(((j-1)*noofrows+i)-emptyspaces +" ");
        }
        System.out.print("\n");
       s.close();  //edited version

请让我知道柜面出了差错


0
投票
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n=5;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            if(j==1)
                cout<<i<<" ";
            else
                cout<<i+j+j<<" ";
        }
        cout<<endl;
    }
}

/*OUTPUT :-

1 
2 6 
3 7 9 
4 8 10 12 
5 9 11 13 15 

*/
© www.soinside.com 2019 - 2024. All rights reserved.