Print a 1-100 rule-arranged triangle with an irregular array

Source: Internet
Author: User

Demand:

1. Print 1-100-digit right triangle, less than '-'

2. Using an irregular array implementation

Technical assessment:

1. Arrays, multidimensional arrays, irregular arrays

2. Self-increment self-decrement operator

3.for multi-layered loops and jumps

    • Break Jump Statement
    • Continue continuation statement

4. String

Ideas:

1. Use an irregular array implementation to arbitrarily define the maximum value of a number

2. Using multiple Nested Loops

3.break Jump Statement

4.continue Continuation statement

Code:

The irregular array prints 1-100 of the trapezoidal public static void Print100nums () {int[][] arr = new Int[20][];out:for (int i = 0; i < arr.length;  i++) {Arr[i] = new Int[i + 1];for (int j = 0; J < Arr[i].length; J + +) {if (i = = 0 & j = 0) {Arr[i][j] = (i + 1) * (j + 1);}  else if (j = = 0) {int lastIndex = Arr[i-1].length-1;int Lastvalue = arr[i-1][lastindex];arr[i][j] = lastvalue + 1;} else {Arr[i][j] = arr[i][j-1] + 1;} if (arr[i][j]==100) {Break Out;}}} int Firindex = 0;int Senindex = 0;for (int i=0;i<arr.length;i++) {for (int j=0;j<arr[i].length;j++) {int value = Arr[i] [J];int len = string.valueof (value). Length (); if (firindex!=0 && senindex! = 0 && Firindex = i && s Enindex < J) {System.out.print ("-" + "); continue;} if (len<2) {System.out.print (value+ "");} else {System.out.print (value+ "");} if (value = =) {Firindex = I;senindex = j;}} System.out.println (); if (firindex!=0 && senindex! = 0 && Firindex = = i) {break;}}}

Another way to print:

         for (int  [] x:arr) {            for (int  y:x) {                + "   ");                 if (y==100) {                    return;                }            }            System.out.println ();        }

Summarize:

Jump statement
Break jump
Usage 1: Jump out of the loop, and when the loop is nested, jump out of the current loop;
Usage 2: Jump out of the statement block. Used to jump out of outer loops when looping nesting.
Continue continue
Usage: Although break can jump out of the loop, it is sometimes necessary to stop the remainder of the loop at the same time and continue with the next loop, which requires continue
return returns

Usage: Terminates execution of all programs below

Print a 1-100 rule-arranged triangle with an irregular array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.