- 复习:逻辑结构1.顺序 2.条件分支 if if - else if – else if –else switch-case(switch具有穿透性 符合第一个就会往下继续执行 加break终止 判断的变量必须是int 可以是能自动转换成int的值或String)3.循环 for while do-while
- 学习内容 1数组 练习1:定义一个长度为10的数组 求最大 最小 平均值public static void main(String[] args){
int[] n=new int[10];
int max,min,sum=0;
max = n[0];
min = 5;
for(int i =0;i<10;i++){
n[i]=(i+1)*3;
if(n[i]>max)
max = n[i];
if(n[i]<min)
min = n[i];
sum = sum+n[i];
}
System.out.println("max="+max+" min="+min+" 平均="+(sum/10));
} Arrays.toString()遍历数组输出System.out.println(Arrays.toString(arr)练习2 把数组的最大值添加到数组的末位(数组的扩容)public static void main(String[] args){
int[] n=new int[10];
int[] m=new int[n.length+1];
int max;
max = 0;
for(int i =0;i<10;i++){
n[i]=(int)(Math.random()*100);
if(n[i]>max)
max = n[i];
System.out.print(n[i]+" ");
}
System.out.println();
for(int i =0;i<m.length;i++){
if(i==m.length-1){
m[i]=max;
System.out.print(m[i]+" ");
break;
}
//m[i]=(int)(Math.random()*100);
m[i]=n[i];
System.out.print(m[i]+" ");
}n = m;
} 方法1.原数组=Arrays.copyOf(1.原数组,2.新数组的长度);(将原数组的元素赋值到新数组)可实现数组的扩容和缩容 方法2. System.arraycopy(arg0,1,2,3,4)含义0:原数组 1:从原数组哪个下标开始复制 2:复制到的新的数组(目标数组)3:从新数组哪个下标开始复制4:从原数组复制的长度 注意:不能下标越界 练习3 双色球 public static void main(String[] args){
int[] red=new int[5];
for(int i=0;i<5;i++){
red[i]=(int)(Math.random()*33+1);
for(int j=0;j<i;j++)
if(red[j]==red[i]){
red[i]=(int)(Math.random()*33+1);
i--;
}
}
System.out.println(Arrays.toString(red));
Arrays.sort(red);
System.out.println(Arrays.toString(red));
int blue=(int)(Math.random()*16+1);
System.out.println(blue);
red=Arrays.copyOf(red,6);
red[5]=blue;
System.out.println(Arrays.toString(red));
} 对上面提到的复制数组方法的简单练习 Arrays.sort(red);为对red数组的升序排序
- 半程重点总结:学了一种数组排序的便捷方法Arrays.sort()和几种数组复制的方法方法1.原数组=Arrays.copyOf(1.原数组,2.新数组的长度);(将原数组的元素赋值到新数组)可实现数组的扩容和缩容 方法2. System.arraycopy(arg0,1,2,3,4)含义0:原数组 1:从原数组哪个下标开始复制 2:复制到的新的数组(目标数组)3:从新数组哪个下标开始复制4:从原数组复制的长度 注意:不能下标越界
- 类加载:将字节码文件中类相关的信息读取到jvm内存(方法区)中
对于栈内存 堆内存 方法区的初次接触 在类里也看到了类似的
堆:引用类型的真正数据
- 方法(函数):public static void main(String[] args) 修饰符(public static) + 返回值类型(void 基本数据类型 引用数据类型) + 方法名(随意) + 方法参数(类型+引用名 类型+变量名) + 方法体{} 方法的命名规则:1驼峰命名法 首单词字母小写 第二个单词首字母大写 2可读性要好 方法的返回值:与规定的值必须相同(return) return表示方法的结束 有方法参数(形参)的方法 形参可以被认为是已知量。
方法练习1 定义找三个整数中最大值的方法
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int max=fOundmax(a,b,c);
System.out.println(max);
}
public static int fOundmax(int a,int b,int c){
int temp = (Math.max(a, b));
int max = (Math.max(temp, c));
return max;
}
2 键盘录入两个整数n m 打印n行m列的@符号
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
daying(a,b);
}
public static void daying(int a,int b){
for(int i=0;i<a;i++) {
for (int j = 0; j < b; j++) {
System.out.print("@");
}
System.out.println();
}
}
练习3 比较数组内容是否相同
public static void main(String[] args){
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
int[] c={1,2,3,4,};
if(compare(a,b))
System.out.println("a b相同");
if(compare(b,c))
System.out.println("b c相同");
else System.out.println("b c不同");
}
public static boolean compare(int a[],int b[]){
if(a.length!=b.length) return false;
for(int i=0;i<a.length;i++)
if(a[i]!=b[i]) return false;
return true;
}
nextline遇到回车就结束
代码加括号叫方法 length()方法 length属性
练习4 随机生成5个不同的大写字母 用户猜 输出猜对几个位置对几个
public static void main(String[] args){
String b=burn();
String g;
Scanner sc=new Scanner(System.in);
g=sc.nextLine();
System.out.println(guess(b,g));
System.out.println(b);
System.out.println(g);
}
public static String burn(){
char[] s=new char[5];
for(int i=0;i<s.length;i++) {
s[i] = (char) (Math.random() * 26 + 65);
for(int j=0;j<i;j++)
if(s[i]==s[j]) {
s[i] = (char) (Math.random() * 26 + 65);
i--;
}
}
String ch=new String(s);
return ch;
}
public static String guess(String s1,String s2){
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
int n=0,m=0;
if(s1.length()!=s2.length()) return "数量对不上";
for (int i=0;i<c1.length;i++)
for (int j=0;j<c1.length;j++)
if(c1[i]==c2[j]) {
n++;
if(i==j)
m++;
}
return "猜对了"+n+"位置对"+m;
}
一天感悟:nextline遇到回车就结束
代码加括号叫方法 length()方法 length属性
更深刻的理解了数组在内存中是怎么储存的,画了相关的图,能为将来理解类以及更深刻的东西打下基础。初步复习掌握运用了方法,现阶段的熟练度应该是够的。知道了几个方便的方法(函数),要更熟练的运用它们