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