数据类型
- 基本类型 int short long float 。。。
- 引用类型 数组和对象
类型 变量名 = 变量值
判断和循环
package com.neusoft.day01;
import java.util.Random;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
int a = 100;
int[] arr = new int[3];
Random random = new Random();
// 左闭右开
int computer = random.nextInt(3);
// System.out.println("computer = " + computer);
Scanner scanner = new Scanner(System.in);
System.out.println("请出拳 0 石头 1 剪刀 2 布");
int player = scanner.nextInt();
System.out.println(player);
if ((player==0 && computer==1)||(player==1 && computer==2)||(player==2 && computer==0)){
System.out.println("恭喜玩家胜利");
}else if (player==computer){
System.out.println("平局");
}else {
System.out.println("你真菜, 电脑赢了");
}
}
}
package com.neusoft.day01;
import java.util.Random;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
// 猜 1 ~ 100之间的任意数字
Random random = new Random();
int computer = random.nextInt(100) + 1;
System.out.println("computer = " + computer);
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("请输入要猜的数字");
int player = scanner.nextInt();
if (player > computer)
System.out.println("猜大了");
else if (player < computer)
System.out.println("猜小了");
else{
System.out.println("猜对了");
break;
}
}
}
}
package com.neusoft.day01;
import java.util.Random;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
// 猜 1 ~ 100之间的任意数字
Random random = new Random();
int computer = random.nextInt(100) + 1;
System.out.println("computer = " + computer);
Scanner scanner = new Scanner(System.in);
for (;;){
System.out.println("请输入要猜的数字");
int player = scanner.nextInt();
if (player > computer)
System.out.println("猜大了");
else if (player < computer)
System.out.println("猜小了");
else{
System.out.println("猜对了");
break;
}
}
}
}
数组
package com.neusoft.day01;
import java.util.Random;
public class Test4 {
public static void main(String[] args) {
double[] arr = new double[5];
for (int i =0; i < 5; i++)
System.out.print(arr[i] + " ");
System.out.println("上面是赋值前");
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextDouble(); // [0, 1)
}
System.out.println("下面是赋值后");
for (int i =0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
方法(函数)
package com.neusoft.day01;
import java.util.Random;
public class Test5 {
public static void main(String[] args) {
hello();
hello("eric", 19);
System.out.println(show());
System.out.println(cal(100));
}
public static void hello(){
System.out.println("hello");
}
// 同名不同参 方法的重载
// 参: 参数类型/个数/顺序 与返回值无关
public static void hello(String name, int age){
System.out.println("hello" + name);
}
// 无参有返回
public static int show(){
return 111;
}
// 有参有返回
public static int cal(int n){
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
package com.neusoft.day01;
import java.util.Random;
public class Test6 {
public static void main(String[] args) {
int[] ints = generate(20, 100, 10);
for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i] + " ");
}
System.out.println();
System.out.println(selectMax(ints));
}
public static int[] generate(int l, int r, int n){
int[] arr = new int[n];
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(r-l+1) + l;
}
return arr;
}
public static int selectMax(int[] arr){
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
}
package com.neusoft.day01;
public class Test7 {
public static void main(String[] args) {
int[] arr = {64, 50, 36, 100, 29, 98, 23, 21, 34, 49};
printArray(arr);
printStar(5, 5);
printSanJiao(5);
printJiuJiu();
}
public static void printStar(int row, int col){
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void printJiuJiu(){
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " x " + i + " = " + i*j + "\t");
}
System.out.println();
}
}
public static void printSanJiao(int row){
for (int i = 0; i < row; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void printArray(int[] arr) {
String str = "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1)
str += arr[i] + "]";
else
str += arr[i] + ", ";
}
System.out.println(str);
}
}
排序
package com.neusoft.day01.guocheng;
public class Test8 {
public static void main(String[] args) {
int[] arr = {64, 50, 36, 100, 29, 98, 23, 21, 34, 49};
printArray(arr);
System.out.println("排序前");
bubbleSort(arr);
System.out.println("排序后");
printArray(arr);
}
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = arr.length -1; j-1>= i ; j--) {
if (arr[j-1] > arr[j]){
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void printArray(int[] arr) {
String str = "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1)
str += arr[i] + "]";
else
str += arr[i] + ", ";
}
System.out.println(str);
}
}
面向对象
- 类和对象
package com.neusoft.day01.oop;
public class Student {
// 属性
String name;
int score;
// 方法
public void study(){
System.out.println(name + "在嗷嗷的学习.....");
}
public void eat(){
System.out.println(name + "在嗷嗷的吃.....");
}
public void show(){
System.out.println(name + "的成绩是" + score );
}
}
测试
package com.neusoft.day01.oop;
public class Test9 {
public static void main(String[] args) {
// 使用类 创建对象,
// 类是相同属性(特征)和方法(行为)的集合
Student s1 = new Student();
Student s2 = new Student();
System.out.println(s1);
System.out.println(s2);
System.out.println(s1.name);
System.out.println(s1.score);
s1.name = "张三";
s1.score = 640;
s1.eat();
s1.study();
s1.show();
}
}
构造函数:初始化属性
package com.neusoft.day01.oop;
public class Student {
// 属性
String name;
int score;
public Student(){
System.out.println("无参构造被调用");
}
public Student(String name, int score){
this.name = name;
this.score = score;
}
// 方法
public void study(){
System.out.println(name + "在嗷嗷的学习.....");
}
public void eat(){
System.out.println(name + "在嗷嗷的吃.....");
}
public void show(){
System.out.println(name + "的成绩是" + score );
}
}
测试
package com.neusoft.day01.oop;
public class Test10 {
public static void main(String[] args) {
Student student2 = new Student("刘能", 78);
Student student1 = new Student("谢广坤", 178);
student2.show();
student1.show();
}
}
面向对象三大特性
- 封装
Java Bean风格的封装形式 - 属性私有化
- 无参构造函数
- 有参构造建议提供
- 提供公有的getXX, setXX方方法
package com.neusoft.day01.oop;
public class Teacher {
// - 属性私有化
private String name;
private int salary;
//- 无参构造函数
public Teacher() {
}
//- 有参构造建议提供
public Teacher(String name, int salary) {
this.name = name;
this.salary = salary;
}
//- 提供公有的getXX, setXX方方法
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
测试
package com.neusoft.day01.oop;
public class Test11 {
public static void main(String[] args) {
Teacher t1 = new Teacher("皮校长", 99999);
System.out.println(t1.getName());
System.out.println(t1.getSalary());
t1.setSalary(99);
System.out.println(t1.getSalary());
}
}
练习
Hero , 属性 名字, 生命值 --- JavaBean
进行测试
- 继承
子类继承父类所有非私有属性和方法
public class Animal {
String name;
String color;
public Animal(String name, String color) {
this.name = name;
this.color = color;
}
}
public class Cat extends Animal{
public Cat(String name, String color) {
super(name, color);
}
}
public class Dog extends Animal{
public Dog(String name, String color) {
super(name, color);
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog("豆豆", "black");
Cat cat = new Cat("花卷", "white");
System.out.println(dog.name);
System.out.println(dog.color);
System.out.println(cat.name);
System.out.println(cat.color);
}
}
重写: 覆盖父类同名方法进行重新实现
重载:同名不同参
package com.neusoft.day01.jicheng;
public class Animal {
String name;
String color;
public Animal(String name, String color) {
this.name = name;
this.color = color;
}
public void run(){
System.out.println("动物在跑....");
}
}
package com.neusoft.day01.jicheng;
public class Cat extends Animal{
public Cat(String name, String color) {
super(name, color);
}
@Override
public void run() {
System.out.println("鸟悄的跑......");
}
}
public class Dog extends Animal{
public Dog(String name, String color) {
super(name, color);
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog("豆豆", "black");
Cat cat = new Cat("花卷", "white");
System.out.println(dog.name);
System.out.println(dog.color);
System.out.println(cat.name);
System.out.println(cat.color);
dog.run();
cat.run();
}
}
- 多态