Day 2
变量
-
变量是什么:就是可以变化的量!
-
Java是一种强类型语言,每个变量都必须声明其类型。
-
Java变量是程序中最基本的存储单元,其要素包括变量名,类型和作用域。
注意事项
- 每个变量都有类型,类型可以是基本类型,也可以是引用类型。
- 变量名必须是合法的标识符。
- 变量声明是一条完整的语句,因此每一个声明都必须以分号结束
public class Demo03 {
//类变量 static
static double salary = 2500;
//属性,变量
//实例变量,从属于对象,如果不自行初始化,这个类型的默认值 0或0.0
//布尔值:默认是false
//除了基本类型,其余的默认值都是null
String name;
int age;
//main 方法
public static void main(String[] args) {
//局部变量,必须声明和初始化值
int i = 10;
System.out.println(i);
Demo03 demo03 = new Demo03();
System.out.println(demo03.age);
System.out.println(demo03.name);
System.out.println(salary);
}
}
输出结果:
10
0
null(也就是0.0)
2500.0
一般用static 定义类变量 可以方便调用
常量
- 常量(Constant):初始化(initialize)后不能再改变值!不会变动的值。
- 所谓常量可以理解成一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变。
final double PI = 3.14;
// final 常量名=值
- 常量名一般使用大写字符
public class Demo04 {
//final 和static 都是修饰符 不存在先后顺序
static final double PI= 3.14;
public static void main(String[] args) {
System.out.println(PI);
// final double PI = 3.14;
// final 常量名=值
}
}
变量的命名规范
- 所有变量、方法、类名:见名知意
- 类成员变量:首字母小写和驼峰原则: monthSalary除了第一个单词以外,后面的单词首字母大写 lastName
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线:MAX_VALUE
- 类名:首字母大写和驼峰原则: Man, GoodMan
- 方法名:首字母小写和驼峰原则: run(). runRun()
运算符
- 算术运算符:+, -, *,,/, %(取余),++, –
- 赋值运算符=
- 关系运算符:>,<,>=,<=,==,!= (不等于),instanceof
- 逻辑运算符: &&(与),||(或),!(非)
- 位运算符: &,|,^,~,>>,<<,>>>(了解就行!! ! )
- 条件运算符?︰
- 扩展赋值运算符:+=,-=,*=,/=
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//Ctrl +D : 复制到下一行
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println((double) a/(double)b);
}
}
输出结果
30
-10
200
0.5
(注意小数需要强转基本类型)
算数运算符
package operator;
public class Demo2 {
public static void main(String[] args) {
long a = 123123123123123L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//long
System.out.println(b+c+d);//int
System.out.println(c+d);//int
}
}
结果:
123123123123123264
141
18
如果运算中有long,那么这个运算结果会以long的类型结束,如有double那么结果一定是double类型,否则不管有没有int结果都是int类型。
关系运算符
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回结果:正确,错误 布尔值
int a = 10;
int b = 20;
System.out.println(a<b);
System.out.println(a>b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
结果:
true
false
false
true
package operator;
public class Demo04 {
public static void main(String[] args) {
//++自增,--自减 一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增
System.out.println(a);
int c = ++a;//执行完这行代码前,先自增,再赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算,2^3 2*2*2
double pow = Math.pow(2, 3);//Math 这个类主要是数学运算
System.out.println(pow);
}
}
输出结果:
4
5
3
5
8.0
逻辑运算符
package operator;
//逻辑运算符(与或非)
public class Demo05 {
public static void main(String[] args) {
//与(and)或(or)非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a || b:"+(a||b));//逻辑或运算:两个变量有一个为真,则结果才为true
System.out.println("!(a && b:)"+!(a&&b));//逻辑非运算:如果是真变为假,如果是假变为真
//短路运算(与运算 如果先算到a为假那么不进行b的判断直接算出结果为假)
int c = 5;
boolean d = (c<4)&&(c++<4);//输出c的结果还是5 证明没有进行c++的运算
System.out.println(d);
System.out.println(c);
boolean f = (c++<4)&&(c--<4);//输出c的结果是6 证明有进行c++的运算 但没有进行c--运算
System.out.println(f);
System.out.println(c);
}
}
结果:
a && b:false
a || b:true
!(a && b:)true
false
5
false
6
位运算符
package operator;public class Demo06 { public static void main(String[] args) { /* A = 0011 1100 B = 0000 1101 ----------------------- A&B = 0000 1100 与 A|B = 0011 1101 或 A^B = 0011 0001 异或运算(相同为0不相同为1) A~B = 1111 0011 非 << *2 >> /2 效率极高 0000 0000 0 0000 0001 1 0000 0010 2 0000 0011 3 0000 0100 4 0000 1000 8 0001 0000 16 <<代表向左移几位 相当于*2 >>代表向右移几位 相当于/2 */ System.out.println(2<<3); }}
结果:
16
扩展赋值运算符
package operator;public class Demo07 { public static void main(String[] args) { int a = 10; int b = 20; a+=b; System.out.println(a);//相当于a+b a-=b;//a-b System.out.println(a); //字符连接符 + ,String System.out.println(""+a+b);//一开始是字符串形式后边都以字符串形式相加 System.out.println(a+b+"");//一开始是一int形式后边全以int形式 }}
结果
30
10
1020
30
三元运算符
package operator;//三元运算符public class Demo08 { public static void main(String[] args) { //x ? y : z //如果x==true,则结果为y,否则为z int score = 80; String type = score <60 ? "不及格":"及格";//必须掌握 System.out.println(type); }}
结果:
及格
优先级:( )
一般来说一元运算符高于二元运算符 比如a++ >a + b
但是加上( )可以使优先级提升 一般需要加( )明确条理
包机制
- 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间。
一般利用公司域名倒置作为包名;
为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。使用"import"语句可完成此功能。
import com.liu.base.*;
JavaDos
javadoc命令是用来生成自己API文档
JDK帮助文档:
https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
参数信息
- @author作者名
- @version版本号
- @since指明需要最早使用的jdk版本
- @param 参数名
- @return返回值情况
- @throws异常抛出情况
package com.liu.base;/** * @author liuke * @version 1.0 * @since 1.8 */public class Doc { String name; /** * * @param name * @return * @throws Exception */ public String test(String name) throws Exception{ return name; }}
在文件内
cmd 输出javadoc 参数 文件名
可以产生一个doc文档的网页
Scanner
-
之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
-
基本语法:
Scanner s = new Scanner(System.in); -
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext() 与hasNextLine()判断是否还有输入的数据。
package com.liu.scanner;import java.util.Scanner;public class Demo01 { public static void main(String[] args) { //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in);//in代表输入 System.out.println("使用next方式接受:"); //判断用户有没有输入字符串 if (scanner.hasNext()){ //使用next方式接收 String str = scanner.next(); System.out.println("输入的内容为:"+str); } scanner.close();//凡是属于IO流的类如果不关闭会一直占用资源.要养成好习惯用完就关掉 }}
使用next方式接受:
10
输入的内容为:10
Process finished with exit code 0
package com.liu.scanner;import java.util.Scanner;public class Demo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine方式接收" ); if (scanner.hasNextLine()){ String str = scanner.nextLine(); System.out.println("输出的内容:"+str); } scanner.close(); }}
使用nextLine方式接收
123 123
输出的内容:123 123
Process finished with exit code 0
next():
1、一定要读取到有效字符后才可以结束输入。
2、对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
4、next()不能得到带有空格的字符串。
nextLine():
1、以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空白。
Scanner进阶使用
package com.liu.scanner;import java.util.Scanner;public class Demo04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //从键盘接收数据 int i = 0; float f = 0.0f; System.out.println("请输入整数"); if (scanner.hasNextInt()) { i = scanner.nextInt(); System.out.println("整数数据"+i); } else { System.out.println("输入的不是整数数据"); } System.out.println("请输入小数"); if (scanner.hasNextFloat()) { f = scanner.nextFloat(); System.out.println("小数数据"+f); } else { System.out.println("输入的不是小数数据"); } }}
正确结果:
请输入整数
10
整数数据10
请输入小数
1.1
小数数据1.1
Process finished with exit code 0
错误结果:
请输入整数
10.1
输入的不是整数数据
请输入小数
小数数据10.1
Process finished with exit code 0
我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果:
package com.liu.scanner;import java.util.Scanner;public class Demo05 { public static void main(String[] args) { //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果: Scanner scanner = new Scanner(System.in); //和 double sum = 0; //计算输入了多少数字 int m = 0; //通过循环判断是否还有输入,并在里面对每一次进行求和和统计 while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); // m = m + 1 ;//m++ sum = sum + x; System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum); } System.out.println(m+"个数和为"+ sum); System.out.println(m + "个数的平均值是" + (sum/m)); scanner.close(); }}
10
你输入了第1个数据,然后当前结果sum=10.0
123
你输入了第2个数据,然后当前结果sum=133.0
158
你输入了第3个数据,然后当前结果sum=291.0
1525
你输入了第4个数据,然后当前结果sum=1816.0
s
4个数和为1816.0
4个数的平均值是454.0
Process finished with exit code 0
if选择结构
if单选择结构
我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句}
package com.liu.struct;import java.util.Scanner;public class ifDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容"); String s = scanner.nextLine(); //equals判断是否相等 if (s.equals("hello")){ System.out.println(s); } System.out.println("end"); scanner.close(); }}
请输入内容
hello
hello
end
Process finished with exit code 0
if双选择结构
语法:
if(布尔表达式){
//如果布尔表达式的值为
true}else{
/如果布尔表达式的值为false
}
package com.liu.struct;import java.util.Scanner;public class ifDemo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩"); int score = scanner.nextInt(); if (score>60){ System.out.println("成绩合格"); }else{ System.out.println("成绩不合格"); } scanner.close(); }}
请输入成绩
502
成绩合格
Process finished with exit code 0
if多选择结构
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}else if(布尔表达式3){
/如果布尔表达式3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
package com.liu.struct;import java.util.Scanner;public class ifDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩"); int score = scanner.nextInt(); if (score == 100){ System.out.println("恭喜满分"); }else 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("成绩不合法"); } }}
请输入成绩
120
成绩不合法
Process finished with exit code 0
if语句至多有1 个else语句,eLse 语句在所有的else if 语句之后。
if语句可以有若干个else if语句,它们必须在else语句之前。
一但其中一个 else if语句检测为 true,其他的else if以及 else语句都将跳过执行。
嵌套if结构
-
使用嵌套的if…else语句是合法的。也就是说你可以在另一个if或者else if语句中使用if或者else if 语句。你可以像if 语句一样嵌套else if…else。
-
语法:
if(布尔表达式1){
/如果布尔表达式1的值为true执行代码 if(布尔表达式2){
//布尔表达式2的值为true执行代码}
}
switch多选择结构
- 多选择结构还有一个实现方式就是switch case语句。
- switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
package com.liu.struct;public class switchDemo01 { public static void main(String[] args) { char grade = 'C'; switch (grade){ case 'A': System.out.println("优秀"); break; case 'B': System.out.println("良好"); break; case 'C': System.out.println("一般"); break; case 'D': System.out.println("合格"); break; default: System.out.println("不合法"); } }}
一般
Process finished with exit code 0
Switch主要是匹配一个具体的值
循环结构
while循环
-
while是最基本的循环,它的结构为:
while(布尔表达式) {
//循环内容
} -
只要布尔表达式为true,循环就会一直执行下去
-
我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
-
少部分情况需要循环一直执行,比如服务器的请求响应监听等。
-
循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死奔溃!
package com.liu.struct;public class WhileDemo01 { public static void main(String[] args) { //输出1-100 int i = 0; while (i<100){ i++; System.out.println(i); } }}
结果输出了1-100
计算1+2+3+…+100
package com.liu.struct;public class WhileDemo02 { public static void main(String[] args) { int i = 0 ; int sum = 0 ; while (i<=100){ sum = sum +i; i++; } System.out.println(sum); }}
5050
Process finished with exit code 0
do while 循环
- 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
- do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。
do {
//代码语句}while(布尔表达式); - While和do-While的区别:
- while先判断后执行。dowhile是先执行后判断!
- Do…while总是保证循环体会被至少执行一次!这是他们的主要差别。
public class DoWhileDemo01 { public static void main(String[] args) { int i = 0 ; int sum = 0 ; do { sum = sum +i; i++; }while(i<=100); System.out.println(sum); }}
5050
Process finished with exit code 0
for循环*
-
虽然所有循环结构都可以用while或者do…while表示,但Java提供了另一种语句——for循环,使一些循环结构变得更加简单。
-
for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
-
for循环执行的次数是在执行前就确定的。语法格式如下:
for(初始化;布尔表达式;更新){
//代码语句
} -
练习1:计算0到100之间的奇数和偶数的和
-
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
-
练习3:打印九九乘法表
while对比for 循环:
package com.liu.struct;public class ForDemo01 { public static void main(String[] args) { int a = 1;//初始化条件 while (a<=100){//条件判断 System.out.println(a);//循环体 a+=2;//迭代 } System.out.println("while循环结束"); for (int i = 1;i<=100;i++){ //100.for更快捷 System.out.println(i); } System.out.println("for循环结束"); }}
关于for循环有以下几点说明:
最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程。for(; ;)是死循环
计算0到100之间的奇数和偶数的和
package com.liu.struct;public class ForDemo02 { public static void main(String[] args) { int oddSum = 0; int evenSum = 0; for (int i = 0; i < =100; i++) { if (i % 2!=0){//奇数 %代表去余 oddSum+=i;//oddSum = oddSum + i }else {//偶数 evenSum+=i; } } System.out.println("奇数的和"+oddSum); System.out.println("偶数的和"+evenSum); }}
奇数的和2500
偶数的和2550
Process finished with exit code 0
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
package com.liu.struct;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){//每三个是一行
System.out.println();
//System.out.println("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
0
5 10 15
20 25 30
35 40 45
50 55 60
65 70 75
80 85 90
95 100 105 …
980 985 990
995
Process finished with exit code 0
练习3:打印九九乘法表
package com.liu.struct;
public class ForDemo04 {
public static void main(String[] args) {
for (int j = 0; j <= 9; j++) {
for (int i = 1;i<=j;i++){
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}//1.我们先打印第一列,这个大家应该都会
//2.我们把固定的1再用一个循斯包起来
//3.去掉重复项, i <= j
//4.调整样式
11=1
21=2 22=4
31=3 32=6 33=9
41=4 42=8 43=12 44=16
51=5 52=10 53=15 54=20 55=25
61=6 62=12 63=18 64=24 65=30 66=36
71=7 72=14 73=21 74=28 75=35 76=42 77=49
81=8 82=16 83=24 84=32 85=40 86=48 87=56 88=64
91=9 92=18 93=27 94=36 95=45 96=54 97=63 98=72 9*9=81
Process finished with exit code 0
增强for循环
- 这里我们先只是见一面,做个了解,之后数组我们重点使用
- Java5引入了一种主要用于数组或集合的增强型for循环。
- Java增强for循环语法格式如下:
for(声明语句︰表达式){
//代码句子} - 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
package com.liu.struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定义了一个数组
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
System.out.println("====================");
//遍历数组的元素
for (int x :numbers){
System.out.println(x);
}
}
}
10
20
30
40
50
10
20
30
40
50
Process finished with exit code 0
break continue
-
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
-
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
关于goto关键字
-
goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子—带标签的break和continue。
-
“标签”是指后面跟一个冒号的标识符,例如: label:
-
对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
package com.liu.struct;
public class BreakDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
break;
}
}
}
}
1
2
3
4
5
6
…
27
28
29
30
Process finished with exit code 0
package com.liu.struct;
public class ContinueDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.print(i);
}
}
}
123456789
111213141516171819
212223242526272829
313233343536373839
414243444546474849
515253545556575859
616263646566676869
717273747576777879
818283848586878889
919293949596979899
Process finished with exit code 0
break在任何循环语句的主体部分,均可用break控制循环的流程。
break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
打印三角形及Debug
package com.liu.struct;
public class TestDemo01 {
//打印三角形 5行
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >=i ; j--) {
System.out.print(" ");
}
for (int j =1;j<=i;j++){
System.out.print("*");
}
for (int j =1;j<i;j++){//注意j<i
System.out.print("*");
}
System.out.println();
}
}
}
*
***
Process finished with exit code 0