Java BASIC Programming Printing 1-100 digital right triangle

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:

1     //irregular array to print 1-100 triangles2      Public Static voidprint100nums () {3         int[] arr =New int[20][];4 5Out: for(inti = 0; i < arr.length; i++) {6Arr[i] =New int[i + 1];7                  for(intj = 0; J < Arr[i].length; J + +) {8                     if(i = = 0 & j = = 0) {9ARR[I][J] = (i + 1) * (j + 1);Ten}Else if(j = = 0) { One                         intLastIndex = arr[i-1].length-1; A                         intLastvalue = arr[i-1][lastindex]; -ARR[I][J] = lastvalue + 1; -}Else { theARR[I][J] = arr[i][j-1] + 1; -                     } -                     if(arr[i][j]==100){ -                          BreakOut ; +                     } -                 } +             } A         intFirindex = 0; at         intSenindex = 0; -          for(inti=0;i<arr.length;i++){ -              for(intj=0;j<arr[i].length;j++){ -                 intValue =Arr[i][j]; -                 intLen =string.valueof (value). Length (); -                 if(firindex!=0 && Senindex! = 0 && Firindex = i && Senindex <j) { inSystem.out.print ("-" + "")); -                     Continue; to                 } +                 if(len<2){ -System.out.print (value+ ""); the}Else { *System.out.print (value+ ""); $                 }Panax Notoginseng                 if(Value = = 100){ -Firindex =i; theSenindex =J; +                 } A             } the System.out.println (); +             if(firindex!=0 && Senindex! = 0 && Firindex = =i) { -                  Break; $             } $         } -          -}

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

Java BASIC Programming Printing 1-100 digital right triangle

Related Article

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.