[end] CS61B Java 04: Lab02 Debugging

Debug操作指北

Installed of clicking the green triangle to run, click the debug option: The selected program should run until it hits its first breakpoint. A debugger window should also appear on the bottom of the interface, where the consonsole was.
On the left, you will be able to see all current method calls and on the right, you will be able to see the values of instantiated variables at this point in the program (they will also be shown in grey test in the editor).实例化变量值,在编辑器中以灰色文本显示 For instances of classes, you can click the dropdown to expand them and look at their fields. 实例化类可展开下拉列表查看其中的字段
To see the console output (and type into the console) while debugging, click the “Console” tab next to “Debugger” in the top left of the debug window.

In the debugger, you have a few options:
在这里插入图片描述
Click |→ to resume the program (until it hits another breakpoint or terminates). 执行直至下一个断点处暂停
Click ↗↘ to advance the program by one line of code.
does something similar, but it will step into any method called in the current line, while ↗↘ will step over it. (每一步要执行完成后✅再执行下一步)
will advance the program untile after it returns from the current method.
If you accidentally step too far and want to start the session over, click .
⚽️/ Mute Breakpoints:所有断点失效,达到Run的效果。
🧮计算器:复制小段代码,在其中计算,查看是否有报错,以及涉及到的参数值。适合一行代码内含多个嵌套调用方法。

Debug 过程中参数this代表什么?正要初始化的对象

Bomb

For this lab, you are forbidden from editing the ‘Bomb’ code, weather to add print statements or otherwise modify it. 不允许修改bomb/Bomb.java

Phase 0

Set a breakpoint at phase0 and use the debugger to find the password for phase0 and replace the phase0 argument accordingly in bomb/BombMain.java. 需修改bomb/BombMain.javaphase0的参数为debug发现的密码
How to find the password? Debug后继续往下走一步,可以看到password被赋值计算的到的结果
Debug 过程中参数this代表什么类?

Visualizer (Phase 1)

Sometimes we have data that’s not the easiest to inspect. For example, to look at long IntLists, we need to click a lot of dropdowns. To use the visualizer, run the debugger until you stop at a breakpoint, then click the “Java Visualizer” tab. The Java Visualizer shows a box-and-pointer diagram of the variables in your program.

Conditional Breakpoints (Phase 2)

To have your program pause only on certain conditions, creat a breakpoint and open the “Edit breakpoint” menu by right-clicking the breakpoint icon itself. Then enter a boolean codition.
Set breakpoints for exceptions in Java. Click ⚪⚪ in the debugger window and press the plus to create a “Java Exception Breakpoint”.

passwordPieces[i]=java.lang.IndexOutOfBoundsException: Invalid array index: 1337 //数组越界
Integer.parseInt(passwordPieces[1337])=-81201430
反之,将十进制整数-81201430转换为字符串passwordPieces[1337] String[]
设计多行代码得出足够长的字符串password -》spilit位Pieces
tip1:Integer.parseInt(-81201430)=-81201430
tip2:设计的数组只要满足第1337位=-81201430即可,其他位置的值没有要求
tip3:password由一段代码生成,可以定义一个变量,生成变量后,将该变量填入参数

Reading Stack Traces

When a runtime error occurs in Java, a stack trace is printed to the console to provide information on where the error occurred and what steps the program took to get there.
The first thing to note is what kind of error occurred. For some exceptions, Java will give you an explantion.
The lines beneath it represent the sequence of methods the program took to arrive at the error: the first line in the list is where the error occurred and the line beneath it represents the line of code that called the method which threw the error, and so on. You can click on blue text to navigate to that file and line.
You can ignore the lines with . Ignore the grey links in the stack trace. The error may have occurred in code that was not yours, but the root cause was probably something your code tried to do.

Debug BeeCountingStage

test incorrect input
Task2
Hint: Just because the error occurs on a certain line doesn’t necessarily mean that piece of code is incorrect - something not shown in the stack trace may be the elusive culprit!

Debug SpecieslListStage

运行AdventureGameTests.java
报错处:captureClean和expectedClean不一致

assertWithMessage(assertionMessage)
        .that(sliceTextFileUntil(captureClean, stageEndLine))
        .isEqualTo(sliceTextFileUntil(expectedClean, stageEndLine));
// 调用该报错的方法处:
public void testStageCorrect(String stage, CaptureSystemOutput.OutputCapture capture) {
    runUntilStage(stage, "correctInput.txt", "correctAnswers.txt", capture,
            "Game output for correct inputs on " + stage + " does not match");

运行 testStageCorrect方法,使用了String stage和capture参数
方法内运行runUntilStage函数,使用stage,inputFile,outputFile和capture参数
报错1diff
查看AdventureGameTests.java里的numCorrect 不同
修改1: SpecieslListStage.java中arraySimilarity方法整除返回的结果改为浮点数

return similarObjects / listOne.size(); // old
        return (float)similarObjects / listOne.size(); //new

报错2:StringIndexOutOfBoundsException: String index out of range: 3
修改2

// for (int i = s.length(); i > 0; i++) { // old
//            a[s.length() - i] = Character.getNumericValue(s.charAt(i));
        for (int i = s.length(); i > 0; i--) { //new
            a[s.length() - i] = Character.getNumericValue(s.charAt(i - 1));

Debug PalindromeStage

报错1:Game output for incorrect inputs on PalindromeStage does not match
Click to see difference

Expected: That’s not a palindrome! Try again.
Wow, nice room number! // numLst.equals(reversedLst)
Hmm, you’re getting hungry. Where do you want to go?
Actual:
Hmm, you’re getting hungry. Where do you want to go? // maybe (reversedLst == null)
/>> [hearst food court] [va cafe]
Sorry, I don’t understand that. Please type one of the responses in the brackets!

处理1:

// Uncomment the following line if you are working through the optional section
reversedLst = reverseList(numLst);

报错2:一直运行中
Note: If the debugger/runner feels unresponsive, it is usually due to an infinite loop somewhere in your code. If you set a breakpoint and it is never reached, then you know an infinite loop occurs before the breakpoint! Use this in combination with stepping to isolate the problem.

old:
AdventureGameTests.java
	testStageIncorrect
		runUtilStage
			runTestGame
				play
					handleStage
			
			nextStagePrompt
new:
AdventureGameTests.java
	testStageIncorrect
		runUtilStage
			runTestGame
				play
AdventureGame.java
	play
		handleStage //this.currentStage = PalindromeStage while(true) 这里无限循环了!!
			playStage

未注释reversedLst = reverseList(numLst);时,

if (reversedLst == null) {
    break;
}

break跳出while (true)
注释后,跳过上个break,但没有走到另一个break,从而陷入while (true)循环
由于Expected依次打印Try again 和 Wow
因此,代码跳过一次break打印Try again ,下次循环则进入这个break,打印Wow

if (numLst.equals(reversedLst)) {
    System.out.println("Wow, nice room number!");
    break;
}

第一轮:input=403

IntList numLst = digitsToIntList(input)
reversedLst = reverseList(numLst);

在这里插入图片描述
返回了两位链接列表 3->0
因此reversedLst有问题
修改2:

reversed = new IntList(l.first, reversed); // 倒装最后一步添加第一位至最后一位

Debug “MachineStage”

Run and Click to see difference

The machine whirrs briefly before outputting a slip of paper, reading xx. //读数有误

直接定位计数处代码,向上回溯,求和公式有误,应选最大的,选了最小的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值