java基础篇
IDEA常用快捷键
快捷键功能
Alt+Enter自动修正代码Ctrl+Y删除光标所在行Ctrl+D复制光标所在行的内容,插入光标位置下面Ctrl+Alt +L格式化代码Ctrl+/单行注释,再按取消注释Ctrl+Shift+/选中代码注释,多行注释,再按取消注释。Alt+Ins自动生成代码,toString,get,set等方法Alt+Shift+上下箭头移动当前代码行
API概述和使用步骤
API使用步骤
JDK 1.6 API 中文版
JDK链接: https://pan.baidu.com/s/1TO9hgDfMcS_N2q1y40s13Q 提取码: e55f
1.打开帮助文档
2.点击显示,找到索引,看到输入框
3.你要找谁?在输入框里输入然后回车
4.看包。java.lang下的类不需要导包,其他需要
5.看类的解释和说明。
6.学习构造方法。
7.使用成员方法。
Scanner概述及其API文档的使用
Scanner的使用步骤
package cn
.itcast
.day01
.demo01
.scanner
;
import java
.util
.Scanner
;
public class Demo01Scanner {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
int num
= sc
.nextInt();
System
.out
.println("输入的int数字是:" + num
);
String str
= sc
.next();
System
.out
.println("输入的字符串是:" + str
);
}
}
Scanner练习
键盘输入两个数字求和
package cn
.itcast
.day01
.demo01
.scanner
;
import java
.util
.Scanner
;
public class Demo02ScannerSum {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
System
.out
.println("请输入第一个数字:");
int a
= sc
.nextInt();
System
.out
.println("请输入第二个数字:");
int b
= sc
.nextInt();
int result
= a
+ b
;
System
.out
.println("结果是:"+ result
);
}
}
键盘输入三个数字求最大值
package cn
.itcast
.day01
.demo01
.scanner
;
import java
.util
.Scanner
;
public class Demo03ScannerThree {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
System
.out
.println("请输入第一个数字:");
int a
= sc
.nextInt();
System
.out
.println("请输入第二个数字:");
int b
= sc
.nextInt();
System
.out
.println("请输入第三个数字:");
int c
= sc
.nextInt();
int temp
= a
> b
? a
: b
;
int max
= temp
> c
? temp
: c
;
System
.out
.println("最大值是:" + max
);
}
}
匿名对象
package cn
.itcast
.day01
.demo01
.anonymous
;
public class Person {
String name
;
public void showName() {
System
.out
.println("我是:" + name
);
}
}
package cn
.itcast
.day01
.demo01
.anonymous
;
public class Demo01Anonymous {
public static void main(String
[] args
) {
Person per
= new Person();
per
.name
= "刘诗诗";
per
.showName();
System
.out
.println("===========");
new Person().name
= "吴奇隆";
new Person().showName();
}
}
匿名对象作为方法的参数和返回值
package cn
.itcast
.day01
.demo01
.anonymous
;
import java
.util
.Scanner
;
public class Demo02Anonymous {
public static void main(String
[] args
) {
Scanner sc
= methodReturn();
int num
= sc
.nextInt();
System
.out
.println("输入的是"+num
);
}
public static void methodParam(Scanner sc
) {
int num
= sc
.nextInt();
System
.out
.println("输入的是"+ num
);
}
public static Scanner
methodReturn() {
return new Scanner(System
.in
);
}
}
Random概述和基本使用
Random生成指定范围的随机数
package cn
.itcast
.day01
.demo01
.random
;
import java
.util
.Random
;
public class Demo01Random {
public static void main(String
[] args
) {
Random r
= new Random();
int num
= r
.nextInt(3);
System
.out
.println("随机数:" + num
);
}
}
生成1~n之间的随机数
package cn
.itcast
.day01
.demo01
.random
;
import java
.util
.Random
;
public class Demo03Random {
public static void main(String
[] args
) {
int n
= 5;
Random r
= new Random();
int result
= r
.nextInt(n
) + 1;
System
.out
.println(result
);
}
}
猜数字小游戏
package cn
.itcast
.day01
.demo01
.random
;
import java
.util
.Random
;
import java
.util
.Scanner
;
public class Demo04Random {
public static void main(String
[] args
) {
Random r
= new Random();
int randomNum
= r
.nextInt(100) + 1;
Scanner sc
= new Scanner(System
.in
);
while (true) {
System
.out
.println("请输入你猜测的数字:");
int guessNum
= sc
.nextInt();
if(guessNum
> randomNum
) {
System
.out
.println("太大了,请重试");
} else if (guessNum
< randomNum
) {
System
.out
.println("太小了,请重试");
} else {
System
.out
.println("恭喜你,猜中啦!");
break;
}
}
System
.out
.println("游戏结束");
}
}
对象数组
package cn
.itcast
.day01
.demo01
.array
;
public class Person {
private String name
;
private int age
;
public Person(String name
, int age
) {
this.name
= name
;
this.age
= age
;
}
public Person() {
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
}
package cn
.itcast
.day01
.demo01
.array
;
public class Demo01Array {
public static void main(String
[] args
) {
Person
[] array
= new Person[3];
Person one
= new Person("迪丽热巴", 18);
Person two
= new Person("杨幂", 28);
Person three
= new Person("金智贤", 38);
array
[0] = one
;
array
[1] = two
;
array
[2] = three
;
System
.out
.println(array
[0]);
System
.out
.println(array
[1]);
System
.out
.println(array
[2]);
System
.out
.println(array
[1].getName());
}
}