for循环语句,双从for嵌套:
/*
for(初始化表达式;循环条件表达式;循环后的操作表达式)
{ 执行语句; } */
/*1,变量有自己的作用域。对于for来讲:如果将用于控制循环的增量定义在for语句中。那么该变量只在for语句内有效。 for语句执行完毕。该变量在内存中被释放。
2,for和while可以进行互换。如果需要定义循环增量。用for更为合适。 总结: 什么时候使用循环结构? 当要对某些语句执行很多次时,就使用循环结构。 */
class ForDemo { public static void main(String[] args) { for(int x = 0; x<3 ; x++) { System.out.println("x="+x); } //System.out.println("x===="+x); int y=0; while(y<3) { System.out.println("y="+y); y++; } System.out.println("y===="+y); } }
//语句嵌套形式。其实就是语句中还有语句。 //循环嵌套。 class ForForDemo { public static void main(String[] args) { for(int x=0; x<3; x++)// { for(int y=0; y<4; y++) { System.out.print("*"); } System.out.println();//只有一个功能就是换行。 } System.out.println("-------------------"); /* ***** **** *** ** * 发现图形有很多行,每一个行有很多列。 要使用嵌套循环。原理:形象说法:大圈套小圈。 */ //int z = 5; for (int x=0; x<5 ;x++ )//x<5:因为外循环控制行数。一共5行。 { for (int y=x; y<5 ;y++) { System.out.print("*"); } System.out.println(); //z++; } } } /* **** **** **** 对于打印长方形:外循环控制的行数。内循环控制的是每一行的列数。也就是一行中元素的个数。 ***** **** *** ** * */
九九乘法表:
/*
*
**
***
****
*****
1
12
123
1234
12345
九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
*/
class ForForTest
{
public static void main(String[] args)
{
/*
*
**
***
****
*****
不是规律的规律:
尖朝上,可以改变条件。让条件随着外循环变化。
尖朝下,可以初始化值,让初始化随着外循环变化。
*/
for (int x=0; x<5 ;x++ )
{
for (int y=0 ; y<=x ; y++ )
{
System.out.print("*");
}
System.out.println();
}
System.out.println("----------------------");
/*
1
12
123
1234
12345
*/
for (int x=1; x<=5; x++)
{
for (int y=1; y<=x;y++ )
{
System.out.print(y);
}
System.out.println();
}
System.out.println("----------------------");
/*
九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
*/
for (int x=1; x<=9 ; x++)
{
for (int y=1; y<=x; y++)
{
System.out.print(y+"*"+x+"="+y*x+" ");
}
System.out.println();
}
}
}
/* ----* ---* * --* * * -* * * * * * * * * * * * * * -* * * * --* * * ---* * ----* */ class ForForTest2 { public static void main(String[] args) { for (int x=0; x<5 ;x++ ) { for(int y=x+1; y<5 ; y++) { System.out.print(" "); } for(int z=0; z<=x ; z++) { System.out.print("* "); } System.out.println(); } } }
class ForTest { public static void main(String[] args) { int x = 1; for(System.out.println("a"); x<3 ;System.out.println("c"),x++) { System.out.println("d"); //x++; } //adcdc for(int y=0; y<3; y++) { } /* 无限循环的最简单表现形式。 for(;;){} while(true){} */ } }
累加计数器:
/* 1,获取1~10的和,并打印。 */ class ForTest2 { public static void main(String[] args) { /* //1,定义变量用于存储不断变化的和。 int sum = 0; //2,定义变量,记录住不断变化的被加的数。 int x = 1; //3,定义循环,重复加法的过程。 while(x<=10) { sum = sum + x; x++; } System.out.println("sum="+sum); */ /* 循环注意: 一定要明确哪些语句需要参与循环,哪些不需要。 */ /* 0+1 1+2 3+3 6+4 */ //用for来体现。 int sum = 0; for(int x=0; x<=10; x++) { sum += x; } System.out.println("for sum = "+sum); /* 其实这就是累加思想。 原理:通过变量记录住每次变化的结果。 通过循环的形式。进行累加动作。 */ } }
/* 2,1~100之间 7的倍数的个数。并打印。 思路: 1,先对1~100进行循环(遍历)通过循环的形式。 2,在遍历的过程中,定义条件。只对7的倍数进行操作。 3,因为7的倍数不确定,只要符合条件,就通过一个变量来记录住这个变化的次数。 步骤: 1,定义循环语句,选择for语句。 2,在循环中定义判断。只要是7的倍数即可。使用if语句。条件:7的倍数 x%7==0; 3,定义变量,该变量随着7的倍数的出现而自增。 */ class ForTest3 { public static void main(String[] args) { int count = 0; for(int x=1; x<=100; x++) { if(x%7==0) //System.out.println("x="+x); count++; } System.out.println("count="+count); /* 计数器思想。 通过一个变量记录住数据的状态变化。 也许通过循环完成。 */ } }
函数:
class FunctionDemo { public static void main(String[] args) { /* int x = 4; System.out.println(x*3+5); x = 6; System.out.println(x*3+5); */ //int y = 4*3+5; //int z = 6*3+5; //int x = getResult(4); //System.out.println("x="+x); //int y = getResult(6); getResult(5); } //发现以上的运算,因为获取不同数据的运算结果,代码出现了重复。 //为了提高代码的复用性。对代码进行抽取。 //将这个部分定义成一个独立的功能。方便与日后使用。 //java中对功能的定义是通过函数的形式来体现的。 //需要定义功能,完成一个整数的*3+5的运算, //1,先明确函数定义的格式。 /* 修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,) { 执行语句; return 返回值; } //当函数运算后,没有具体的返回值时,这是返回值类型用一个特殊的关键字来标识。 //该关键字就是void。void:代表的是函数没有具体返回值的情况。 //当函数的返回值类型是void时,函数中的return语句可以省略不写。 */ public static void getResult(int num) { System.out.println(num * 3 + 5); return;//可以省略 } }
class FunctionDemo2 { public static void main(String[] args) { /* int sum = getSum(4,6); System.out.println("sum="+sum); sum = getSum(2,7); System.out.println("sum="+sum); */ //get(4,5); int x = getSum(4,4); int y = getSum(7,9); int num = getMax(x,y); } /* 这个功能定义思想有问题,为什么呢?因为只为完成加法运算,至于是否要对和进行打印操作, 那是调用者的事,不要在该功能中完成。 public static void get(int a,int b) { System.out.println(a+b); return ; } */ /* 如何定义一个函数呢? 1,既然函数是一个独立的功能,那么该功能的运算结果是什么先明确 因为这是在明确函数的返回值类型。 2,在明确在定义该功能的过程中是否需要未知的内容参与运算。 因为是在明确函数的参数列表(参数的类型和参数的个数)。 */ //需求:定义一个功能。完成3+4的运算。并将结果返回给调用者。 /* 1,明确功能的结果:是一个整数的和。 2,在实现该功能的过程中是否有未知内容参与运算,没有。 其实这两个功能就是在明确函数的定义。 1,是在明确函数的返回值类型。 2,明确函数的参数列表( 参数的类型和参数的个数)。 public static int getSum() { return 3+4; } */ /* 以上这个函数的功能,结果是固定的,毫无扩展性而言。 为了方便用户需求。由用户来指定加数和被加数。这样,功能才有意义。 思路: 1,功能结果是一个和。返回值类型是int。 2,有未知内容参与运算。有两个。这个两个未知内容的类型都是int。 */ public static int getSum(int x,int y) { return x+y; } /* 需求:判断两个数是否相同。 思路: 1,明确功能的结果:结果是:boolean 。 2,功能是否有未知内容参与运算。有,两个整数。 */ public static boolean compare(int a,int b) { /* if(a==b) return true; //else return false; */ //return (a==b)?true:false; return a==b; } /* 需求:定义功能,对两个数进行比较。获取较大的数。 */ public static int getMax(int a,int b) { /* if(a>b) return a; else return b; */ return (a>b)?a:b; } }
重载
/* 什么时候用重载? 当定义的功能相同,但参与运算的未知内容不同。 那么,这时就定义一个函数名称以表示起功能,方便阅读,而通过参数列表的不同来区分多个同名函数。 */ class FunctionOverload { public static void main(String[] args) { // add(4,5); // add(4,5,6); print99(); } public static void print99(int num) { for(int x=1; x<=num; x++) { for(int y=1; y<=x; y++) { System.out.print(y+"*"+x+"="+y*x+" "); } System.out.println(); } } //打印99乘法表 public static void print99() { print99(9); } //定义一个加法运算,获取两个整数的和。 public static int add(int x,int y) { return x+y; } //定义一个加法,获取三个整数的和。 public static int add(int x,int y,int z) { return add(x,y)+z; } } /* void show(int a,char b,double c){} a. void show(int x,char y,double z){}//没有,因为和原函数一样。 b. int show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。 c. void show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。 d. boolean show(int c,char b){}//重载了,因为参数个数不同。 e. void show(double c){}//重载了,因为参数个数不同。 f. double show(int x,char y,double z){}//没有,这个函数不可以和给定函数同时存在与一个类中。 */
矩形,99表
/* 1,定义一个功能,用于打印矩形。 2,定义一个打印99乘法表功能的函数。 */ class FunctionTest { public static void main(String[] args) { // draw(5,6); // printHr(); // draw(7,9); // printHr(); print99(); } /* 定义一个打印99乘法表功能的函数。 */ public static void print99() { for(int x=1; x<=9; x++) { for(int y=1; y<=x; y++) { System.out.print(y+"*"+x+"="+y*x+" "); } System.out.println(); } } /* 定义一个功能,用于打印矩形。 思路: 1,确定结果:没有,因为直接打印。所以返回值类型是void 2,有未知内容吗?有,两个,因为矩形的行和列不确定。 */ public static void draw(int row,int col) { for(int x=0; x<row; x++) { for(int y=0; y<col; y++) { System.out.print("*"); } System.out.println(); } } public static void printHr() { System.out.println("------------------------------"); } }
面向对象函数:
http://www.cnblogs.com/itcqx/p/5546906.html
1 /* 2 练习:3000米长的绳子,每天减一半。问多少天这个绳子会小于5米?不考虑小数。 3 4 */ 5 class ForTest4 6 { 7 public static void main(String[] args) 8 { 9 int day = 0; 10 for(int x=3000; x>=5; x/=2) 11 { 12 day++; 13 } 14 System.out.println("day="+day); 15 16 /* 17 for(int x=3000; x>=5; day++) 18 { 19 x = x/2; 20 } 21 */ 22 } 23 }