1, branch statements (IF)
scenario: When the determination of the scope of
the grammatical structure:
A, first form
if(表达式){
语句块;
}
b、第二种形式
if(表达式){
语句1;
}else{
语句2;
}
c、第三种形式
if(表达式1){
语句1;
}else if(表达式2){
语句2;
}
...
else{
语句n;
}
2, branch statements (switch)
Scene: Analyzing equivalent
Syntax:
switch(变量|表达式){
case 常量1:
语句1;
break;
case 常量2:
语句2;
break;
...
default:
语句n;
break;
}
注意:其中常量值可以是整数、字符、枚举,JDK1.7后支持String类型。
现象:case下滑(下坠)。
3, loop (while)
the scene: in the end is not clear how many cycles
grammatical structure:
while(表达式){
循环体;
迭代变量自增;
}
注意:一定要添加变量自增,否则进入死循环。
死循环:
while(true){
}
4, loop statements (do ... while)
the grammatical structure:
do{
循环体;
}while(表达式);
注意:while后有';',do-while循环至少执行一次循环体。
举例:
Scanner sc = new Scanner(System.in);
String answer = "no";
// 向女朋友求婚
do {
System.out.println("嫁给我吧!");
answer = sc.nextLine();
} while (!answer.equals("yes"));
//建议:String判断相等,使用equals方法,不建议用==
//结果输出
System.out.println("结婚!");
总结:while与do-while的区别。
5, loop (for)
Scene: general know how many cycles
grammatical structure:
for(表达式1;表达式2;表达式3){
循环体;
}
执行顺序:表达式1-->表达式2-->(满足)循环体-->表达式3-->表达式2-->(满足)循环体-->...
注意事项:for()中有且只有两个';'
死循环:
for(;;){
}
举例:
for(int i=100;i<999;i++){
//拆分
//整数相除为整数,浮点数运算为浮点数
int bai = i/100; //百位
int shi = i%100/10; //十位
int ge = i%10; //个位
//判断是否满足水仙花特点
if(i == (bai*bai*bai + shi*shi*shi + ge*ge*ge)){
System.out.println(i);
}
}
嵌套循环:九九乘法表
6, control statements
a, break
scenarios: Switch used, the loop out of the loop, the end of all cycles later.
For example:
for(int i=0;i<10;i++){
System.out.println(i + "跑一圈");
if(i==6) break;
}
b、continue
场景:循环语句中,结束本次循环,进入下一次循环。
举例:
for(int i=0;i<10;i++){
System.out.println(i + "下一题");
if(i==6) continue;
System.out.println("next");
}
c、return
场景:方法中,需要返回给调用者一定的结果。
7, the array
a, defined: storing a set of a series of the same data type.
b, declaration syntax structure:
Data Type [] array name; -> recommended
data type array name [];
Note: not specified array length (i.e., number of elements) alone declare an array.
C initialized, the array:
NOTE: Once initialized array length immutable; uninitialized array can not be used.
1) static initialization
syntax:
Data Type [] = new Array Name Data Type [] {} initialization list
data type [] array initialization list name = {}
Example:
int [] Scores = new int [] {90, 85 , 81,91,70}; // java results
int [] = {70,80,90,100,88 scores2};
2) dynamic initialization
syntax:
data type [] = new array name data type [length]
Example:
int[] scores3 = new int[5];
scores3[0] = 90;
scores3[1] = 98;
scores3[2] = 80;
scores3[3] = 78;
scores3[4] = 89;
Definition: The index (subscript), refers to the number of elements in the array, Java in the index from zero.
Note: The array bounds exception, null pointer.
d, array operation
1) traverse
Example 1:
for(int i=0;i<5;i++){
System.out.println(scores[i]);
}
注意:可以使用数据名.length来获得数组的长度。
例2:
//增强for循环,foreach
for(int score:scores){
System.out.println(score);
}
2)取最值
例:
int[] scores={90,89,81,78,22};
int max = 0; //最大值
for(int i=0;i<scores.length;i++){
if(max < scores[i]){
max = scores[i];
}
}
System.out.println("最高成绩为:"+max);
3) Sort
Example:
Arrays.sort (Scores);
E, multidimensional array.
8, method
a, defined: is the number of relevant statements grouped together, for a block of statements to solve a particular problem.
B, Syntax:
[Modifier] return type method name (parameter data type 1 1, ..., n data type of the parameter n) {
method body;
return result;
}
Example:
public static int myAdd(int a, int b){
int c = a + b;
return c;
}
Note: At first, it is recommended to add both public static modifier before each method. If no return value, the return value of type void, may not be added at this time return statement.
Concept: parameter, argument.
c, passed by value
parameter passing method in Java is only one: the value passed.
A copy of the actual parameter values within the coming of (replica) passed into the method, and the parameter itself is not affected.
Example:
// does not change, no change in the basic data types
public static void myFunction (int A) {
A = 10;
}
// changed, the reference data memory address transfer type
public static void myFunction2 (int [] a) {
a [0] =. 8;
}
D, method overload (overload)
is defined: in the same class, there has been a method of the same name, but different parameter list (the number of parameters, parameter types).
Benefits: Programmers can easily call.
public static int myAdd(int a, int b){
return a + b;
}
public static int myAdd(int a, int b, int c){
return a + b + c;
}
public static short myAdd(short a, short b){
return (short)(a + b);
}