work4的code和问题

//for循环
package cn.itcast.work4;
/*
* A:循环结构的分类
* for,while,do...while
* B:循环结构for语句的格式:
*
for(初始化表达式;条件表达式;循环后的操作表达式) {
循环体;
}
* C执行流程:
* a:执行初始化语句
* b:执行判断条件语句,看其返回值是true还是false
* 如果是true,就继续执行
* 如果是false,就结束循环
* c:执行循环体语句;
* d:执行循环后的操作表达式
* e:回到B继续。
* D:案例演示
* 在控制台输出10次"helloworld"
*/
class For {
public static void main(String[] args) {
//在控制输出10次helloworld,这样做不推荐,因为复用性太差
/*System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");*/

for (int i = 1;i <= 10 ;i++ ) {
System.out.println("helloworld");
}
}
}

运行结果:

//while循环
package cn.itcast.work4;
/*
* A:循环结构while语句的格式:
*
while循环的基本格式:
while(判断条件语句) {
循环体语句;
}

完整格式:

初始化语句;
while(判断条件语句) {
循环体语句;
控制条件语句;
}
* B:执行流程:
* a:执行初始化语句
* b:执行判断条件语句,看其返回值是true还是false
* 如果是true,就继续执行
* 如果是false,就结束循环
* c:执行循环体语句;
* d:执行控制条件语句
* e:回到B继续。
* C:案例演示
* 需求:请在控制台输出数据1-10
*/
class While {
public static void main(String[] args) {
int x = 1;
while (x <= 10) {
System.out.println("x = " + x);
x++;
}
}
}

 运行结果:

//do_while循环
package cn.itcast.work4;
/*
* A:循环结构do...while语句的格式:
*
do {
循环体语句;
}while(判断条件语句);

完整格式;
初始化语句;
do {
循环体语句;
控制条件语句;
}while(判断条件语句);
* B:执行流程:
* a:执行初始化语句
* b:执行循环体语句;
* c:执行控制条件语句
* d:执行判断条件语句,看其返回值是true还是false
* 如果是true,就继续执行
* 如果是false,就结束循环
* e:回到b继续。
* C:案例演示
* 需求:请在控制台输出数据1-10
*/
class DoWhile {
public static void main(String[] args) {
//while 和do while的区别
/*int i = 11;
do {
System.out.println("i = " + i);
i++;
}
while (i <= 10);

System.out.println("---------------------");

int j = 11;
while (j <= 10) {
System.out.println("j = " + j);
j++;
}*/

/*for (int i = 1;i <= 10 ;i++ ) {
System.out.println("i = " + i);
}

//System.out.println("i = " + i); for语句执行后变量会被释放,不能再使用
System.out.println("-------------------");
int i = 1;
while (i <= 10) {
System.out.println("i = " + i);
i++;
}
System.out.println("-------------------");
System.out.println("i = " + i); //while语句执行后,初始化变量还可以继续使用*/

//while语句的无限循环
/*while (true) {
System.out.println("hello world");
}*/

//System.out.println("hello world");
//for语句的无限循环
for (; ; ) {
System.out.println("hello world");
}
}

}

运行结果:

//循环嵌套之九九乘法表

package cn.itcast.work4;
/*
* A:案例演示
* 需求:在控制台输出九九乘法表。

1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
...

*
**
***
*/
class For_99 {
public static void main(String[] args) {
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();
}

//System.out.println("\""); 转义双引号
//System.out.println('\''); //转义单引号
}
}

 运行结果:

 

//控制语句之break
package cn.itcast.work4;
/*
* A:break的使用场景
* 只能在switch和循环中
*/
class Break {
public static void main(String[] args) {
for (int x = 1;x <= 10 ;x++ ) {
if (x == 4) {
break; //跳出循环
}

System.out.println("x = " + x);
}
}
}

运行结果:

//控制语句之continue
package cn.itcast.work4;
/*
* A:continue的使用场景
* 只能在循环中
*/
class Continue {
public static void main(String[] args) {
for (int x = 1;x <= 10 ;x++ ) {
if (x == 4) {
continue; //终止本次循环,继续下次循环
}

System.out.println("x = " + x);
}
}
}

运行结果:

//控制语句之标记
package cn.itcast.work4;

class Mark { //mark 标记
public static void main(String[] args) {
outer: for (int i = 1;i <= 10 ;i++ ) { //a就是标号,只要是合法的标识符即可
System.out.println("i = " + i);
inner: for (int j = 1;j <= 10 ;j++ ) {
System.out.println("j = " + j);
break outer;
}
}

System.out.println("大家好");
http://www.heima.com
System.out.println("才是真的好");
}
}

运行结果:

//控制语句之return
package cn.itcast.work4;

class Return {
public static void main(String[] args) {
for (int i = 1;i <= 10 ;i++ ) {
System.out.println(i);
if (i == 4) {
//break; //停止循环
return; //返回的意思,用来返回方法
}
}

System.out.println("循环结束了");
}
}

运行结果:

//新方法的构建
package cn.itcast.work4;

class Method {
public static void main(String[] args) {

}

/*
* C:方法的格式
*
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {
方法体语句;
return 返回值;
}
* D:方法的格式说明
* 修饰符:目前就用 public static。后面我们再详细的讲解其他的修饰符。
* 返回值类型:就是功能结果的数据类型。
* 方法名:符合命名规则即可。方便我们的调用。
* 参数:
* 实际参数:就是实际参与运算的。
* 形式参数;就是方法定义上的,用于接收实际参数的。
* 参数类型:就是参数的数据类型
* 参数名:就是变量名
* 方法体语句:就是完成功能的代码。
* return:结束方法的。
* 返回值:就是功能的结果,由return带给调用者。
*/
}

//构建求和函数
package cn.itcast.work4;
/*
* A:如何写一个方法
* 1,明确返回值类型
* 2,明确参数列表
* B:案例演示
* 需求:求两个数据之和的案例
* C:方法调用图解
*/
class Method_Sum {
public static void main(String[] args) {
/*int a = 10;
int b = 20;
int sum = a + b;
System.out.println(sum);

int c = 30;
int d = 40;
int sum2 = c + d;
System.out.println(sum2);*/

int sum = add(10,20);
System.out.println(sum);

//add(30,40); //有返回值方法的单独调用,没有意义
System.out.println(add(30,40)); //这样调用是可以,but如果需要用这个结果不推荐这样调用

//盘子 = 炒菜(地沟油,苏丹红,镉大米,烂白菜);
}

/*
求两个整数的和
1,整数的和结果应该还是整数
2,有两个未知内容参与运算

如何写方法
1,明确返回值类型
2,明确参数列表

* 修饰符:目前就用 public static。后面我们再详细的讲解其他的修饰符。
* 返回值类型:就是功能结果的数据类型。
* 方法名:符合命名规则即可。方便我们的调用。
* 参数:
* 实际参数:就是实际参与运算的。
* 形式参数;就是方法定义上的,用于接收实际参数的。
* 参数类型:就是参数的数据类型
* 参数名:就是变量名
* 方法体语句:就是完成功能的代码。
* return:结束方法的。
* 返回值:就是功能的结果,由return带给调用者。
*/
public static int add(int a,int b) { //int a = 10,int b = 20
int sum = a + b;
return sum; //如果有返回值必须用return语句带回
}

/*
盘子 炒菜(油,调料,米,菜) {
炒菜的动作
return 一盘菜;
}
*/

}

运行结果:

//重载函数Overload
package cn.itcast.work4;
/*
重载:方法名相同,参数列表不同,与返回值类型无关
重载的分类
1,参数个数不同
2,参数类型不同
顺序不同
*/
class Overload { //overload重载
public static void main(String[] args) {
double sum1 = add(10,20.1);
System.out.println(sum1);

int sum2 = add(10,20,30);
System.out.println(sum2);

double sum3 = add(12.3,13);
System.out.println(sum3);
}

/*
求两个整数的和
1,返回值类型int
2,参数列表 int a,int b
*/

public static double add(int a,double b) {
return a + b;
}

/*
求三个整数的和
1,返回值类型int
2,参数列表 int a,int b,int c
*/

public static int add(int a,int b,int c) {
return a + b + c;
}

/*
求两个小数的和
1,返回值类型double
2,参数列表 double a,double b
*/

public static double add(double a,int b) {
return a + b;
}
}

运行结果:

问题 1:for循环的格式?要能看懂执行流程。
    用for循环完成如下案例

    求和
    求偶数和
    求奇数和
    打印水仙花数
    统计水仙花数

    九九乘法表

答:

* A:循环结构的分类
* for,while,do...while
* B:循环结构for语句的格式:
*
for(初始化表达式;条件表达式;循环后的操作表达式) {
循环体;
}
* C执行流程:
* a:执行初始化语句
* b:执行判断条件语句,看其返回值是true还是false
* 如果是true,就继续执行
* 如果是false,就结束循环
* c:执行循环体语句;
* d:执行循环后的操作表达式
* e:回到B继续。

问题 2:while循环的格式?要能看懂执行流程
    用while循环完成如下案例

    求和
    纸张折叠成珠穆朗玛峰高度的次数

问题 3:break,continue和return分别有什么用?

答:

* A:break的使用场景
* 只能在switch和循环中


* A:continue的使用场景
* 只能在循环中

控制跳转语句标号:

* 标号:标记某个循环对其控制
* 标号组成规则:其实就是合法的标识符

* A:return的作用

* 返回
* 其实它的作用不是结束循环的,而是结束方法的。

* return和break以及continue的区别?
* return是结束方法
* break是跳出循环
* continue是终止本次循环继续下次循环


问题 4:函数的概念?函数的格式?格式的解释说明

答:

* A:为什么要有方法
* 提高代码的复用性
* B:什么是方法
* 完成特定功能的代码块。
* C:方法的格式
*
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {
方法体语句;
return 返回值;
}
* D:方法的格式说明
* 修饰符:目前就用 public static。后面我们再详细的讲解其他的修饰符。
* 返回值类型:就是功能结果的数据类型。
* 方法名:符合命名规则即可。方便我们的调用。
* 参数:
* 实际参数:就是实际参与运算的。
* 形式参数;就是方法定义上的,用于接收实际参数的。
* 参数类型:就是参数的数据类型
* 参数名:就是变量名
* 方法体语句:就是完成功能的代码。
* return:结束方法的。
* 返回值:就是功能的结果,由return带给调用者。

问题 5:函数的调用
  A:明确返回值类型的函数调用
  B:void类型的函数调用

问题 6:函数的练习:
  A:求两个数据之和
  B:判断两个数据是否相等
  C:获取两个数中较大的值
  D:打印m行n列的星形矩形
  E:打印nn乘法表

问题 7:什么是函数重载?

 答:

* A:方法重载概述
* 求和案例
* 2个整数
* 3个整数
* 4个整数
* B:方法重载:
* 在同一个类中,方法名相同,参数列表不同。与返回值类型无关。

* 参数列表不同:
* A:参数个数不同
* B:参数类型不同
* C:参数的顺序不同(算重载,但是在开发中不用)

 

posted @ 2016-08-23 12:36  匆匆这些年  阅读(414)  评论(0编辑  收藏  举报