java学习笔记day06
思维导图
二维数组 + 面向对象概述
二维数组🎄
与C不同,java的二维数组必须有行数(一维数组的个数固定),而且各个一维数组的长度可以不相等。C语言的二维数组必须有列数(一维数组的长度固定)。
二维数组的内存分配 【P150】
二维数组案例1:杨辉三角
题目:
输入n代表杨辉三角行数,打印杨辉三角。
题解:
答案代码:
import java.util.Scanner;
public class stuy {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字n");
int n = sc.nextInt();
int [][] a = new int[n][n];
for (int i = 0; i < n; i++) {
a[i][0] = 1;
a[i][i] = 1;
}
for (int i = 2; i <a.length; i++) {
for (int j = 1; j <= i-1; j++) {
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j <=i; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
二维数组案例2:密码加密
题目:
答案代码:
import java.util.Scanner;
public class stuy {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h =0;//计算输入数字的位数
int x = sc.nextInt();
int y = x;
while(y!=0) {
y/=10;
h++;
}
int [] a = new int[h];
for (int i = h-1; i >= 0; i--) {
a[i] = x%10;
x/=10;
}
for (int i = 0; i < h/2; i++) {
int temp = a[i];
a[i] = a[h-1-i];
a[h-1-i] = temp;
}
for (int i = 0; i < h; i++) {
a[i] = (a[i]+5) % 10;
}
int t = a[0];
a[0] = a[h-1];
a[h-1] = t;
for (int i = 0; i < h; i++) {
System.out.print(a[i]);
}
}
}
面向对象🎄
面向对象思想(把大象装进冰箱)
类与对象⭐
类的定义及使用
案例⭐
学生类的定义及其使用
1.学生类的定义
2.学生类的使用
内存图⭐ 【P170】
1.一个对象的内存图
2.两个对象的内存图
3.三个对象的内存图
Java程序的开发、设计、特征
写在最后
java的基础语法和C几乎一样。现在已经过完了基础,并对面向对象有了一些认识。下面的学习就该难了…
加油,坚持下来。