第一章 第一个JAVA程序解析
public class HelloWorld{
public static void main(String[] args){
System.out.println("欢迎学习JAVA");
}
——
}
注意:
- JAVA 明确区分大小写,关键字都是小写(有特殊用法的单词)
- 类名要大写
- JAVA严格区分单双引号
1.1 程序解析
public:关键字,固定用法的单词,公开的,别人可以使用
class:关键字,用于定义一个JAVA类,类是JAVA源程序的最小单元,JAVA文件是由一个个类构成的。
一个JAVA文件只有一个加public的类,主类,该类名和要文件名完全相同。
HelloWorld:类的名字
1.1.1 类名规范
1.合法的标识符(字母,数字,_ ,$ ,构成,不能是数字开头)
2.不能是关键字
3.首字母大小,符合单词,每个单词的首字母都大写
4.有一定的含义
1.1.2 语句块:一个语句块,一个整体
{
}
1.1.3 主方法(主函数)
程序的入口,项目至少有一个主方法,运行程序的起点
public static void main(String[] args){
}
快捷键快速生成:main alt+/
1.1.4 程序的输出语句
System.out.println("欢迎学习JAVA");输出内容 并且换行
System.out.print("欢迎学习JAVA");输出内容 不换行
//注意:
print 加ln 和 不加ln的区别
快速生成:sout alt+/
双引号代表字符串: " " (原样打印的内容)
分号代表语句的结束: ;
1.2 JAVA的注释
注释不用于运行,而是用于提示作用的语句,开发时一定要养成写注释的习惯
三种注释:
// 单行注释 ctrl+/
/* */ 多行注释
/** */ 文档注释(用于标注JAVA类的用途,作者,创作时间,版本...)
1.3 JAVA中+的用法
1.加法:两端都是数字
2.拼接,粘贴:(符号的两侧有一端是字符串)
package demo;
/**
* +(加号的用法)
*/
public class TestPlus {
public static void main(String[] args) {
// 加法
System.out.println(10 + 30);
System.out.println(20.6 + 12.8);// 精度问题
// 拼接
System.out.println("于老师的性格" + "温柔善良");
System.out.println("于老师的年龄" + 18);
System.out.println("于老师" + 18 + ",但是不太像");
// 结合
System.out.println(2 + 3 + "4");// 54
System.out.println(2 + "3" + 4);// 234
System.out.println("2" + 3 + 4);// 234
}
}
第二章 JAVA的基本语法
JAVA语句块的构成:
- 变量和常量
- 数据类型(决定变量或者常量的类型)
- 运算符和表达式
- 语句(分支,循环)
2.1 变量和常量
变量:可以不断变化的量
常量:值定义后不会再改变
2.1.1 变量定义语法:
//定义一个
数据类型 变量名 [= 初始值];
int a = 10;
//定义多个
数据类型 变量名1[= 初始值],变量名2[= 初始值];
int a = 1,b = 2,c = 3;
2.1.2 变量名规范
1.合法的标识符,不能是关键字,小写,复合单词驼峰式,xXX
2. = (等号代表赋值)右侧的值赋给等号左侧的变量
3.变量的值取最后一次的赋值结果
2.1.3 常量定义语法:
//定义一个
final 数据类型 常量名 [= 初始值] ;
final int MON = 12;
//定义多个
final 数据类型 常量名1 [= 初始值] , 常量名2 [= 初始值];
final int YEAR = 365,MON = 12;
2.1.4 常量名规范
1.合法的标识符
2.不能是关键字
3.全大写
2.2 基本数据类型(简单数据类型)
JAVA的数据类型:(1.决定变量的值是什么类型,2.决定变量开辟内存的大小)
四种八个:
2.2.1 整数型
类型 | 含义 | 字节数位数 | 范围 |
---|---|---|---|
byte | 字节型 | 1字节(8位) | -128 ~ 127 |
short | 短整型 | 2字节(16位) | -32768 ~ 32767 |
int | 整型 | 4字节(32位) | -2^31(-2147483648) ~ -2^31 -1(2147483647) |
long | 长整型 | 8字节(64位) | -2^63 ~ -2^63 -1 |
注意:
JAVA默认所有整数数值均为int,因此需要在long型值后面添加L或l,以作为标识。
2.2.2 浮点型(小数类型)
类型 | 含义 | 字节数(位数) | 范围 |
---|---|---|---|
float | 单精度浮点 | 4字节(32位) | IEEE,大于Long型 |
double | 双精度浮点 | 8字节(64位) | IEEE,大于float型 |
数值型排序:byte < short < int < long < float< double
注意:
JAVA默认所有浮点数值均为double,因此需要在float类型数值后面加f或F,以作为标识。double可加可不加d或D。
使用数据类型实例:
/**
测试数据类型的使用
*/
public class TestZS2 {
public static void main(String[] args) {
//姓名 String是类 字符串
String name = "diana";
//年龄
byte age = 18;
//手机号
long tel = 13610943167L;
//密码
int pass = 831;
//视力
float degree = 4.5f;
//测评分数
double score = 99.4;
//打印信息
System.out.println("年龄:" + age + ",电话:" + tel );
}
}
int a = 10;
//以0开头八进制
a = 071;// 1*1+7*8 = 57
//以0x开头十六进制
a = 0xFF; //15*1+15*16 = 255
System.out.println(a);
注意:
使用变量和数据类型的时候,赋值为八进制或十六进制(了解)
2.2.3 类型转换(数值类型之间的,重点)
自动转换:大类型 = 小类型;
强制转换:小类型 = (小类型) 大类型;
double d = 10.5;
//强制转换
int a = (int)d;
//自动转换
d = 100;
强制转换原理:
1.小数转成整数 直接砍掉小数点
2.整数之间转换:转化为二进制:short>byte 16位 强行 8位
2.2.4 字符型
char:
使用 ’ ’ 扩起来的一个字符,2字节(16位),无符号的,范围:(0 ~ 65535)
代表一个字符,一个字母,一个数字,一个汉字…
2.2.5 JAVA的编码机制:
UTF-8(Unicode 全球编码),UTF-16 UTF-32… 128+
ASCII : 128位
常用编码:
a-z (97~122)
A-Z (65~90)
0-9 (48~57)
字符型示例:
public class TestChar {
public static void main(String[] args) {
char c1 = 'a';
//c1 = 'd';
//JAVA采用UTF-8编码,汉字一个字符
//c1 = '中';
//字符型存储性别
//char xb = '女' ;
System.out.println('2' + 10);//60 求和
System.out.println("2" + 10);//210 拼接
System.out.println(2 + 10);//12
}
}
注意:
1." " 是字符串 ’ ’ 是字符型,字符型可以和数值型转换的
2.’ ’ 只能是一个字符,除非使用转义字符 \
\n 换行
//使用斜线\转义:
char c = '\'';
//打印单个 单引号
System.out.println(c);
c = '\\';
//打印一个斜线
System.out.println(c);
\t tab
\r 回车
\ ’
\ \
2.2.6 布尔型
用于逻辑判断,值只有两个
boolean: true 真 / false 假
1个bit , 默认值 false
注意:
布尔类型唯一 一个不能和其他数据类型转换的。
总结
八个数据类型: byte short int long float double char boolean
2.3 引用数据类型(复杂数据类型)
数组 类 接口 枚举 注解
2.4 计算机内存
计算机内存存储:二进制(0和1),0或1每个占位称之为一个比特位 bit,每8个比特位一组,叫一字节
符号位 | 数值位 | 数值位 | 数值位 | 数值位 | 数值位 | 数值位 | 数值位 |
---|---|---|---|---|---|---|---|
0正1负 | 0/1 | 0/1 | 0/1 | 0/1 | 0/1 | 0/1 | 0/1 |
举例 :
00000000 0 00000001 1
一字节 = 8 bit
1k = 1024字节
1m = 1024k
1g = 1024m
…
十进制: 0-9
八进制: 0-7
十六进制: 0-9 A-F
2.5 什么是原码
原码:十进制数据的二进制表现形式,最左边是符号位,0为正,1为负。
源码的弊端:利用原码进行计算的时候,如果是正数完全没有问题。(但是如果是负数计算,结果就出错,实际运算的方向,跟正确的运算方向是相反的。)
2.6 什么是反码
反码:为了解决原码不能计算负数的问题而出现的。
反码的计算规则:正数的反码不变,负数的反码在原码的基础上,符号位不变。数值取反,0变1,1变0。
反码的弊端:负数运算的时候,如果(结果不跨0),是没有任何问题的,但是如果(结果跨0),跟实际结果会有1的偏差。
2.7 什么是补码
补码:为了解决负数计算时跨0的问题而出现的。
补码的计算规则:正数的反码不变,负数的补码在反码的基础上+1。(另外补码还能多记录一个特殊的值-128,该数据在1个字节下,没有原码和反码)
补码的注意点:计算机中的存储和计算都是以补码的形式进行的。
第三章 运算符
运算符是用于对定义的变量做运算的
特点:
1.但凡做过运算,结果类型不会低于int型
byte b1 = 1, b2 = 2;
//此行代码会报错,因为整数参与运算默认是int型
//byte b3 = b1 + b2;
//所以要强转,把运算用括号括起来
byte b3 = (byte)(b1 + b2);
2.但凡做过运算,结果取出现的最高类型
int a = 10;
double b = 2.5;
//此行代码会报错,因为double类型参与运算,而且比int类型大
//int d = a * b;
//强转后结果为25,砍掉小数点
int d = (int)(a * b);
//结果为25.0,所以运算结果取最高类型double
System.out.println(a * b);
注意:
结果范围 : int ~ double
3.1 算术运算符
符号 | 含义 | 特殊注意 |
---|---|---|
+ | 加法 , 拼接 | |
- | 减法 | |
* | 乘法 | |
/ | 除法 | 除号两侧如果都是整数,只能得到整数(向下取整); 除号两侧只要有一侧是小数才能得到小数 |
% | 余数 | a%b为0,a是b的倍数; a % 2 为0 a是偶数 |
++ | 自加1 | 一个变量的自加: a++ ; ++a ; a的上面加1 |
- - | 自减1 | 一个变量的自减: a-- ; --a ; a的上面减1 |
System.out.println(10 + "20");
System.out.println(10 + '2');
System.out.println(10 + 20);
System.out.println(10.5 - 2);
System.out.println(10 * 2.5);
System.out.println(5 / 2);
System.out.println(5.0 / 2);
System.out.println(10 % 3);
System.out.println(8 % 2);
System.out.println(6 % 7);
int a = 12;
//a是6的倍数
System.out.println(a % 6);
//a是偶数
System.out.println(a % 2);
注意:
++ - - 当自加自减运算时没有其他运算或者操作,无区别
++在后:先执行其他操作,后自加1
++在前:先自加1,再执行其他操作
int a = 10;
//a ++;// 11 a = a + 1
//++ a; // 12 a = a + 1
//int b = a ++;//先赋值后加1
int b = ++ a;//先加1后赋值
System.out.println(a);//11
System.out.println(b);//10
案例:获得个位,十位,百位,求和
package com.qf.homework;
/**
* 定义一个三位数,
* 获得每个位置上数字求和
* int a = 234;
* 打印 sum = 2 + 3 + 4 = 9
*/
public class Work1 {
public static void main(String[] args) {
int a = 234;
// 获得个位
int a1 = a % 10;
// 获得十位
int a2 = a / 10 % 10;
// 获得百位
int a3 = a / 100;
// 求和变量
int sum = a1 + a2 + a3;
System.out.println("sum=" + sum);
}
}
package com.qf.homework;
/**
* 华氏温度转换为摄氏温度
* 转换公式:c = 5 / 9 * (F-32)//数学公式
*/
public class Work2 {
public static void main(String[] args) {
double f = 100;
double c = 5.0 / 9 * (f - 32);
System.out.println(c);
}
}
package com.qf.homework;
/**
* 基本类型的运算
* 一家商场在举行打折促销,
* 所有商品都进行8折优惠。
* 一位程序员把这个逻辑写成:
* short price = ...;//先计算出原价
* short realPrice = price*8/10;//再计算出打折之后的价格
* 问:
* 1.这段代码是否正确?
* 2.如果正确,假设price为100,那计算之后的realPrice值为多少?
* 3.如果不正确,应该怎么改正?
* */
public class Work3 {
public static void main(String[] args) {
//商品原价
short price = 100;
//折后价
double realPrice = price * 8.0 / 10;
//打印折后价格结果为80
System.out.println(realPrice);
}
}
package com.qf.homework;
/**
* (表达式)int a,b;
* 请用表达式判断a是否是b的倍数
* */
public class Work4 {
public static void main(String[] args) {
//定义两个变量
int a = 137, b = 75;
//判断a是否是b的倍数
boolean boo = a % b == 0;
//打印结果为false
System.out.println(boo);
}
}
package com.qf.homework;
/**
* 定义三个变量
* 获取三个变量中的最大值
*/
public class Work5 {
public static void main(String[] args) {
int a = 1, b = 4, c = 5;
//写法1
int max1 = a > b ? a : b;
int max2 = max1 > c ? max1 : c;
System.out.println("最大值是:"+max2);
//写法2
System.out.println("最大值是:" + ((a > b ? a : b) > c ? (a > b ? a : b) : c));
}
}
package com.qf.homework;
/**
* 定义变量a和b 代表矩形的宽高
* 获得矩形的周长和面积
* 周长=(长+宽)×2
* 面积=长×宽
*/
public class Work6 {
public static void main(String[] args) {
int a = 8, b = 12;
int zc = (a + b) * 2;
System.out.println("矩形的周长为:" + zc);
int mj = a * b;
System.out.println("矩形的面积为:" + mj);
}
}
package com.qf.homework;
/**
* 定义变量a
* 使用表达式判断a是否是偶数
*/
public class Work7 {
public static void main(String[] args) {
int a = 921;
//写法1
boolean num = a % 2 == 0;
//打印num的结果是不是偶数true 是偶数,false 不是偶数
System.out.println(num);
//写法2
System.out.println(a % 2 == 0 ? "是偶数" : "不是偶数");
}
}
3.2 比较运算符
又叫关系运算符,比大小。
符号 | 含义 | 特殊情况 |
---|---|---|
== | 判断两个值是否相等 | |
!= | 不等于 | |
> | ||
>= | ||
<= | ||
< |
注意:
- 比较运算符结果是:布尔类型 true false
- 经常可以用于判断的条件
- 比较的是变量的值(和内存空间的大小无关)
int a = 4, b = 3;
//System.out.println(a == b);//false
//System.out.println(a != b);//true
//System.out.println(a >= b);//true
//先比较,后自增
//System.out.println(a++ == b);//false
System.out.println(a == ++b);//true
值的比较:
int a = 10;
char c = 10;
float f = 10.0f;
double d = 10.0;
System.out.println(a == c);//true
System.out.println(c == f);//true
System.out.println(c == d);//true
System.out.println(f == d);//true
3.3 逻辑运算符
符号 | 含义 | 特殊情况 |
---|---|---|
&& | 短路与 | 当第一个操作数为假,第二个操作数不执行(被短路) |
& | 逻辑与 | 两侧都执行 |
|| | 短路或 | 当第一个操作数为真,第二个操作数不执行(被短路) |
| | 逻辑或 | 两侧都执行 |
! | 非 |
注意:
与:两端操作数,同真则真,一假则假
或:两端操作数,一真则真,同假则假
非:真变假,假变真
逻辑运算符的操作数必须是布尔类型
int a = 1, b= 2, c = 3;
//逻辑运算
System.out.println(a ++ == b && ++ b == c);
//a > b a > c
System.out.println(a > b && a > c);
//a b c 相等
System.out.println(a == b && b == c);
System.out.println(!true);
System.out.println(!(a == b));
总结:
与: && 短路 & 不短路,& 操作的是数字,不做逻辑运算,位运算
或: || 短路 | 不短路,| 操作的是数字,不做逻辑运算,位运算
int a = 1, b = 1, c = 1;
//boolean boo1 = a ++ == 1 && b ++ == 1 || c++ == 1;
//System.out.println(boo1);// true
//System.out.println(a + "," + b + "," + c);// 2 2 1
boolean boo1 = ++a == 2 || b ++ == 1 && ++c == 2;
System.out.println(boo1);// true
System.out.println(a + "," + b + "," + c);// 2 1 1
3.4 位运算:
二进制运算:先变成二进制,(了解)
按位与 & : 同为1 ,得到1 ,有0则0
按位或 | : 有1为1 ,同0则0
按位异或 ^ : 相同为0,不同为1
左移 << 左移n位 => *2的n次幂
右移 >> 右移n位 => /2的n次幂
// 2的二进制 0000 0010
// 8的二进制 0000 1000
//左移
//2乘以2的2次幂
//System.out.println(2 << 2);//0000 1000 2 * 4 = 8
//5乘以2的3次幂
//System.out.println(5 << 3); // 5 * 8 = 40
//3乘以2的2次幂
//System.out.println(3 << 2); // 3 * 4
//右移
//16除以2的2次幂
//System.out.println(16 >> 2);// 16 / 4 = 4
//32除以2的3次幂
//System.out.println(32 >> 3); // 32 / 8 = 4
System.out.println(2 & 3); // 按位与
//0000 0010
//0000 0011
//0000 0010
System.out.println(2 | 3); // 按位或
//0000 0010
//0000 0011
//0000 0011
System.out.println(2 ^ 3);// 按位异或
//0000 0010
//0000 0011
//0000 0001
3.5 条件运算符
三目运算符(三元运算符,需要三个操作数)
符号 | 含义 | 特殊情况 |
---|---|---|
条件操作数1?操作数2:操作数3 | 先做第一个操作数条件判断,如果是真的,最终取操作数2,如果是假的,最终取操作数3 | 取最高类型 |
int age = 17;
System.out.println(age > 18 ? "成年人" : "未成年");
int a = 54, b = 33;
//获得a和b的最大值
System.out.println("最大值=" + (a > b ? a : b));
//获得a和b的最小值
int min = 0;
min = a < b ? a : b;
System.out.println("最小值=" + min);
//结果是99.0,取最高类型,选c
double res = 99 > 8.0 ? 99 : 8.0;
System.out.println(res);// A 99 B 8.0 C 99.0 D 1
3.6 赋值运算符
将右侧的值赋给左侧
符号 | 含义 | 特殊情况 |
---|---|---|
= | 赋值 | |
+= | 加上再赋值,a+=10;a = a+10; | += 完成类型内部转化 |
-= | ||
*= | ||
/= | ||
%= |
byte a = 10 , b = 20;
a = 20;
// a = a + 10; 错误写法
a += 10;
System.out.println(a);//结果为30
注意:
- = 赋值,取最后一次赋值的结果
- += -= *= /= %= 可以内部转化类型
- 除了++ - - 后置,赋值运算符的优先级最低
3.7 运算符总结
优先级:
- 括号, 单目 : ! ++ – 前置 -
- 算术运算符
- 关系运算符
- 逻辑运算符 && > ||
- 条件运算符
- 赋值运算符
第四章 JAVA中的语句
4.1 语句的分类:
1.顺序语句 不改变语句的执行,按照从上往下,从左往右
2.分支语句 根据条件分情况执行,只能执行一条。 if-else ; switch
3.循环语句 根据条件语句反复执行。 for ; while ; do-while
4.跳出循环 continue;break;return; System.exit(0);
4.2 分支语句
4.2.1 if - else 结构
语法1:(1条路)
if(布尔类型条件) {
满足条件执行的语句;
}
控制台的输出:
System.out.println();
4.3 创建扫描工具Scanner
控制台的输入:
//1. 创建一个扫描工具
Scanner 变量名 = new Scanner(System.in);
//2. 导入Scanner所在包 ctrl + shift + o
//提示语
System.out.println("请输入两个数字:");
//3. 动态接收(多次接收)
int a = 变量名.nextInt();
double d = 变量名.nextDouble();
String name = 变量名.next();
数值拆分:
package a;
import java.util.Scanner;
/**
* 键盘录入一个三位数,
* 获取其中的个位,十位,百位后,打印在控制台
*/
public class A {
public static void main(String[] args) {
// 创建扫描工具,键盘录入
Scanner sc = new Scanner(System.in);
// 提示语
System.out.println("请输入三位数");
// 接收变量
int num = sc.nextInt();
// 获取个位,十位,百位
// 公式:
// 个位:数字 % 10
// 十位:数字 / 10 % 10
// 百位:数字 / 10 / 10 % 10
int ge = num % 10;
int shi = num / 10 % 10;
int bai = num / 100 % 10;
System.out.println("获取到" + num + "个位的数字是" + ge);
System.out.println("获取到" + num + "十位的数字是" + shi);
System.out.println("获取到" + num + "百位的数字是" + bai);
}
}
示例代码:
System.out.println("欢迎注册信息,请输入您的信息:");
Scanner s = new Scanner(System.in);
//提示语
System.out.println("请输入您的姓名:");
String name = s.next();
System.out.println("请输入您的年龄:");
int age = s.nextInt();
System.out.println("请输入您的身高:");
double height = s.nextDouble();
System.out.println("欢迎您,您的信息如下");
System.out.println(name + "\t" + age + "\t" + height);
package com.qf.demo;
import java.util.Scanner;
public class TestScanner2 {
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// System.out.print("请输入商品的名称");
// String name = sc.next();
// System.out.print("请输入商品的价格");
// double price = sc.nextDouble();
// System.out.print("请输入商品的数量");
// int count = sc.nextInt();
// double zj = (price * count) * 0.9;
// System.out.println(name+"9折后的价格为:" + zj + "元");
// 写法2,简写
Scanner sc = new Scanner(System.in);
System.out.println("请输入商品的名称,单价,数量");
String name = sc.next();
double price = sc.nextDouble();
int count = sc.nextInt();
double zj = price * count * 0.9;
System.out.println(name + "9折后的价格为:" + zj);
}
}
结合if和接收:
//控制台输入
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int a = sc.nextInt();
// 判断是否是偶数
if (a % 2 == 0) {
System.out.println(a + "是偶数");
}
案例:判断年龄是否成年
package com.qf.demo;
/**
* 控制台输入一个年龄
* 判断年龄是否成年
* 输出您已成年
* */
import java.util.Scanner;
public class Testif_2 {
public static void main(String[] args) {
// 创建扫描工具,控制台接收
Scanner sc = new Scanner(System.in);
// 提示语
System.out.println("请输入您的年龄");
// 动态接收
int age = sc.nextInt();
// 用if判断年龄条件
if (age >= 18) {
System.out.println("您已成年");
}
if (age < 18) {
System.out.println("您还差" + (18 - age) + "年成年");
}
}
}
案例:
package com.qf.homework;
import java.util.Scanner;
/**
* 定义三个变量,
* 接收a = 34,b = 56,c = 12
* 按照 12 < 34 < 56 打印
* 思路1:六中列举
* 思路2:a < b < c(交换思想,将最小的赋给a)if else
*/
public class Work1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三个变量");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int temp = 0;
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
System.out.println(a + "<" + b + "<" + c);
// 写法2
// 新定义三个变量,最小值,中间值,最大值
int min, mid, max;
// 找出最大值和最小值
min = (a <= b ? a : b) < c ? (a <= b ? a : b) : c;
max = (a <= b ? b : a) < c ? c : (a <= b ? b : a);
// a.b.c 不为最大或最小即为中间值
if (a == b || a == c || b == c) {
System.out.println("请重新输入三个不同的数字");
} else {
if (a != min && a != max) {
mid = a;
} else if (b != min && b != max) {
mid = b;
} else {
mid = c;
}
// 按顺序输出a<b<c
System.out.println(a + "," + b + "," + c + "三个值从小到大排序:" + min + "<" + mid + "<" + max);
}
// 写法3
int min = 0, mid = 0, max = 0;
if (a > b && a > c) {
max = a;
} else if (a < b && a < c) {
min = a;
} else {
mid = a;
}
if (b > a && b > c) {
max = b;
} else if (b < a && b < c) {
min = b;
} else {
mid = b;
}
if (c > a && c > b) {
max = c;
} else if (c < a && c < b) {
min = c;
} else {
mid = c;
}
System.out.println(min + "<" + mid + "<" + max);
}
}
语法2:(二选一)
if(布尔类型条件) {
满足条件执行的语句;
} else{
不满足条件执行的语句;
}
注意:
else 不加条件
if(age >= 18) {
System.out.println("您已成年");
}else {
System.out.println("您还需" + (18 - age) + "成年");
}
案例:
package com.qf.homework;
import java.util.Scanner;
public class Work5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个数");
int a = sc.nextInt();
int b = sc.nextInt();
if (a % b == 0 || a + b > 1000) {
System.out.println(a);
} else {
System.out.println(b);
}
}
}
案例:判断平年,闰年
package com.qf.demo;
import java.util.Scanner;
/**
* 接收一个年份 判断该年是否是闰年,平年
* 闰年:能被4整除,同时不能被100整除,或者直接整除400
*/
public class Testif_4 {
public static void main(String[] args) {
// 控制台接收
Scanner sc = new Scanner(System.in);
// 提示语
System.out.println("请输入年份");
// 动态接收
int y = sc.nextInt();
// 使用if判断条件
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
System.out.println(y + "是闰年");
} else {
System.out.println(y + "是平年");
}
}
}
案例:控制台接收整数 三个变量,输出他们的最大值(if-else):
package com.qf.demo;
import java.util.Scanner;
/**
* 控制台接收三个变量
* 获得它们的最大值
* 使用 if()else{}
* 比较来完成
*/
public class Testif_5 {
public static void main(String[] args) {
//控制台接收
Scanner sc = new Scanner(System.in);
// 写法1
//动态接收三个变量
// System.out.println("请输入第一个数字");
// int a = sc.nextInt();
// System.out.println("请输入第二个数字");
// int b = sc.nextInt();
// System.out.println("请输入第三个数字");
// int c = sc.nextInt();
// //定义一个max变量表示最大值
// int max = 0;
//使用if判断条件
// if (a >= b && a >= c) {
// max = a;
// }
// if (b >= a && b >= c) {
// max = b;
// }
// if (c >= a && c >= b) {
// max = c;
// }
// System.out.println("三个数字的最大值是:" + max);
// 写法2
System.out.println("请输入三个变量");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int max = 0;
if (a > b) {
max = a;
} else {
max = b;
}
if (c > max) {
max = c;
}
System.out.println("三个变量的最大值为:" + max);
}
}
语法3:(多选一)
if(条件1) {
满足1件执行的语句;
}
else if(条件2){
满足2件执行的语句;
}
else if(条件3){
满足3件执行的语句;
}
...
else{
都不满足条件执行的语句;
}
使用第三种语法格式实现,获得三个变量的最大值:
package com.qf.demo;
import java.util.Scanner;
public class Testif_6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三个变量");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int max = 0;
if (a >= b && a >= c) {
max = a;
} else if (b >= a && b >= c) {
max = b;
} else {
max = c;
}
System.out.println("三个变量的最大值是:" + max);
}
}
案例:考试分数等级 A B C D (不及格)
package com.qf.demo;
import java.util.Scanner;
public class Testif_7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的成绩");
int score = sc.nextInt();
// 写法1
// if (score < 0 || score > 100) {
// System.out.println("成绩非法");
// } else {
// if (score >= 90) {
// System.out.println("A");
// } else if (score >= 80) {
// System.out.println("B");
// } else if (score >= 70) {
// System.out.println("C");
// } else if (score >= 60) {
// System.out.println("D");
// } else {
// System.out.println("不及格");
// }
// }
// 写法2
if (score >= 90 && score <= 100) {
System.out.println("A");
} else if (score >= 80 && score <= 90) {
System.out.println("B");
} else if (score >= 70 && score <= 80) {
System.out.println("C");
} else if (score >= 60 && score <= 70) {
System.out.println("D");
} else if (score >= 0 && score < 60) {
System.out.println("不及格");
} else {
System.out.println("成绩非法");
}
}
}
案例:判断日期情况
package com.qf.demo;
/**
* 星期范围1~7
* 周一~周五 上课
* 周六 自习
* 周日 休息
* */
import java.util.Scanner;
public class Testif_8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入星期");
int day = sc.nextInt();
if (day >= 1 && day <= 5) {
System.out.println("上课");
} else if (day == 6) {
System.out.println("自习");
} else if (day == 7) {
System.out.println("休息");
} else {
System.out.println("非法日期");
}
}
}
案例:判断num 是否能整除5 6
package com.qf.demo;
/**
* 键盘录入一个整数变量num
* 判断num 是否能整除5 6
* 1.只能被5整除 25
* 2.只能被6整除 36
* 3.都能 30
* 4.都不能 13
*
* */
import java.util.Scanner;
public class Testif_9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字");
int num = sc.nextInt();
// if (num % 5 == 0 && num % 6 == 0) {
// System.out.println("能同时被5和6整除");
// } else if (num % 5 == 0) {
// System.out.println("只能被5整除");
// } else if (num % 6 == 0) {
// System.out.println("只能被6整除");
// } else {
// System.out.println("都不能整除");
// }
if (num % 5 == 0 && num % 6 != 0) {
System.out.println("只能被5整除");
} else if (num % 6 == 0 && num % 5 != 0) {
System.out.println("只能被6整除");
} else if (num % 5 == 0 && num % 6 == 0) {
System.out.println("同时整除");
} else {
System.out.println("都不能整除");
}
}
}
案例:判断是什么三角形
package com.qf.homework;
import java.util.Scanner;
public class Work2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三角形的三条边");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
// 判断是否是三角形
if (a + b > c && a + c > b && b + c > a) {
if (a == b && b == c) {
System.out.println("等边三角形");
} else if (a == b || b == c || a == c) {
System.out.println("等腰三角形");
} else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) {
System.out.println("直角三角形");
} else {
System.out.println("普通三角形");
}
System.out.println("三角形的周长=" + (a + b + c));
} else {
System.out.println("无法构成三角形");
}
}
}
案例:账号登录判断
package com.qf.homework;
import java.util.Scanner;
public class Work4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入账户");
int no = 12345;
int pass = 12345;
int yno = sc.nextInt();
if (yno == no) {
System.out.println("账号正确");
System.out.println("请输入密码");
int ypass = sc.nextInt();
if (ypass == pass) {
System.out.println("密码正确,登陆成功");
} else {
System.out.println("密码错误,请重新输入");
}
} else {
System.out.println("账号有误,请重新输入");
}
}
}
4.2.2 switch 结构
语法:
switch(表达式/变量){
case 值1 : {语句1 ; break;}
case 值2 : {语句2 ; break;}
case 值3 : {语句3 ; break;}
default:{
都不匹配执行的语句;
}
}
注意:
- switch()里的值类型要求: byte /short / int / char / String(jdk7+)/ 枚举(jdk5+),不能使用条件
- case后面的值代表和switch里变量匹配的,值要求类型: 和switch()里类型一致,值不能相同
- 匹配哪个值,执行语句;break表示跳出整个switch,可以省略不加,如果没加break,语句依次向下执行,直到遇到break;
- default 没有case的匹配,执行
package com.qf.demo;
import java.util.Scanner;
public class TestSwitch_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入灯的颜色");
String light = sc.next();
switch (light) {
case "red":
System.out.println("红灯停");
break;
case "yellow":
System.out.println("黄灯缓行");
break;
case "green":
System.out.println("绿灯行");
break;
default:
System.out.println("非法灯");
break;
}
}
}
案例:switch 判断薪资等级(money/1000)
package com.qf.demo;
import java.util.Scanner;
public class TestSwitch_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入薪资");
int money = sc.nextInt();
switch (money / 1000) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
System.out.println("A");
break;
case 7:
case 8:
case 9:
System.out.println("B");
break;
case 10:
System.out.println("C");
break;
default:
System.out.println("others");
break;
}
}
}
案例:switch 判断分数等级(score/10)
package com.qf.demo;
import java.util.Scanner;
/**
* 输入成绩score,
* 判断相应的等级(用switch实现)
* score在90-100分为“优秀”
* score在80-89分为“良好”
* score在70-79分为“中等”
* score在60-69分为“及格”
* score在60分以下,为“不及格”
*/
public class TestSwitch4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的分数");
int score = sc.nextInt();
switch (score / 10) {
case 9:
case 10:
System.out.println("优秀");
break;
case 8:
System.out.println("良好");
break;
case 7:
System.out.println("中等");
break;
case 6:
System.out.println("及格");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
System.out.println("不及格");
break;
default:
System.out.println("非法");
break;
}
}
}
案例:控制台接收一个年份和月份,输出该年该月有多少天
1 3 5 7 8 10 12 (31)
4 6 9 11 (30)
2:判断闰年29,平年28
package com.qf.homework;
import java.util.Scanner;
public class Work3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int y = sc.nextInt();
System.out.println("请输入月份");
int m = sc.nextInt();
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
case 2:{
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
System.out.println("29天");
} else {
System.out.println("28天");
}
break;
}
default:
System.out.println("月份不合法");
break;
}
// 写法2
// Scanner sc = new Scanner(System.in);
// System.out.println("请输入年份");
// int y = sc.nextInt();
// System.out.println("请输入月份");
// int m = sc.nextInt();
// if (m > 12 || m < 1) {
// System.out.println("月份不合法");
// } else {
// if (m == 2) {
// if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
// System.out.println("29天");
// } else {
// System.out.println("28天");
// }
// } else if (m == 4 || m == 6 || m == 9 || m == 11) {
// System.out.println("30天");
// } else {
// System.out.println("31天");
// }
// }
}
}
4.3 循环语句
4.3.1 循环的定义
当需要反复让某个代码段运行
4.3.2 循环的分类
for 循环
while 循环
do while 循环
4.3.3 循环的组成部分:
循环的初始值: 循环最初状态
循环的条件: 能够让程序反复运行的条件
循环体: 被反复运行的代码段
迭代: 循环的变更,每次的变化
1 圈 <= 10 跑 3
4.3.4 for循环
语法:
for(初始值 ; 循环条件 ; 迭代){
循环体;
}
循环的示例:
for(int i = 1; i <= 100; i ++) {
System.out.println("侯军长跑第" + i + "圈");
}
打印1-100之间的偶数:
package com.qf.demo;
public class Testfor_2 {
public static void main(String[] args) {
for (int i = 2; i <= 100; i += 2) {
System.out.println("1~100之间的偶数" + i);
}
//写法2
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println("1~100之间的偶数" + i);
}
}
}
}
打印1-50之间敲七数字:7的倍数 或以7结尾
for(int i = 7; i <= 50; i ++) {
if(i % 7 == 0 || i % 10 == 7) {
System.out.println(i);
}
}
1~100之间敲7次数
package com.qf.demo;
/**
* 1~100之间敲7
*/
public class Test_For6 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
// 7的倍数(i % 7 == 0),以7结尾(i % 10 == 7),取十位数字(i / 10 == 7)
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {
count++;
}
}
System.out.println(count);
}
}
和/积问题:
求和问题:计算1-100累加和
//1-100累加和
int sum = 0;
for(int i = 1; i <= 100;i ++) {
sum += i;
}
System.out.println(sum);
1-n的累计乘积 ,n是控制台接收
乘积不能从0开始,要从1开始
//1-n 累计乘
Scanner sc = new Scanner(System.in);
System.out.println("请输入n:");
int n = sc.nextInt();
//最后乘积
long mul = 1;
//从1开始到n结束
for(int i = 1; i <= n;i ++) {
mul = mul * i;
}
System.out.println(mul);
计数问题: 统计个数
统计一下: 1-100之间有几个 3或7的倍数
int count = 0;
for (int i = 1; i <= 100; i++) {
if(i % 3 == 0 || i % 7 == 0) {
count ++;
}
}
System.out.println(count);//43
统计1900-2022年一共多少个闰年?
package com.qf.demo;
public class Testfor_7 {
public static void main(String[] args) {
int count = 0;
for (int i = 1900; i <= 2022; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
count++;
}
}
System.out.println(count);
}
}
双层循环嵌套:
循环的内容循环
//外层
for(int i = 1;i <= 3;i ++){
//内层
for(int j = 1; j <= 3;j ++){
System.out.println(i + "," + j);
}
}
// 1,1 1,2 1,3
// 2,1 2,2 2,3
// 3,1 3,2 3,3
打印:
/**
*****
*****
*****
*/
//行
for (int i = 1; i <= 3; i++) {
//列
for (int j = 1; j <= 5; j++) {
System.out.print("*");
}
System.out.println();
}
打印:
*
**
***
****
*****
for (int i = 1; i <= 5; i++) {
//列
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
*
***
*****
*******
for (int i = 1; i <= 4; i++) {
//列
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
*
***
*****
*******
//行
for (int i = 1; i <= 4; i++) {
//空格
for (int k = 1; k <= 4 - i; k++) {
System.out.print(" ");
}
//星星
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
*******
*****
***
*
4 7 3 5 2 3 1 1
4 0 3 1 2 2 1 3
for (int i = 4; i >= 1; i--) {
for (int k = 1; k <= 4 - i; k++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
案例:打印小九九形状
package com.qf.demo;
/**
* 打印小九九形状1
*/
public class Testxiao99 {
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();
}
}
}
package com.qf.demo;
/**
* 打印小九九形状2
* */
public class Testxiao99_2 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int k = 0; k <= 9 - i; k++) {
System.out.print("\t");
}
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
}
package com.qf.demo;
/**
* 打印小九九形状3
*/
public class Testxiao99_3 {
public static void main(String[] args) {
for (int i = 9; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
}
package com.qf.demo;
/**
* 打印小九九形状4
*/
public class Testxiao99_4 {
public static void main(String[] args) {
for (int i = 9; i >= 1; i--) {
for (int k = 1; k <= 9 - i; k++) {
System.out.print("\t");
}
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
}
周考案例:百元买鸡问题
package com.qf.demo;
/**
* 实现百元买鸡的问题:
* 现在你手头上有100元钱,
* 要用这些钱来买100只鸡
* 公鸡3元1只,母鸡2元1只,小鸡0.5元一只
* 请问应该如何购买才能实现
*/
public class Testfor_13 {
public static void main(String[] args) {
int count = 0;
for (int x = 0; x <= 100; x++) {
for (int y = 0; y <= 100; y++) {
for (int z = 0; z <= 100; z++) {
if (x + y + z == 100 && 3 * x + 2 * y + 0.5 * z == 100) {
System.out.println(x + "," + y + "," + z);
count++;
}
}
}
}
System.out.println("有" + count + "种方式实现购买");
}
}
周考案例:输出1~100之间能被3整除且个位数是6的数字
package com.qf.zk;
/**
* 输出1~100之间能被3整除且个位数是6的数字
*/
public class ZK_2 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 10 == 6) {
System.out.println(i);
}
}
}
}
周考案例:打印闰年,每四个一换行
package com.qf.zk;
/**
* 打印1900~2022年之间的所有的闰年,
* 且每四行一输出
* 如:
* 1900 1904 1908 1912 1901
* xxxx xxxx
*/
public class ZK_3 {
public static void main(String[] args) {
int count = 0;
for (int i = 1900; i <= 2022; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
System.out.print(i + "\t");
count++;
if (count % 4 == 0) {
System.out.println();
}
}
}
}
}
案例:输出10个斐波那契序列 1 1 2 3 5 8 13 21
package com.qf.homework;
/**
* 输出10个斐波那契序列 1 1 2 3 5 8 13 21
* a b 先定义好
* int a = 1,b = 1
*/
public class Work1 {
public static void main(String[] args) {
// 先定义好a和b的初始值
// int a = 1,b = 1;
// 打印a和b的结果
// System.out.print(a + "\t" + b + "\t");
// 利用for循环从1到结束条件<=8
// for (int i = 1; i <= 8; i++) {
// 定义一个c变量存储a+b的和
// int c = a + b;//2 3 5 8 13 21 34
// 交换思想
// a = b;//1 2 3 5 8 13 21
// b = c;//2 3 5 8 13 21 34
// System.out.print(c + "\t");
// }
// 写法2
// 定义数列初始值,a为第0个数,值为1,b为数列第1个数,c做临时储存用
int a = 0, b = 1, c = 0;
// 循环斐波那契数列 并打印10个
for (int i = 1; i <= 10; i++) {
// 原b值暂存 c
c = b;
// 等于前两个数之和
b = a + b;
// 将原b值赋予a(即所有值向后顺推一位)
a = c;
System.out.print(a + " ");
}
//写法3
int a = 1, b = 1;
for (int c = 1; c <= 5; c++) {
System.out.print(" " + a + " " + b);
a = a + b;
b = a + b;
}
}
}
案例:打印1~100之间的完全平方数(4 9 16 25 …)
package com.qf.homework;
/**
* 打印1~100之间的完全平方数(4 16 25 ...)
*/
public class Work2 {
public static void main(String[] args) {
// 使用for循环条件开始是1~100结束
for (int i = 1; i <= 100; i++) {
// 获得完全平方数
int a = i * i;
// 完全平方数满足条件<=100
if (a >= 1 && a <= 100) {
// 打印
System.out.println(a + "\t");
}
}
}
}
package com.qf.homework;
import java.util.Scanner;
/**
* 用户输入任意两个数,输出这两个数的最大公约数和最小公倍数
*
* @author 86136
*
*/
public class Homework7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个数字");
int a = sc.nextInt();
int b = sc.nextInt();
//找因子
//将a和b小的存储到a上
//a = a > b ? b : a;
if(a > b) {
int temp = a;
a = b;
b = temp;
}
//定义最大值变量
int max = 0;
for (int i = 1; i <= a; i++) {
if(a % i == 0 && b % i == 0) {
//System.out.println(i);
//比较谁比max大
if(max < i) {
max = i;
}
}
}
System.out.println("最大公约数:" + max);
// for (int i = a; i >= 1; i--) {
// if(a % i == 0 && b % i == 0) {
// System.out.println(i);
// //跳出循环
// break;
// }
// }
//辗转相除法
//最小公倍数 = a * b / 最大公约数
//System.out.println("最小公倍数:" + a * b / max);
int min = 0;
for (int i = b; i <= a * b; i++) {
if(i % a == 0 && i % b == 0) {
min = i;
break;
}
}
System.out.println("最小公倍数:" + min);
}
}
案例:猴子吃桃
package com.qf.homework;
/**
* 有一只猴子吃桃子,
* 每天吃掉桃子的一半,
* 然后不过瘾,
* 再多吃一个,
* 到第10天,
* 就剩下一个桃子,
* 问第一天是多少个桃子
*/
public class Work3 {
public static void main(String[] args) {
//最后桃子的个数
int num = 1;
//循环条件从第1天开始,到第10天结束
for (int i = 1; i < 10; i++) {
//假设num是3000个
//num - num / 2 -1
//得出以下公式:
num = (num + 1) * 2;
}
System.out.println(num);
}
}
案例:李先生和夫人的年龄
package com.qf.homework;
/**
* 李先生岁数的平方与他的夫人的岁数之和是1053,
* 而他的夫人的岁数的平方与他的岁数之和是873,
* 请编写程序计算李先生及其夫人的岁数各是多少
*/
public class Work4 {
public static void main(String[] args) {
// 使用for循环假设条件是从1开始到100岁结束
// i代表丈夫的年龄
for (int i = 1; i <= 100; i++) {
// j代表夫人的年龄
for (int j = 1; j <= 100; j++) {
// 丈夫年龄的平方加上j等于1053,同时夫人年龄的平方加上i等于873
if (i * i + j == 1053 && j * j + i == 873) {
// 打印
System.out.println("丈夫的年龄" + i + "," + "夫人的年龄" + j);
}
}
}
}
}
案例:最大公约数和最小公倍数
package com.qf.homework;
import java.util.Scanner;
/**
* 用户输入任意两个数,
* 输出这两个数的最大公约数和最小公倍数.
* A b 4 6
* 最大公约数:1 2 4 1 2 3 6
*/
public class Work5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个数字");
int a = sc.nextInt();
int b = sc.nextInt();
// 将a,b中最小值存储到a
if (a > b) {
//交换思想
int temp = a;
a = b;
b = temp;
}
// 找因子
// int max = 0;
// for (int i = 1; i <= a; i++) {
// if (a % i == 0 && b % i == 0) {
// max = i;
//
// }
// }
int max = 0;
for (int i = a; i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
max = i;
break;
}
}
System.out.println("最大公约数:" + max);
int min = 0;
for (int i = b; i <= a * b; i++) {
if (i % a == 0 && i % b == 0) {
min = i;
break;
}
}
System.out.println("最小公倍数:" + min);
// 最小公倍数:辗转相除 a * b / max
// System.out.println("最小公倍数:" + a * b / max);
}
}
案例:水仙花数
package com.qf.homework;
/**
* 打印1~1000之间的水仙花数
* (1*1*1+5*5*5+3*3*3==153)
*/
public class Work7 {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
// 个位
int a = i % 10;
// 十位
int b = i / 10 % 10;
// 百位
int c = i / 100;
// a的立方加b的立方加c的立方等于i
if (a * a * a + b * b * b + c * c * c == i) {
// 打印水仙花数
System.out.println(i);
}
}
}
}
总结for的执行顺序:
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
- 循环初始值 int i = 1;
- 判断条件
- 满足条件执行循环体,不满足执行跳出循环
- 迭代
- 继续判断条件
- 返回第三步
案例:打印1-1000之间的完数(6 = 1 + 2 + 3 )
for (int i = 1; i <= 1000; i++) {
//1.找因子
int sum = 0;
for (int j = 1; j < i; j++) {
if(i % j == 0) {
//2.求和
sum += j;
}
}
//3.判断是否相等
if(sum == i) {
System.out.println(i);
}
}
案例:打印10-100之间的素数(质数,只有两个)
写法1: 统计因子的个数是否是2个
//素数 两个因子 2 3 5 7 11 12
for (int i = 10; i <= 100; i++) {
//找因子个数
int count = 0;
for (int j = 2; j < i; j++) {
if(i % j == 0) {
//个数+1
count ++;
}
}
//判断是否相等
if(count == 0) {
System.out.println(i);
}
}
写法2: 去掉1和本身,是否还存在其他因子
//素数 两个因子 2 3 5 7 11 12
for (int i = 10; i <= 100; i++) {
//标记
boolean boo = true;
for (int j = 2; j < i; j++) {
if(i % j == 0) {
//还有其他因子,不是素数
//修改标记
boo = false;
//a ++;
break;
}
}
// boo == true
if(boo) {
System.out.println(i);
}
}
案例:万年历综合练习
public class MyCalendar {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
int year = input.nextInt();
System.out.print("请输入月份:");
int month = input.nextInt();
// 总天数差
int sum = 0;
// 1900.1.1 - year.1.1
for (int i = 1900; i < year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
sum += 366;
} else {
sum += 365;
}
}
// int count = 0;
// for (int i = 1900; i < year; i++) {
// if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
// count ++;
// }
// }
// sum = (year - 1900) * 365 + count;
// year.1 ~ year .5
for (int i = 1; i < month; i++) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
sum += 31;
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
sum += 30;
} else if (i == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum += 29;
} else {
sum += 28;
}
} else {
System.out.println("月份不正确");
}
// switch (i) {
// case 1:
// case 3:
// case 5:
// case 7:
// case 8:
// case 10:
// case 12:
// sum += 31;
// break;
// case 4:
// case 6:
// case 9:
// case 11:
// sum += 30;
// break;
// case 2:
// if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
// sum += 29;
// } else {
// sum += 28;
// }
// break;
// default: {
// System.out.println("你输入的月份有误!");
// }
// }
}
int week = (sum + 1) % 7;
System.out.println("************************万年历*************************");
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
// 打空格
//int count = 0;
for (int i = 1; i <= week; i++) {
System.out.print("\t");
//count++;
}
int day = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
day = 29;
} else {
day = 28;
}
break;
default:
System.out.println("你输入的月份有误!");
}
// 打印的截至
for (int i = 1; i <= day; i++) {
System.out.print(i + "\t");
//count++;
week++;
if (week % 7 == 0) {
System.out.println();
}
}
System.out.println();
System.out.println("******************************************************");
}
}
4.3.5 while循环
语法:
初始值;
while(循环条件){
循环体;
迭代;
}
案例:求和1~100
//求和 1-100和
int sum = 0;
int a = 1;
while(a <= 100) {
sum += a;
a ++;
}
System.out.println(sum);
案例:打印1~100的奇数和
package com.qf.demo;
public class TestWhile2 {
public static void main(String[] args) {
//定义求和变量
int sum = 0;
//初始值
int i = 1;
//循环条件
while (i <= 100) {
//判断条件
if (i % 2 != 0) {
sum += i;
}
i++;
}
System.out.println(sum);
}
}
案例:求商和余数
package com.qf.homework;
/**
* 给定两个整数
* 被除数和除数(都是正数,且不超过int的范围)
* 将两数相除,
* 要求:
* 不使用乘法,除法和 % 运算符。
* 得到商和余数
*/
public class Work8 {
public static void main(String[] args) {
// 定义变量记录被除数
int bcs = 100;
// 定义变量记录除数
int cs = 37;
// 定义变量用来统计相减了多少次
int count = 0;
// 使用while循环,在循环中,不断的用被除数-除数
// 只要被除数 是大于等于 除数的,那么就一直循环
while (bcs >= cs) {
bcs = bcs - cs;
// 只要减一次,那么统计变量就自增一次
count++;
}
// 当循环结束bcs变量记录的就是余数
System.out.println("余数为:" + bcs);
// 当循环结束count变量记录的就是商
System.out.println("商为;" + count);
}
}
案例:判断回文数
package com.qf.homework;
/**
* 定义一个x变量
* int x = 12345;
* 判断x是不是一个回文数
* */
public class Work7 {
public static void main(String[] args) {
int x = 12345;
int temp = x;
int num = 0;
while (x != 0) {
int ge = x % 10;
x = x / 10;
num = num * 10 + ge;
}
System.out.println(num == temp ? "是回文数" : "不是回文数");
}
}
注意:
for循环适用于循环次数固定
while循环适用于循环条件外部决定,客户需求决定。
System.out.println("----欢迎进入消消乐游戏大厅-----");
System.out.println("是否开始? 0开始,任意键退出");
Scanner sc = new Scanner(System.in);
//初始值
int opr = sc.nextInt();
while(opr == 0) {
//循环体
System.out.println("游戏开始进行......");
System.out.println("Amazing.......");
System.out.println("游戏结束,是否继续,0继续,任意键退出");
//迭代
opr = sc.nextInt();
}
System.out.println("bye-下次继续");
练习:控制台接收学生成绩: 直到接收的成绩是-1 终止接收,获得学生的成绩的总分,最高分和平均分
package com.qf.project07;
import java.util.Scanner;
public class TestWhile4 {
public static void main(String[] args) {
System.out.println("----系统录入学生成绩-----");
System.out.println("请输入第1个学生成绩");
Scanner sc = new Scanner(System.in);
//初始值
double sum = 0;
int count = 0;
int max = 0;
int score = sc.nextInt();
while(score >= 0) {
//循环体
if(score > max) {
max = score;
}
sum += score;
count ++;
//迭代
System.out.println("请输入第" + (count+1)+ "个学生成绩");
score = sc.nextInt();
}
System.out.println("总分数:" + sum);
System.out.println("最高分:" + max);
System.out.println("平均分:" + (sum / count));
}
}
while循环通过死循环制作菜单程序(ATM操作):
package com.qf.project07;
import java.util.Scanner;
public class TestWhile5 {
public static void main(String[] args) {
System.out.println("欢迎进入ATM");
//账户余额
double balance = 5000;
while(true) {
System.out.println("1.存钱");
System.out.println("2.取钱");
System.out.println("3.查看余额");
System.out.println("4.转账");
System.out.println("5.退出");
Scanner sc = new Scanner(System.in);
System.out.println("请输入操作编号");
int opr = sc.nextInt();
if(opr == 1) {
System.out.println("请输入存款金额");
//整百> 0
double money = sc.nextDouble();
if(money % 100 == 0 && money > 0) {
balance += money;
}else {
System.out.println("金额有误");
}
}else if(opr == 2) {
System.out.println("请输入取款金额");
//判断
double money = sc.nextDouble();
if(money > 0 && money <= balance) {
balance -= money;
}else {
System.out.println("余额不足");
}
}else if(opr == 3) {
System.out.println("卡内余额:" + balance);
}else if(opr == 4) {
System.out.println("暂未开启功能");
}else {
//退出死循环
break;
}
}
System.out.println("谢谢使用");
}
}
总结:死循环:
while(true){
}
for(;;){
}
4.3.6 do-while循环
语法:
初始值;
do{
循环体;
迭代;
}while(循环条件);
案例:获得1~100的偶数和
public class TestDoWhile {
public static void main(String[] args) {
int i = 1 , sum = 0;
do {
if(i % 2 == 0) {
sum += i;
}
//迭代
i ++;
}while(i <= 100);
System.out.println(sum);
}
}
注意:
先执行循环体,后判断条件,即使循环条件不满足,也会执行
do{ }中的变量不可以使用在while中使用
三种循环的总结:
- 当循环条件中次数固定,首选for , 执行次数: 0 次或n次
- 当循环次数外部决定:
- while 先判断条件后循环 ,执行次数: 0 次或n次
- do-while 先执行循环体,后判断条件, 执行次数:1 次或n次
三种循环对比:
int i = 1 , sum = 0;
for(i = 2; i > 100;i ++) {
sum += i;
}
// while(i > 100) {
// sum += i;
// i ++;
// }
// do {
// sum += i;
// //迭代
// i ++;
// }while(i > 100);
System.out.println(sum + "," + i);
商品的支付:
public class TestDoWhile3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入商品名称:");
String name = sc.next();
System.out.println("请输入商品价格:");
double price = sc.nextDouble();
System.out.println("请输入商品数量:");
int num = sc.nextInt();
//总额 = 单价*数量*折扣
double sum = price * num * 0.9;
System.out.println(name + "总计花费:" + sum);
//支付
double money = 0;
do {
System.out.println("还请支付:" + (sum - money) + "元");
money += sc.nextDouble();
}while(money < sum);
//退出找零
System.out.println("支付成功,找零" + (money - sum ));
}
}
4.3.7 循环的跳出
在循环执行当中,因为某些需要,跳出循环。
语句 | 含义 |
---|---|
continue | 略过当前循环,执行下一个 |
break | 退出当前一层循环 |
return | 退出方法 |
System.exit(0) | 退出虚拟机 |
break 标签; | 标签加在某个循环的上方,从该循环中跳出,向下跳 |
continue 标签 |
for (int i = 1; i <= 10; i++) {
if(i == 5) {
//continue;
//break;
//return;
//System.exit(0);
}
System.out.println(i);
}
System.out.println("循环出来了");
注意:
break只能退出一层循环
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if(i == 2) {
break;
}
System.out.println(i + "," + j);
}
}
p:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if(i == 2) {
//break;
//return;
break p;
}
System.out.println(i + "," + j);
}
}
System.out.println("是否可以运行");
购买逻辑:
double sum = 0;
do {
System.out.println("请输入商品编号:");
int pno = sc.nextInt();
System.out.println("请输入购买数量:");
int count = sc.nextInt();
if(pno == 1) {
//name1
sum += price1 * count;
System.out.println(name1 + "\t" + price1 + "\t" + price1 * count);
}else if(pno == 2) {
sum += price2 * count;
//name2
System.out.println(name2 + "\t" + price2 + "\t" + price2 * count);
}
else if(pno == 3) {
sum += price3 * count;
//name3
System.out.println(name3 + "\t" + price3 + "\t" + price3 * count);
}
else {
System.out.println("马上进货");
}
//提示是否继续
System.out.println("是否继续? y/任意键");
}while(sc.next().equals("y"));
第五章 数组
5.1 数组的定义
存储相同数据类型的一组数据集;相同数据类型(基本数据类型+引用数据类型),第一个引用数据类型。
5.2 数组的作用
方便管理和维护一组数据。
5.3 数组的内存
- 数组是连续的存储空间
- 数组有下标,从0开始
- 基本数据类型临时变量,存储栈中,引用数据类型的数组存储在堆中
内存的区域: 栈(临时变量), 堆 (引用类型),常量池 , 方法区 , 静态区(static)
5.4 数组的声明和创建(重点)
1.声明语法:
数据类型 [] 数组名 ;
数据类型 数组名 [];
注意:
数据类型既可以是基本类型 int,double;也可以是引用类型 String
数组名定义方式同变量名
2.创建语法:(3种)
第一种:
数组名 = new 数据类型[数组大小];
注意:
数组创建使用关键字 : new 开辟新空间(堆)
数组大小必须给定,决定数组空间大小
声明和创建的类型一致
int[] a = new int[4];
注意:
a是数组堆中地址(引用)
a[下标] 获取对应的元素
数组的元素有默认值: int 0 double 0.0 boolean false char
‘\u0000’ 引用类型 null数组的长度(个数/大小): 数组名.length
数组下标范围: 0 ~ 数组.length - 1
//创建一个四个元素的数组
int [] a = new int[4];
//a存储的是数组的堆中地址
System.out.println(a);
//通过下标获取元素
System.out.println(a[0]);
//打印数组的个数
System.out.println("数组个数:" + a.length);
使用数组元素
数组名[下标] = 值; //赋值
System.out.print(数组名[下标]); //获取元素
System.out.print(a[0]); //获取第一个元素
System.out.print(a[a.length - 1]); //获取最后一个元素
数组的元素遍历(数组的迭代)
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
增强for循环(JDK5+)
for(数据类型 迭代变量 : 数组名 ){
打印迭代变量
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println("\n---------------------------------------------\n");
for(int m : a) {
System.out.print(m + "\t");
}
第二种:
数据类型 [] 数组名 = {元素1,元素2,元素3....};
注意:一次性创建且赋值
- 不需要给大小
- 声明和创建不能分开写
案例:定义数组并求和
public class TestArray5 {
public static void main(String[] args) {
int [] a = {1,2,3,4};
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
System.out.println(sum);
}
}
第三种创建语法:
数据类型 [] 数组名 ;
数组名 = new 数据类型[]{元素1,元素2,元素3....};
数组名 = new 数据类型[]{元素1,元素2};
注意:
[] 不要给大小,给就错误
声明和创建可以分开写
总结:
- 数组大小已知,元素未知,第一种
- 数组元素固定,不改变 第二种
- 数组元素已经给定,改变 第三种
5.5 数组的高级应用
动态给数组赋值
数组求和
数组平均值
最大值最小值
计数和去重
排序
查找
复制合并
5.5.1 动态赋值
int [] score = new int[5];
for (int i = 0; i < score.length; i++) {
score[i] = sc.nextInt();
}
5.5.2 求和 平均值
double sum = 0;
for (int i = 0; i < score.length; i++) {
score[i] = sc.nextInt();
sum += score[i];
}
System.out.println("总分:" + sum);
System.out.println("平均分:" + sum / score.length);
5.5.3 最大值和最小值
public class TestUse2 {
public static void main(String[] args) {
System.out.println("请输入五个学生成绩:");
Scanner sc = new Scanner(System.in);
int[] score = new int[5];
int max = 0, min = 999 , maxIndex = -1, minIndex = -1;
for (int i = 0; i < score.length; i++) {
score[i] = sc.nextInt();
//每个人的分数和最高的比较,比max高,刷出最高分
if(score[i] > max) {
max = score[i];
maxIndex = i;
}
if(score[i] < min) {
min = score[i];
minIndex = i;
}
}
System.out.println(max + "," + min);
System.out.println(maxIndex + 1 + "," + (minIndex + 1));
}
}
最高分重复问题:
for (int i = 0; i < score.length; i++) {
if(max == score[i]) {
System.out.println("最高分:" + (i + 1));
}
}
综合练习(五个评委打分,0分淘汰,去掉最高最低分求平均值)
package com.qf.homework;
import java.util.Scanner;
public class Remark {
public static void main(String[] args) {
//定义五个元素的数组
int [] a = new int[5];
Scanner sc = new Scanner(System.in);
double sum = 0;
int max = 0, min = 9999;
//
boolean flag = true;
for (int i = 0; i < a.length; i++) {
//动态赋值
System.out.println("请第" + (i + 1) + "个评委打分:");
a[i] = sc.nextInt();
if(a[i] == 0) {
System.out.println("选手,您被淘汰了");
flag = false;
continue;
}
sum += a[i];
if(max < a[i]) {
max = a[i];
}
if(min > a[i]) {
min = a[i];
}
}
System.out.println("去掉最高分:" + max);
System.out.println("去掉最低分:" + min);
if(flag) {
System.out.println("参数成绩:" + (sum - max - min ) / (a.length - 2));
}else {
System.out.println("参数成绩:" + 0.0);
}
}
}
5.5.4 计数问题
数组内部统计个数
统计不及格的学生人数:
public class TestCount {
public static void main(String[] args) {
int [] score = new int[5];
System.out.println("请输入五个学生成绩");
Scanner sc = new Scanner(System.in);
//计数变量
int count = 0;
for (int i = 0; i < score.length; i++) {
score[i] = sc.nextInt();
if(score[i] < 60) {
count++;
}
}
System.out.println("不及格人数:" + count);
}
}
数组将0元素去除:
public class TestCount2 {
public static void main(String[] args) {
int [] b = {12,45,66,0,12,7,34,0,4};
//0元素去除
int count = 0;
for (int i = 0; i < b.length; i++) {
if(b[i] == 0) {
count ++;
}
}
int [] c = new int[b.length - count];
//控制c数组的下标
int index = 0;
for (int i = 0; i < b.length; i++) {
if(b[i] != 0) {
c[index++] = b[i];
//index ++;
}
}
System.out.println(Arrays.toString(c));
}
}
获得元素出现次数:
public class TestCount3 {
public static void main(String[] args) {
int [] b = {12,45,66,34,12,7,34,12,45,45,78,45};
int max = 0,index = 0;
//获得出现次数最多的元素以及对应的次数
for (int i = 0; i < b.length; i++) {
int count = 1;
for (int j = i + 1; j < b.length; j++) {
if(b[i] == b[j]) {
count ++;
}
}
if(max < count) {
max = count;
index = i;
}
}
System.out.println(b[index] + "," + max);
}
}
5.5.5 数组排序
排序方式:(冒泡排序, 选择排序,插入排序,归并排序,堆排序…)
冒泡排序:(下沉法)
将数组元素挨着的两两比较,交换,大的下沉,小的上浮,直到最大的沉到最下面,完成一次冒泡,后续将剩余元素按照此原来依次完成。
n个数字 冒 n-1泡 4 3 2 1
第一次冒泡: 3次
第二次冒泡: 2次
第三次冒泡: 1次
i ~ a.length - i
int [] a = {12,45,66,34,12,7,34,12,45,45,78,45};
//冒泡的次数
for (int i = 1; i < a.length; i++) {
//每次冒泡中比较和交换
for (int j = 0; j < a.length - i; j++) {
if(a[j] > a[j + 1]) {
//交换
int c = a[j];
a[j] = a[j + 1];
a[j + 1] = c;
}
}
}
System.out.println(Arrays.toString(a));
选择排序:
将数组的某个位置上的元素与其他位置元素比较,进行交换,a[0]和 a[1],a[2]a[3] ,每次确定一个位置。
public class TestJh {
public static void main(String[] args) {
int [] b = {12,45,66,34,12,7,34,12,45,45,78,45};
//控制每个固定位置
for (int i = 0; i < b.length - 1; i++) {
//拿其他位置和固定的位置比较
for (int j = i + 1; j < b.length; j++) {
if(b[i] > b[j]) {
int c = b[i];
b[i] = b[j];
b[j] = c;
}
}
}
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + "\t");
}
}
}
JAVA中数组的API(application programming interface )
Arrays java.util.Arrays JAVA 工具包
专门处理数组: 数组的帮助类,该类中定义很多操作数组的方法: 排序/ 查找/输出数组/交换元素…
排序方法: sort()
Arrays.sort(数组名); // 自动升序排列
打印方法: toString()
Arrays.toString(数组名); //打印数组成串
5.5.6 数组元素查找
线性查找,二分查找(折半查找)
线性查找: 将目标元素和数组中每一个元素依次比较,当发现有相同,查找到,当循环全部执行结束后,依然没有找到,则没有。
public class TestFind {
public static void main(String[] args) {
int [] a = {12,45,66,34,12,7,34,12,45,45,78,45};
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查找的元素:");
int target = sc.nextInt();
boolean flag = true;
for (int i = 0; i < a.length; i++) {
if(target == a[i]) {
System.out.println("找到该元素");
flag = false;
break;
}
}
if(flag) {
System.out.println("没有该元素");
}
}
}
二分查找(折半查找):(了解)
先保证数组先排序,取中间位置上元素和目标比较,目标>中间元素, 中间位置成为查找开始位置, 目标<中间元素, 中间位置成为查找结束位置,目标就是中间位置,直接找到。
public class TestFind2 {
public static void main(String[] args) {
int [] a = {12,45,66,34,12,7,34,12,45,45,78,45};
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查找的元素:");
int target = sc.nextInt();
boolean flag = true;
int s = 0, e = a.length - 1, m;
while(s <= e) {
m = (s + e) / 2;
if(a[m] == target) {
System.out.println("找到了");
flag = false;
break;
}else if(a[m] < target) {
s = m + 1;
}else {
e = m - 1;
}
}
if(flag) {
System.out.println("没有该元素");
}
}
}
二分查找方法: binarySearch(a, target);
int index = Arrays.binarySearch(a, target);
//a 数组名
//target 查找的目标
//index 查找的下标 >= 0
查找API
public class TestFind3 {
public static void main(String[] args) {
int [] a = {12,45,66,34,12,7,34,12,45,45,78,45};
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查找的元素:");
int target = sc.nextInt();
Arrays.sort(a);
//第一个参数数组 第二个参数是目标
int index = Arrays.binarySearch(a, target);
if(index >= 0) {
System.out.println("包含");
}else {
System.out.println("不包含");
}
}
}
5.5.7 数组去重复
//公式 [a,b] (int)((b - a + 1) * Math.random() + a)
int blue = (int)((16) * Math.random() + 1);
int [] reds = new int[6];
for (int i = 0; i < reds.length; i++) {
reds[i] = (int)((33) * Math.random() + 1);
//判断
for (int j = 0; j < i ; j++) {
if(reds[i] == reds[j]) {
//当前生成的i去掉
i --;
break;
}
}
}
System.out.println(Arrays.toString(reds));
5.5.8 数组的大小和赋值
数组的大小:
数组名.length , 数组的大小在定义数组后是固定的。
int [] a = {1,2,3,4};
a = new int[] {1,2,3,4,5};
数组的赋值(不同于基本类型)
int [] a = {1,2,3,4};
int [] b = a;// a把地址赋值给b
//b修改元素
b[0] = 88;
//a发生改变
System.out.println(Arrays.toString(a));// 88 2 3 4
数组的合并: 将两个数组合并为一个数组
int[] a = {1,2,3,4};
int[] b = {3,6,56,12,8};
int [] c = new int[a.length + b.length];
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
//存b
for (int i = 0; i < b.length; i++) {
c[a.length + i] = b[i];
}
System.out.println(Arrays.toString(c));
API:
System.arraycopy(原数组, 原数组开始下标, 目标数组, 目标开始下标, 复制个数);
public class TestArray4 {
public static void main(String[] args) {
int[] a = {1,2,3,4};
int[] b = {3,6,56,12,8};
//创建新数组
int [] c = new int[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
System.out.println(Arrays.toString(c));
}
}
5.5.9 二维数组(了解)
二维数组,数组的元素是一个个一维数组。
声明语法:
数据类型[][] 数组名; // 推荐使用
数据类型 数组名[][] ;
数据类型[]数组名[] ;
创建语法:
数组名 = new 数据类型[必须给长度][];
a = new int[4][3];// 有四个一维数组,每个数组有3个元素
数组名 = {{1,2},{4,6,7},{1} };
数组元素的获取
b //二维数组的地址
b[0] //一维数组的地址
b[0][0] //元素
b.length // 查看二维数组个数
二维数组的遍历:
int [] b[] = {{1,2,6},{8,3,5},{4,7,9}};
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
System.out.print(b[i][j] + "\t");
}
System.out.println();
}