Java Basics | Looping structures and arrays

Source: Internet
Author: User

Following the review of the branch structure of the previous days, let's review some of the knowledge of circular structures and arrays today.Exercise: Enter a number and then output the number to indicate the day of the weekSwitch (SELECT) {case option 1:code1;break;    Case option 2:code2;break; ......} An option is required in 1.switch (), which can only be byte/short/char/int, starting with JDK 1.7, allowing the stringcase order to be used without affecting the result 2. If there is no break after one or more cases, then case The order between them affects the resultExercise: Enter two numbers and a symbol to output the results of these two numbers under this symbol5 9 +->14 Exercise: Enter three digits to indicate the day of the year, the output of this day in the first few days of 3 5------>652014 3 5------>64 need to judge Common year leap year: hundred evenly divisible 400; not every hundred divisible 42012 4 = 02100% = 0---False Loop structure:Used to perform some of the same or similar repetitive operations while (logical value) {Code;}Practice:1. Ask for all odd and---i=1 within 1-100; I+=2;2. The number int i = 0 that can be divisible by 3 and not divisible by 7 within 100 of the print; I +=3;3. Enter a number, and the output number is a few digits. Enter a number to output all the factors of this number.                ---starting from 1, one by one number to take the number of the input, to see if it can be divided into 20:1 2 4 5 10 20 Define a loop, need three elements: define a variable to control the number of times, use the judging condition to determine the end of the loop, and change the variable do{Code during the cycle; Executes the code block first, then executes the logical value, and if the logical value is true, turns around to continue executing the code block, and if False, executes the next program. }whie (logical value);Note: The Do-while loop body must be executed at least once, regardless of whether the condition is met. 2 7 for(){defining a Loop variable;Control Conditions;change the loop variable){    //1456Code;}Note: For a For loop, if the control condition of the second part is not written, then the default is true, and this time it becomes a dead loop. Customary if the change is irregular or the number of times is not sure to use while (for example, from the console to get a more than 10 odd, it is possible to enter many times, that is, with this) while (N < | | n 2 ==0) {n =s.nextint ();} For (N < ten | | n 2 = = 0; n = s.nextint ())
Ask 1-50 and int sum = 0;//for the For loop, if the control condition of the second part is not written, then the default is true, and this time it becomes a dead loop. for (int i = 1; i <=; i++) {sum + = i;i++;} SYSTEM.OUT.PRINTLN (sum);

  

Practice: Hundred Money Hundred Chicken---3 text money 1 Rooster, 2 text Money 1 hen, 1 text money 3 chickens, 100 money how to buy exactly 100 chickens? ---First define a loop to represent the rooster, nest a loop to represent the hen, count the number of chickens, and calculate whether the money is 100 according to the number of chicken.
/** Practice: Hundred money Hundred chicken  ---3 text money 1 Rooster, 2 text Money 1 hens, 1 money 3 chickens, 100 money how to buy exactly 100 chickens? ---First define a loop to represent the rooster, nest a loop to represent the hen, calculate the number of chickens, and calculate whether the money is 100*/import java.util.scanner;public class Demo2d{public static depending on the number of chickens. void Main (string[] args) {Scanner s = new Scanner (system.in); for (int i = 1; i <=; i++) for    (int j = 1; J <= 5 0; J + +) {int k = 100-i-Jif (k% 3 = = 0 && 3*I+2*J+K/3 = =) {System.out.print (i+ "," +j+ "," +k+ "\ T");}}}    

  

Break and ContinueBreak: Used to select a structure and a looping structure, representing the end of the current layer of structure continue: can only be used for looping structures, which means skipping the loop to continue the next loop,Extended job: one loop to print 99 multiplication table
for (int i = 1, i <= 9; i++) {for (int j = 1; J <= I; j + +) {//if (i*j >=) {//system.out.print (i+ "*" +j+ "=" +i*j+ "   ");//}else{//system.out.print (i+" * "+j+" = "+i*j+"    ")//}system.out.print (i+" * "+j+" = "+i*j+" \ T ");//Use tab \ t, Can not be deliberately to control, automatic left alignment. }system.out.println ();}

  

ArrayA container that stores multiple data of the same type---the fixed-size array numbers the data that is put in, and the number is---subscript from 0 (one less than the length of the array) the capacity---length of the array Defining Formatsdata type [] Array name = new data type [length];int arr = new Int[5];    Represents a definition of an array that can store 5 integral elements arr[3] = 10; ARR[5] = 7; //arrayindexoutofboundsexception---array subscript out-of-bounds exception MemoryJava divides the memory used for 5 blocks: Stack memory (*), heap memory (*), method area, local method stack (involving block language issues), PC counters (registers, hardware-related) Stack MemoryFor storing variables, the variables are removed from the stack memory immediately after they are used in the stack memory, with a xiemoshalv look. Whenever new is encountered, a space is created in the heap (from 0-n) with an address. For the data in the first default value (0), when assigned to change the value. Heap MemoryUsed to store objects (arrays are a special kind of object). The element is automatically assigned the default value after it is stored in heap memory:byte/short/int-0   long-0L  float-0.0f  double-0.0  char-' \u0000 ' & nbsp Boolean-false, the default values for other types are null.    [(indicates that the data type---[represents an array]    i (if this is an array, this part represents the element type in the array---I represents Int)   @ (the marker symbol, which indicates that the address is behind) 15db9742 (You can think of an address, which is actually a 16 binary form of the object's hash code), which is recycled at some point when it's out of date.   Tomorrow content:         Arrays                 Definitions                 Memory                 Applications                          Traverse, sort, find, reverse, copy                Time/ Spatial Complexity---series (arithmetic progression summation), exponent, logarithm (logarithmic formula logab=logcb/logca,logabn=nlogab  Logab=1/logba)           two-dimensional arrays                    

Java Basics | Looping structures and Arrays

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.