一、Java语言基础组成-Part 3
1.7 程序流程控制
1.7.3 循环结构
For循环嵌套
示例1: - class ForForDemo
- {
- public static void main(String[] args){
- for(int x = 0; x < 3; x++){
- for(int y = 0; y < 4; y++){
- System.out.println("ok" );
- }
- }
- }
- }
复制代码 运行结果:
- class ForForDemo
- {
- public static void main(String[] args){
- for(int x = 0; x < 4; x++){ //外循环控制的是行数
- for(int y = 0; y < 5; y++){//内循环控制的是每一行的个数
- System.out.print("*" );
- }
- System.out.println();
- }
- }
- }
复制代码 运行结果:
练习1:打印出下格式的内容:
*****
****
***
**
*
答案1:
- class ForForDemo
- {
- public static void main(String[] args){
- int z = 5;
- for(int x = 0; x <= 4; x++){
- for(int y = 1; y <= 5 - x; y++){
- System.out.print("*" );
- }
- System.out.println();
- z--;
- }
- }
- }
复制代码 运行结果:
答案2: - class ForForDemo
- {
- public static void main(String[] args){
- for(int x = 1; x <= 5; x++){
- for(int y = x; y <= 5; y++){
- System.out.print("*" );
- }
- System.out.println();
- }
- }
- }
复制代码 运行结果:
练习2:打印出下格式的内容:
答案: - class ForForDemo
- {
- public static void main(String[] args){
- for(int x = 1; x <= 5; x++){
- for(int y = 1; y <= x; y++){
- System.out.print("*" );
- }
- System.out.println();
- }
- }
- }
复制代码 运行结果:
练习3:打印出下格式的内容:
54321
5432
543
54
5
答案:
- class ForForDemo
- {
- public static void main(String[] args){
- for(int x = 1; x <= 5; x++){
- for(int y = 5; y >= x; y--){
- System.out.print(y);
- }
- System.out.println();
- }
- }
- }
复制代码 运行结果:
练习4:打印出下格式的内容:
1
22
333
4444
55555
答案: - class ForForDemo
- {
- public static void main(String[] args){
- for(int x = 1; x <= 5; x++){
- for(int y = 1; y <= x; y++){
- System.out.print(x);
- }
- System.out.println();
- }
- }
- }
复制代码 运行结果:
练习5:打印九九乘法表成如下形式:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
...
答案:
- class ForForDemo
- {
- 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();
- }
- }
- }
复制代码 运行结果:
P.S.
1、代码中的"\t"是一个转义字符,也就是制表符。还有其他的一些转义字符:\n:回车,\b:退格,\r:回车符。
2、windows系统中回车符其实是由两个转义字符组成的:\r\n,linux中回车符是\n。
例子: 打印"hello world":
System.out.println("\"hello word\"");
打印\hello world\:
System.out.println("\\hello word\\");
练习6:打印出下格式的内容:
* * * * *
* * * *
* * *
* *
*
答案:
- class ForForDemo
- {
- public static void main(String[] args){
- for(int x = 1; x <= 5; x++){
- //首先打印出*前面的空格
- for(int y = 1; y < x; y++){
- System.out.print(" " );
- }
- //再打印出*
- for(int z = x; z <= 5; z++){
- System.out.print("* " );
- }
- System.out.println();
- }
- }
- }
复制代码 运行结果:
1.7.4 其他流程控制语句 break(跳出),continue(继续)。
break语句:
应用范围:选择结构和循环结构。
示例1:
- class BreakDemo
- {
- public static void main(String[] args){
- for(int x = 0; x < 3; x++){
- System.out.println("x = " + x);
- break;
- }
- }
- }
复制代码 运行结果:
示例2:
- class BreakDemo
- {
- public static void main(String[] args){
- for(int x = 0; x < 3; x++){
- if(x == 1)
- break;
- System.out.println("x = " + x);
- }
- }
- }
复制代码 运行结果:
示例3: - class BreakDemo
- {
- public static void main(String[] args){
- for(int x = 0; x < 3; x++){
- for(int y = 0; y < 4; y++){
- System.out.println("x = " + x);
- break;
- }
- }
- }
- }
复制代码 运行结果:
continue语句:
应用范围:循环结构。
continue语句是结束本次循环继续下次循环。
示例1:
- class ContinueDemo
- {
- public static void main(String[] args){
- for(int x = 0; x < 11; x++){
- if(x % 2 == 0)
- continue;
- System.out.println("x = " + x);
- }
- }
- }
复制代码 运行结果:
P.S.
1、这两个语句离开应用范围,存在是没有意义的。
2、这个两个语句单独存在,下面都不可以有语句,因为执行不到。
示例1: - class BreakDemo
- {
- public static void main(String[] args){
- for(int x = 0; x < 3; x++){
- break;
- System.out.println("x = " + x);
- }
- }
- }
复制代码 运行结果:
示例2: - class ContinueDemo
- {
- public static void main(String[] args){
- for (int x = 0; x < 3; x++){
- continue;
- System.out.println("x = " + x);
- }
- }
- }
复制代码 运行结果:
3、标号的出现,可以让这两个语句作用于指定的范围。
示例1: - class BreakDemo
- {
- public static void main(String[] args){
- out:for(int x = 0; x < 3; x++){
- in: for(int y = 0; y < 4; y++){
- System.out.println("x = " + x);
- break out ;
- }
- }
- }
- }
复制代码 运行结果:
示例2: - class ContinueDemo
- {
- public static void main(String[] args){
- out: for(int x = 0; x < 3; x++){
- in: for(int y = 0; y < 4; y++){
- System.out.println("x = " + x);
- continue out ;
- }
- }
- }
- }
复制代码 运行结果:
1.8 函数
1.8.1 函数的定义
函数就是定义在类中的具有特定功能的一段独立小程序,函数也称为方法。
函数的格式:
修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,...)
{
执行语句;
return 返回值;
}
返回值类型:函数运行后的结果的数据类型。
参数类型:是形式参数的数据类型。
形式参数:是一个变量,用于存储调用函数时传递给函数的实际参数。
实际参数:传递给形式参数的具体数值。
return:用于结束函数。
返回值:该函数运算后的结果,该结果会返回给调用者。
示例1: - class FunctionDemo{
- public static void main(String[] args){
- int c = add(3,4);
- System.out.println("c = " + c);
- }
- public static int add(int a, int b){
- return a + b;
- }
- }
复制代码 运行结果:
1.8.2 函数的特点
1、定义函数可以将功能代码进行封装。
2、便于对该功能进行复用。
3、函数只有被调用才会被执行。
4、函数的出现提高了代码的复用性。
P.S.
1、对于函数没有具体返回值的情况,返回值类型用关键字void表示,那么该函数中的return语句如果在最后一行可以省略不写,或者写上return;。
示例:
- class FunctionDemo{
- public static void main(String[] args){
- myPrint();
- myPrint();
- }
- public static void myPrint(){
- System.out.println("hello world" );
- return;
- }
- }
复制代码 运行结果:
2、函数中只能调用函数,不可以在函数内部定义函数。否则,编译时期就会报错。
示例: - class FunctionDemo{
- public static void main(String[] args){
- public static void myPrint() {
- System.out.println( "hello world");
- return;
- }
- }
- }
复制代码 运行结果:
3、定义函数时,函数的结果应该返回给调用者,交由调用者处理。
提问:
如果需要写一个两数相加的例子,那么使用下面的代码一好呢?还是代码二好呢?
代码一:
- public static int add(int a,int b){
- return a + b;
- }
复制代码
代码二:
- public static void add(int a,int b){
- System.out.println(a + b);
- }
复制代码
答案:虽说两种都可以执行出正确的数据,代码一比较好,因为调用者只需要获取两数相加的结果,而不需要你做打印的操作!
1.8.3 函数的应用
两个明确: 明确要定义的功能最后的结果是什么?
明确在定义该功能的过程中,是否需要未知内容参与运算。
问题1:
需求:定义一个功能,完成两个整数的和的获取。
思路:既然定义功能,就可以用函数来体现。 通过两个明确来完成:
明确一:这个功能的结果是什么?是和。是功能的结果,所以该功能的返回值类型是int。其实,就是在明确函数的返回值类型。
明确二:这个功能实现过程中是否需要未知内容参与运算?有,加数和被加数。这就是函数的参数列表(参数的个数,参数的类型)。其实,就是在明确参数列表。
P.S.
返回值类型和参数类型没有直接关系。
代码:
- public static int add(int a,int b){
- return a + b;
- }
复制代码
问题2:
需求:定义一个功能,画一个矩形在控制台。
思路:既然定义功能,就可以用函数来体现。
明确一:这个功能结果是什么?没有结果,因为直接打印到了控制台,并未返回给调用者。用void表示。
明确二:这个功能实现过程中是否需要未知内容参与运算?有,行和列不确定,两个,整数int类型。
代码: - public static void draw(int row,int col){
- for(int x = 1; x <= row; x++){
- for(int y = 1; y <= col; y++){
- System.out.print("*");
- }
- System.out.println();
- }
- return;//可以省略不写
- }
复制代码
问题3:
需求:两个数是否相等。
明确一:这个功能的结果是什么?有,boolean类型。
明确二:这个功能实现过程中是否需要未知内容参与运算?有,两个整数。
代码:
- public static boolean equals(int a,int b){
- return a == b;
- }
复制代码
问题4:
需求:定义一个功能,获取两个整数中较大的那个数。
明确一:这个功能的结果是什么?有,是int类型。
明确二:这个功能实现过程中是否需要未知内容参与运算?有,参与比较的两个整数,int类型。
代码:
- public static int getMax(int a,int b){
- return a > b?a:b;
- }
复制代码
问题5:
定义功能,打印99乘法表。
代码:
- public static void print99(){
- for(int x = 1; x <= 9; x++){
- for(int y = 1; y <= x; y++){
- System.out.println(y + "*" + x + "=" + y*x + "\t");
- }
- }
- }
复制代码
问题6:
根据考试成绩获取学生分数对应的等级。
90~100 A
80~89 B
70~79 C
60~69 D
60以下 E
代码:
- public static char getLevel(int num){
- char level;
- if(num >= 90 && num <= 100)
- level = 'A';
- else if(num >= 80 && num <= 89)
- level = 'B';
- else if(num >= 70 && num <= 79)
- level = 'C';
- else if(num >= 60 && num <= 69)
- level = 'D';
- else
- level = 'E';
- return level;
- }
复制代码 1.8.4 函数的重载
重载的概念:
在同一个类中,允许存在一个以上的同名函数,只要它们的参数个数或者参数类型不同即可。
重载的好处:
方便于阅读,优化了程序设计。
示例:
- class FunctionDemo{
- public static void main(String[] args){
- System.out.println(add(3,4));
- System.out.println(add(3.0,4.0));
- System.out.println(add(3,4,5));
- }
- //加法运算,两个整数的和
- public static int add(int a, int b){
- return a + b;
- }
- //加法运算,两个小数的和
- public static double add(double a, double b){
- return a + b;
- }
- //加法运算,三个整数的和
- public static int add(int a, int b,int c){
- return add(a,b) + c;
- }
- }
复制代码 运行结果:
总结:
函数的功能一样,仅仅是参与运算的未知内容不同时,可以定义多函数,却使用统一函数名称,这样方便阅读。在调用时,虚拟机通过参数列表的不同来区分同名函数。
P.S.
1、重载与返回值类型无关,只看参数列表。
2、java是严谨性语言,如果函数调用出现不确定性,会编译失败。
练习:打印乘法表。 - class FunctionDemo{
- public static void main(String[] args){
- printCFB(5);
- System.out.println("----------------------------------------------------------------");
- printCFB();
- }
- public static void printCFB(int num){
- for(int x = 1; x <= num; x++){
- for(int y = 1; y <= x; y++){
- System.out.print(y + "*" + x + "=" + y*x + "\t");
- }
- System.out.println();
- }
- }
- public static void printCFB(){
- for(int x = 1; x <= 9; x++){
- for(int y = 1; y <= 9; y++){
- System.out.print(y + "*" + x + "=" + y*x + "\t");
- }
- System.out.println();
- }
- }
- }
复制代码 运行结果:
1.9 数组 1.9.1 数组的定义
概念:
同一种类型数据的集合。其实,数组就是一个容器。
数组的好处:
可以自动给数组中的元素从0开始编号,方便操作这些元素。
格式1:
元素类型[] 数组名 = new 元素类型[元素个数或数组长度];
示例1:需要一个容器,但是不明确容器的具体数据。
示例2:
- class ArrayDemo{
- public static void main(String[] args){
- int[] arr = new int[3];
- System. out.println(arr[0]);
- arr[0] = 89;
- System. out.println(arr[0]);
- }
- }
复制代码 运行结果:
格式2:需要一个容器,存储已知的具体数据。
元素类型[] 数组名 = new 元素类型[]{元素,元素,……};
示例:
- int[] arr = new int[]{3,5,1,7};
复制代码 或
1.9.2 数组的内存分配及特点
int[] arr = new int[4];
内存分配图:
Java程序在运行时,需要在内存中分配空间。为了提高运算效率,又对空间进行了不同区域的划分,因为每一片区域都有特定的处理数据方式和内存管理方式。
内存的划分: 1. 寄存器。 2. 本地方法区。 3. 方法区。 4. 栈内存。 5. 堆内存。
栈内存:
用于存储局部变量,当变量所属的作用域一旦结束,所占空间会自动释放。
堆内存:
数组和对象,通过new建立的实例都存放在堆内存中。
每一个实体都有内存地址值。
实体中的变量都有默认初始化值,根据类型的不同而不同。整数类型是0,小数类型是0.0或0.0f,boolean类型是false,char类型是'\u0000'。
如果将数组的引用实体设置为null,也就是实体不再被使用,那么会在不确定的时间内被垃圾回收器回收。
1.9.3 数组操作常见问题
数组脚标越界异常(ArrayIndexOutOfBoundsException):访问到了数组中的不存在的脚标时发生。
示例1: - class ArrayDemo{
- public static void main(String[] args){
- int[] arr = new int[3];
- System. out.println(arr[3]);
- }
- }
复制代码 运行结果:
空指针异常(NullPointerException):当引用型变量没有指向任何实体时,用其操作实体,就会发生该异常。
示例2: - class ArrayDemo{
- public static void main(String[] args){
- int[] arr = null;
- System. out.println(arr[0]);
- }
- }
复制代码 运行结果:
P.S.
直接打印数组的引用变量,打印出来的结果是数组初始地址的哈希值。
示例3:
- 示例3:
- ArrayDemo.java
- class ArrayDemo{
- public static void main(String[] args){
- int[] arr = new int[3];
- System. out.println(arr);
- }
- }
复制代码 运行结果:
P.S.
"[I"表示的是int类型数组,"@"后面的内容表示数组初始地址的哈希值。
|
|