课程: Learning Java 17
Debugging with print statements
- [Instructor] When writing code, sometimes we can make mistakes and get errors. These errors can cause our code to give the wrong output or crash our program entirely. And you've probably already experienced this a little bit so far. The term debugging involves the process of locating and correcting code errors in your program. We often call these errors bugs because computer scientist pioneer Grace Hopper coined the term bug when she found a moth causing an error in her early computer. In this chapter, we'll look at some strategies for how to debug our programs and fix some common errors. Now, in order to fix our code, we have to understand how our program works. One way to do this is with print statements. For example, we can use a print statement to find out the value of a given variable. We can also use them to follow the control flow of our code. There's a few different categories of errors a given program can have, but we're going to look at syntax errors first. Syntax errors cause your program to fail before it's even executed, because the computer has to understand your code in order to run it. This means if something's misspelled or a certain symbol is missing, the program won't run because your code is not in the correct format. Now, in this lesson, we'll take a look at our multiple choice program from the last challenge, with a few errors introduced. Let's find some syntax errors. These can be easier to see with an IDE because an IDE tells you what line has the error. It might not be that specific line you have to change, but it's probably something around it. On line 11, we have this red error, answerThree. We need to change this to choiceThree because answerThree is not a valid variable, it's not something we've defined. So let's change that. We can also see that correctAnswer is grayed out. This means this variable is not used. Here we have correctAnswer, but it's in red. It looks like we missed that r at the end of this word,. Let's add it. On line 19, we have another red squiggly. We can add semicolon here and that completes the statement, eliminating that error. Here's another bit of red. We just need another parenthese to close this if statement. Now all the errors are gone and we can run the program. Let's run it. The correct answer is Jupiter, but it says we're incorrect. So even though we can run the program, we still have some bugs. These bugs are different from the syntax errors because they deal with the logic of our program. The correct answer is not considered correct. We call these errors logical errors. A logical bug is an error where the program is able to run, but it doesn't act as the user expects. And that's exactly what we're seeing here. Jupiter is the correct answer, but the program doesn't know it's correct. In order to debug this, we often look for parts of the program that aren't working as expected, find its respective code, add some print statements, and fix it. Let's print out what the correct answer is as well as what the user input is. We'll add some print statements here. We'll say user's answer, and then we'll add the correct answer. Let's run it. We'll put in Jupiter. So it says the user's answer is Jupiter and the correct answer is Saturn. Saturn's not the correct answer, so the correctAnswer variable has the wrong value. Let's scroll up to where we set it. We set correctAnswer to be the value of choiceThree. We need to set the correctAnswer to choiceTwo. Let's run it again. We'll put in Jupiter, the user's answer's Jupiter and the correct answer's Jupiter, but we're incorrect. Let's scroll down. So up to this point, the program works as expected. The issue must be in this comparison. Here, we convert the input to lower case, but we don't convert the correct answer to lower case. CorrectAnswer has the value Jupiter with a capital J, so we need to change this to a lower j. And we'll do this for the other choices as well. Let's save it and run it again. We'll put in Jupiter, and it's correct. Now, this is a fairly trivial example, but as our programs get more complicated, understanding the values of our variables and the way our program is executing code becomes more and more important.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。