1 Eclipse断点调试
1.1 Eclipse断点调试常用操作:
什么是断点?
就是一个标记,记录开始位置
如何设置断点?
目的代码片段的有效起始位置的 左侧双击
在哪里设置
需要查看代码详细运行情况的部位
如何运行设置断点后的程序?
右键 – Debug as – Java Application
Debug:断点测试的地方,f6,逐行运行代码
Variables:查看程序的变量变化
ForDemo:被查看的源文件
Console:控制台
双击去除单个
找到Debug视图,Variables界面,Breakpoints ,点击,点击双叉
找不到可以在Debug视图下打开Windows–show view–Breakpoint (Variables同理)
基础语法练习
switch穿透
相同判定的放在一起
结束case完成动作
package com.itheima.day06.mycode;
import java.util.Scanner;
// 需求:键盘录入一个月份,输出该月份对应的季节。
public class MonthToSeason {
public static void main(String[] args) {
System.out.println("请输入月份:");
Scanner scanner = new Scanner(System.in);
int month = scanner.nextInt();
monthToSeason(month);
}
private static void monthToSeason(int month) {
// TODO Auto-generated method stub
switch (month) {
case 3:
case 4:
case 5:
System.out.println(month + "月是春季!");
break;
case 6:
case 7:
case 8:
System.out.println(month + "月是夏季!");
break;
case 9:
case 10:
case 11:
System.out.println(month + "月是秋季!");
break;
case 12:
case 1:
case 2:
System.out.println(month + "月是冬季!");
break;
}
}
}
循环控制循环的变量定义时机
要注意:
1.作用范围
2.判定时机
3.定义时机
// 1.随机生成10个整数,不重复,存入数组中
public static int[] setArr(int a) {
// System.out.println(a);
int[] arr = new int[a];
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
int sign = 0;
while (sign == 0) {//赋值成功后,才进行下一循环
int count = 0;// 要定义在这里,否则无法+1后无法置零!!!
int num = random.nextInt(15) + 1;// 定义在这里,出现重复后重新随机
// System.out.println(num);
for (int j = 0; j < arr.length; j++) {//遍历数组,记录与随机数相同的数的个数
if (num == arr[j]) {
count++;
}
}
//结束for循环后判定
if (count == 0) {
arr[i] = num;
sign = 1;
}
}
}
return arr;
}
费伯纳西数列
package com.itheima.day06.mycode;
//需求:
//有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
//假如兔子都不死,问第二十个月的兔子对数为多少?
public class FeiBoNaxi {
public static void main(String[] args) {
//算法一:所有兔子数=一月龄兔纸+二月龄兔纸+成熟龄兔纸;
int first = 1;
int second = 0;
int third = 0;
for (int i = 2; i <= 20; i++) {
third += second;
second = first;
first = third;
}
System.out.println(third+second+first);
//算法二:所有兔子=上月兔子+新生兔子;新生兔子=成熟兔子=上上个月兔子总数(上上个月的新兔子这个月全部成熟)即:所有兔子=上月兔子+上上月兔子
int month=1;
int sum=month;
for(int i=3;i<=20;i++){
int c=sum;
sum=sum+month;
month=c;
}
System.out.println(sum);
}
}