Java学习笔记03
循环语句
while:先判断条件,只有条件满足才执行循环体
do while:先执行循环体,在判断条件,条件满足,在继续执行循环体。
总结:do while:无论条件是否满足,循环体至少执行一次
for循环参与运算时初始化表达式只执行一次
条件不满足循环结束
while与for区别
变量作用域不同
变量有自己的作用域。对于for来讲:如果将用于控制循环的增量定义在for语句中。那么该变量只在for语句内有效。For语句执行完毕,该变量在内存中被释放。
for和while可以进行互换。如果需要定义循环增量。用for更为合适。
public class ForTest{
public static void main(String [] args){
int x=1;
for(System.out.println("a");x<3;System.out.println("b")){
System.out.println("c");
x++;
}
}
}
输出结果:
a
c
b
c
b
下面两个for循环运行结果是一样的
无限循环的最简单表现形式
for ( ; ; ){}
while(true){}
计算并输出1到10的和
public class ForTest
{
public static void main(String []args){
int x=1,sum=0;
for(;x<11;x++){
sum+=x;
}
System.out.println(sum);
}
}
注意:输出语句不能写到循环体中。
一定要明确哪些代码需要循环哪些不需要
累加思想:通过变量记录住循环操作后的结果
计数器思想
通过一个变量记录住数据的状态变化,也需要通过循环来完成。
循环嵌套
public class Test
{
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("*");
}
}
}
输出结果:
****
****
****
打印出的是一个长方形:外循环控制行数,内循环控制列数。
打印一个倒三角形
public class Test{
public static void main(String []args){
for(int x=5;x>0;x--){
for(int y=1;y<x;y++){
System.out.print("*");
}
System.out.println("*");
}
}
}
输出结果:
*****
****
***
**
*
打印正三角
public class ForTest{
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("*");
}
}
}
输出结果:
*
**
***
打印九九乘法表
class Demo{
public static void main(String[] args){
for(int x=1;x<=9;x++){
for(int y=1;y<=x;y++){
System.out.print(y+"*"+x+"="+x*y+"\t");
}
System.out.println();
}
}
}
输出结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
练习
class Demo{
public static void main(String[] args){
for(int x=0;x<5;x++){
for(int y=x;y<4;y++){
System.out.print("-");
}
for(int z=0;z<=x;z++){
System.out.print("* ");
}
System.out.println();
}
}
}
输出结果:
----*
---* *
--* * *
-* * * *
* * * * *
函数
什么是函数?
函数就是定义在类中的具有特定功能的一段独立的小程序。
函数也称方法。
函数的格式:
修饰r符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2------)
{
执行语句;
return 返回值;
}
返回值类型:函数运行后结果的数据类型。
参数类型:参数的数据类型。
实际参数:传递给形式参数的具体数值。
Return:用于结束函数。
返回值:改值会返回给调用者。
打印矩形:
public class FunctionTest
{
public static void main(String[] args){
draw(6,8);
printLn();
draw(8,9);
printLn();
}
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 printLn(){
System.out.println("------------");
}
}
********
********
********
********
********
********
------------
*********
*********
*********
*********
*********
*********
*********
*********
------------
定义一个函数 打印九九乘法表:
public class Function2
{
public static void main(String [] args){
print99();
}
public static void print99(){
for (int x=1;x<=9;x++){
for (int y=1;y<=x;y++){
System.out.print(y+"*"+x+"="+x*y+"\t");
}
System.out.println();
}
}
}
函数的重载:
重载的概念
在同一个类中,允许存在一个以上的同名函数,只要他们的参数个数或者参数类型不同即可.
重载的特点:
与返回值类型无关,只看参数列表.
重载的好处:
方便阅读,优化了程序设计.
重载示例:
返回两个整数的和
int add(int x,int y){return x+y;}
返回三个整数的和
int add(int x,int y,int z){return x+y+z;}
返回两个小数的和
double add(double x,double y){return x+y;}
数组
int [] x=new{3};
其中int是数组中元素的数据类型
X是数组类型,数组是一种单独的数据类型。属于引用数据类型