如何以两个金字塔的形式打印一系列自然编号,一个正常,一个反转使用单个循环? [英] How to print a series of natural no.s in the form of two pyramids , one normal and one inverted using single for loop?

查看:85
本文介绍了如何以两个金字塔的形式打印一系列自然编号,一个正常,一个反转使用单个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成基于用户输入的这种模式。这里输入是num = 7



输出



 1 

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1





这是我的代码:





 import java.util.Scanner; 
class 测试
{
public static void main( String arr [])
{
扫描仪输入= 扫描仪(系统。 in );
系统。 out .println( 输入否打印对称的金字塔:);
int num = input.nextInt();

// 打印普通金字塔

< span class =code-keyword> for ( int i = 0 ; i< = num; i ++)
{
for int j = 1 ; j< i; j ++)
{
系统。 out .print(j) ;
}

系统。 out .println( );
}
// 中间
for int i = 1 ; i< = num; i ++)
{
System。 out .print(i);
}
系统。 out .println( );

// 打印倒金字塔

< span class =code-keyword> for
int i = num; i> = 0 ; i--)
{
for int j = < span class =code-digit> 1 ; j< i; j ++)
{
System。 out .print( j)条;
}

系统。 out .println( );

}

}
}





什么我试过了:



如何只使用1 for循环来实现相同的输出?



先谢谢!! :)

解决方案

我们不做你的家庭作业。

HomeWork不会测试你的技能,乞求别人去做你的工作,它旨在帮助您的老师检查您对所学课程的理解以及您应用它们时遇到的问题。

任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



您的代码中有5个循环。

- 您可以简单地通过说上三角形中还有一条线来移除中间线的循环。

- 您可以通过巧妙地使用绝对函数来合并上下三角形(abs())。

- 通过使用递归函数,你可以模拟一个没有循环的循环。

真正的工作留给你,因为它是HomeWork。 br $> b $ b

-----------------------

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



建议:给一张纸做故事并尝试手工完成,你的程序应该使用相同的程序。

I am trying to generate this kind of pattern which is based on user input .Here input is num=7

Output

1

1 2 

1 2 3 

1 2 3 4

1 2 3 4 5 

1 2 3 4 5 6 

1 2 3 4 5 6 7

1 2 3 4 5 6 

1 2 3 4 5 

1 2 3 4

1 2 3 

1 2 

1



Here's my code:


import java.util.Scanner;
    class Test
    {
    public static void main(String arr[])
    {
        Scanner input=new Scanner(System.in);
        System.out.println("Enter a no to print a symmetrical pyramid :");
        int num=input.nextInt();
        
        //Printing normal Pyramid
        
       for(int i=0;i<=num;i++)
       {
           for(int j=1;j<i;j++)
           {
               System.out.print(j); 
           }
           
           System.out.println("");   
       }
        //Middle 
        for(int i=1;i<=num;i++)
        {
            System.out.print(i);
        }
        System.out.println("");
       
        //Printing inverted Pyramid
        
        for(int i=num;i>=0;i--)
        {
           for(int j=1;j<i;j++)
           {
               System.out.print(j); 
           }
           
           System.out.println("");
            
        }
        
    }
    }



What I have tried:

How can I achieve the same output using only 1 for loop ?

Thanks in Advance !! :)

解决方案

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

You have 5 loops in your code.
- You can remove the loop of middle line simply by saying that there is 1 more line in upper triangle.
- You can merge upper and lower triangles with a clever usage of the absolute function (abs()).
- And by using a recursive function, you can simulate a loop without loop.
Real work is left to you since it is HomeWork.

-----------------------
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: tale a sheet of paper and try to do it by hand, your program should use the same procedure.


这篇关于如何以两个金字塔的形式打印一系列自然编号,一个正常,一个反转使用单个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆