如何翻转三角形? [英] How to Flip the triangle?

查看:99
本文介绍了如何翻转三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何翻转该三角形?

因此,我正在使杂音序列三角形.它是颠倒的. 如何将其旋转180度?

So i was making aritmethic sequance triangle. It was upside down. How do I turn it 180 degree?

例如:


1=1
1+2=3
1+2+3=6
etc...

我的代码:

package javaapplication4;

public class NewClass5 {
    public static void main(String[] args) {
        int i=5,a; 
        for(int j=i; j>=1; j--) { 
            for(a=1; a<=i; a++)
                System.out.print(a +" + ");
            int n = 0;
           for(a = 1; a<=i; a++) { 
               n = n + a;
           }
           System.out.print(" = "+ n);
           System.out.println();
           i--; 
       } 
    } 
} 

推荐答案

通过获取用户的输入,您可以对任何n个输入

You can do it for any n, by getting input from the user

Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for (int i = 1; i <= n; i++) {
        int add = 0;
        for (int j = 1; j <= i; j++) {
            System.out.print(j);
            if (j == i) 
                System.out.print("=");
            else 
                System.out.print("+");    
            add += j;    
        }
        System.out.println(add);
    }

这篇关于如何翻转三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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