Javase Basics of the Java Learning Path 3

Source: Internet
Author: User
Tags modifier naming convention

All code is untested:

1:for the format of the loop?
For loop format:
For (initialize an expression; a conditional expression; an action expression after a loop) {
Circulation body;
}
Execution process:
* A: Execution of initialization statements
* B: Execute a JUDGMENT condition statement to see if its return value is TRUE or False
* If true, continue execution
* If False, end loop
* C: Execute the loop body statement;
* d: Operation expression after loop execution
* E: Go back to B.

Use the For loop to complete the following case

Sum of data between 1 and 10
int sum = 0;
for (int i=1;i<=10;i++) {
sum + = i;
}
SYSTEM.OUT.PRINTLN (sum);

Find the sum of even numbers between 1 and 100
int sum = 0;
for (int i=1;i<=100;i++) {//for (int i=0;i<=100;i+=2)
if (i% 2 = = 0) {
sum + = i;
}
}
SYSTEM.OUT.PRINTLN (sum);

Find the sum of odd numbers between 1 and 100
int sum = 0;
for (int i=1;i<=100;i++) {//for (int i=1;i<=100;i+=2)
if (i% 2! = 0) {
sum + = i;
}
}
SYSTEM.OUT.PRINTLN (sum);

Number of daffodils printed:
* The so-called Narcissus number refers to a three-digit number whose numbers are cubic and equal to the number itself.
* Example: 153 is a number of daffodils.
* 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
* The difficulty is how to get the operand to each three-digit bit
for (int i=100;i<=999;i++) {//sets the range of all daffodil numbers 100~999
int GE = i% 10;//Use the modulo operator to get the number of each bit
int shi = i/10% 10;
int bai = i/10/10% 10;
if (Ge*ge*ge+shi*shi*shi+bai*bai*bai = = i) {
System.out.println (i);
}
}

Statistics of daffodils
int sum = 0;
for (int i=100;i<=999;i++) {//sets the range of all daffodil numbers 100~999
int GE = i% 10;//Use the modulo operator to get the number of each bit
int shi = i/10% 10;
int bai = i/10/10% 10;
if (Ge*ge*ge+shi*shi*shi+bai*bai*bai = = i) {
System.out.println (i);
sum + = 1;
}
}
SYSTEM.OUT.PRINTLN (sum);

99 Multiplication Table
for (int i=1;i<=9;i++) {
for (int j=1;j<=i;j++) {
System.out.print (j+ "*" +i+ "=" +i*j+ "\ t");
}
System.out.println ();
}

2:while loop format? To be able to read the basic format of the execution process
while loop:
while (judge conditional statement) {
Loop body statement;
}

While loop full format:
initialization statement;
while (judging conditional statement) {
Loop body statement;
Control condition statement;
} The

uses the while loop to complete the following case
1 to 100 and
int sum = 0;
int num = 1;
while (num <=100) {
sum + = num;
num++;
}
System.out.println (sum);

the number of times the paper folds into the height of Mt. Everest, write freely.
int count = 0;
int num = 1;
while (num <= 10000) {
count++;
Num *= 2;
}
System.out.println (count);

format of the looping structure Do...while statement:
*
Do {
loop body statement;
} while (judging conditional statements);

full format;
initialization statement;
Do {
loop body statement;
Control condition statement;
}while (judging conditional statements);

Execution process:
* A: Execution of initialization statements
* B: Execute the loop body statement;
* C: Execute control condition statement
* D: Execute judgment condition statement to see if its return value is TRUE or False
* If true, continue execution
* If False, end loop
* E: Go back to B.

The difference between the three circular statements:
* The Do...while cycle performs at least one loop body.
* and the For,while cycle must first determine whether the condition is true, and then decide whether to execute the Loop body statement.
* The difference between A for loop and a while loop:
If you want to continue using the variable that controls the condition after the loop is over, use the while loop, otherwise use the For loop.
I don't know who to use for loop. Because variables disappear early from memory, you can improve the efficiency of memory usage.
Must pay attention to control the condition statement control of the problem of the variable, do not lose, otherwise it is easy to die loop.
Two of the simplest dead loop formats
* while (true) {...}
* for (;;) {...}


What is the use of 3:break,continue and return respectively?
* Return is the End method;
* Break is an out-of-loop, and can only be used in switch and loop.
* Continue is to terminate this cycle to continue with the next cycle; only in loops.

4: The concept of function, the format of the function, the explanation of the format
A block of code that completes a specific function.
Modifier returns a value type method name (parameter type argument name 1, argument type parameter Name 2 ...) {
Method body Statement;
return value;
}
Format description of the method:
* Modifier: Use public static now. We'll explain the other modifiers in more detail later.
* Return value type: is the data type of the functional result.
* Method Name: Conforms to the naming convention. Convenient for our call.
Parameters
* Actual parameters: is actually involved in the operation.
* Formal parameters; It is the method definition that is used to receive the actual parameters.
* Parameter type: is the data type of the parameter
* Parameter name: Name of variable
* Method Body Statement: Is the code to complete the function.
* Return: The End method.
* Return value: Is the result of the function, which is brought to the caller by return.

5: Call to function
A: A function call that explicitly returns a value type
* A: Call alone, generally no meaning, so not recommended.
* B: Output call, but not good enough. Because we may need to do further work on the results.
* C: Assignment call, recommended scheme.
function calls of type b:void
* Called separately
* Output Call (Error)
* Assignment Call (Error)

6: Exercises for functions:
A: Ask for the sum of two data
public static int Add (int a,int b) {
int sum = a + b;
return sum;
}

B: Determine if two data is equal
public static Boolean isequals (int a,int b) {
Boolean BL = a = = B;
return BL;
}

C: Get the larger value in two numbers
public static int Getmaxnum (int a,int b) {
int maxnum = (a>b)? A:B;
return maxnum;
}

D: Print star rectangle for m row n columns
Scanner sc = new Scanner ();
int m = Sc.nextint ();
int n = sc.nextint ();
PRINTMN (M,n);
public static void printmn (int m,int n) {
for (int i=1;i<=m;i++) {
for (int j=1;j<=i;j++) {
System.out.print ("*");
}
System.out.println ();
}
}

E: print nn multiplication table
Scanner sc = new Scanner ();
int m = Sc.nextint ();
int n = sc.nextint ();
public static void printnn (int m,int n) {
for (int i=1;i<=m;i++) {
for (int j=1;j<=i;j++) {
System.out.print (j+ "*" +i+ "=" +i*j+ "\ t");
}
System.out.println ();
}
}

7: What is a function overload?
In the same class, the method name is the same, and the parameter list is different. Is independent of the return value type.
The parameter list is different:
* Different number of parameters
* Different parameter types
* The order of the parameters is different (overloaded, but not in development)

Javase Basics of the Java Learning Path 3

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.