一、断点调试的作用:
1、查看程序的执行流程
2、调试程序
断点:就是一个标记
二、断点的使用
1、在何处加断点:哪里不会加哪里
2、如何加断点:在代码区域 左边双击即可
3、如何运行加断点 程序:
代码区域-----右键----Debug As----Java Application
弹出一个框,选择进入Debug视图
4、如何让程序往下执行:F6(step over)
5、看哪些区域:
代码区域:看程序的执行流程
Debug区域:看流程的执行流程
Variables:变量的变化
Console:看程序的输入和输出
6、如何去掉断点:
1、双击去掉断点
2、打开断点视图----Breakpoint,选中要删除的断点
7、注意:断点必须加在有效的语句上
三、参数的引用
1、参数的引用类型是基本数据类型
形式参数的改变不会影响实际参数
2、参数的引用类型是引用数据类型
形式参数的改变直接影响实际参数
代码块:
public class DebugText{
public static void main(String[] args){
int[] arr={1,2,3,4,5};
for(int x=0;x<arr.length;x++){
System.out.println(arr[x]);
}
}
public static void change(int[] arr){
for(int x;x<arr.length;x++){
if(arr[x]%2==0){
ar[x]*=2;
}
}
}
}
输出结果为:
1
4
3
8
5
四、用例
1、需求:键盘录入一个月份,输出该月份对应得季节
分析:
代码录入一个月份,用Scanner实现
判断该月份是几月,根据月份输出对应的季节
代码实现1.1:
public class Test{
public static void main(String[] args){
//键盘录入一个月份
Scanner sc=new Scanner(System.in);
//接受数据
System.out.println("请输入月份(1-12):");
int month=sc.nextInt();
if(month>12||month<1){
System.out.println("你输入的月份有误");
}else if(month==1){
System.out.println("冬季");
}else if(month==2){
System.out.println("冬季");
}else if(month==3){
System.out.println("春季");
}else if(month==4){
System.out.println("春季");
}else if(month==5){
System.out.println("春季");
}else if(month==6){
System.out.println("夏季");
}else if(month==7){
System.out.println("夏季");
}else if(month==8){
System.out.println("夏季");
}else if(month==9){
System.out.println("秋季");
}else if(month==10){
System.out.println("秋季");
}else if(month==11){
System.out.println("秋季");
}else{
System.out.println("冬季");
}
}
}
代码块1.2
//代码简化
public class Test{
public static void main(String[] args){
//键盘录入一个月份
Scanner sc=new Scanner(System.in);
//接受数据
System.out.println("请输入月份(1-12):");
int month=sc.nextInt();
if(month==1||month==2||month==12){
System.out.println("冬季");
}else if(month==3||month==4||month==5){
System.out.println("春季");
}else if(month==6||month==7||month==8){
System.out.println("夏季");
}else if(month==9||month==10||month==11){
System.out.println("秋季");
}else if{
System.out.println("你输入的月份有误");
}
}
}
2、有一对兔子,从出生后第三个月起,每个月都生一对兔子,小兔子长到第三个月后,每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
规律:
第一个月:1
第二个月:1
第三个月:2
第四个月:3
第五个月:5
~~~~~~
分析:
A、定义数组实现
int[] arr=new int[20]
B、给数组的元素赋值
arr[0]==1
arr[1]==2
C、找规律
arr[2]==arr[0]+arr[1];
arr[3]==arr[1]+arr[2];
arr[4]==arr[2]+arr[3];
arr[5]==arr[3]+arr[4];
…
代码块:
public class Test{
public static void main(Sring[] args){
//定义数组
int[] arr=new int[20];
//给数组的元素赋值
arr[0]==1;
arr[1]==2;
//按规律赋值
for(int x=2;x<arr.length;x++){
arr[x]=arr[x-1]+arr[x-2];
}
//输出结果
System.out.println("第二十个月兔子的对数是:"+arr[19]);
}
}
来源:https://blog.csdn.net/weixin_43801116/article/details/102732942