您的位置:首页 > 编程语言 > Java开发

Java学习第三天:函数语句

2014-01-22 12:16 274 查看
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

UNTIL3

1)语句while

2)语句do-while

3)For语句

4)语句之间的区别

5)循环语句的特点

6)For语句的练习

7)关键字break和continue

8)函数的概述和练习

 

一、语句while

While语句格式:

While(条件表达式){}true的话继续循环

二、语句do-while

Do-while语句个事:

do{}while(条件表达式) 和上面的while相同 不过不管条件是否为false  都会进行一次的运算

三、For语句和选择语句

For语句格式:for(变量;条件;循环操作){};

选择结构——switch语句

     (1)格式:

  switch(表达式){

      case 取值1:

      执行语句;break;

      case 取值2:

      执行语句;break;

      ``````

      default:

      执行语句;break;

    }

 

    (2)特点:

         a、switch语句选择的类型只有四种:byte,short,int , char。

         b、case之间与default没有顺序。先执行第一个case,没有匹配的case执行default。

         c、结束switch语句的两种情况:遇到break,执行到switch语句结束。

         d、如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,

            直到遇到break或者switch结尾结束。

     

四、语句之间的区别

For和while之间的区别在于,for循环作用的范围和占用的资源都比while优秀

五、循环语句的特点

无限循环:

For(;;){while(true)}

六、For语句的练习

1、获取1-10的和,并且打印

public class math_for {

public static void main(String[] args) {
//获取1-10的和,并且打印
int sum = 0;
for(int x = 1;x<=10;x++)
{
sum+=x;
}
System.out.println(sum);
}
}


2、1-100之间7的倍数,并且打印

public class math_for_02 {

public static void main(String[] args) {
//1-100之间7的倍数,并且打印
for(int x = 1;x<101;x++)
{
if(x%7==0)
{
if(x<95)
System.out.print(x+".");
else
System.out.print(x);
}
}
}
}


3、嵌套练习

A.

public class nesting_01 {

public static void main(String[] args) {
/*
嵌套练习:    输出这样的图形
*
**
***
****
*****
*/
for(int x = 1;x<6;x++)
{
for(int y = 0;y<x;y++)
{
System.out.print("*");
}
System.out.println();
}

}
}


 

B.

public class nesting_02 {
/*
嵌套练习:    输出这样的图形
1
12
123
1234
12345
*/
public static void main(String[] args) {
for(int x = 1;x<6;x++)
{
for(int y =1;y<=x;y++)
{
System.out.print(y);
}
System.out.println();
}

}
}


 

C.

public class nesting_03 {

public static void main(String[] args) {
// 输出九九乘法表
for(int x = 1;x<10;x++)
{
for(int y = 1;y<=x;y++)
{
System.out.print(y+"x"+x+"="+x*y+"\t");
}
System.out.println();
}

}

}


 

D、

public class nesting_04 {
/*
嵌套练习:    输出这样的图形
-----*
----* *
---* * *
--* * * *
-* * * * *
*/
public static void main(String[] args) {
for(int x = 0;x<5;x++)
{
for(int y = 5;y>x;y--)
{
System.out.print("-");
}
for(int z = 0;z<=x;z++)
{
System.out.print("*"+" ");
}
System.out.println();
}

}

}


七、关键字break和continue

Break作用范围在选择语句和循环语句之中!跳出该选择或循环

Continue只能作用在循环语句中!跳出本次(continue下面的代码不会给执行)循环接着循环下去

 

选择语句格式:

Switch(表达式)



case 常量1:

语句1

break;

 

case 常量2:

语句2

break;

default:

默认处理语句!也就是说一定会执行的

breank;



八、函数的概述和练习

 

什么是函数:

函数就是定义在类中的具有特定功能的一段独立小程序!

函数也叫做方法。

函数的格式:

修饰符 返回值类型 函数名(参数类型 形式参数)



执行语句

Return 返回值



 

函数练习:

public class demo_01 {

public static void main(String[] args) {
// 定义一个打印矩形的函数
rec(7,7);
}
public static void rec(int row,int col)
{
for(int x = 0;x<col;x++)
{
for(int y = 0;y<row;y++)
{
if(x==0 || x==(col-1))
System.out.print("*");
else
{
if(y==0 || y==(row-1))
System.out.print("*");
else
System.out.print(" ");
}
}
System.out.println();
}
}
}


什么是重载函数:

函数重载是指函数的名字相同但是参数不同~只要参数类型和参数的数量不同但是名字相同的,都是重载函数。(与修饰符和返回值类型是无关的)

 

 

----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息