English subtitles

← Create the Score Variable - Solution

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Right, the first question.
  2. Should scoreTeamA be local or global?
  3. I'm going to answer this before
    even moving onto the code.
  4. Now, you had a little bit of practice
    with this in the earlier exercises.
  5. Given then the score needs to be saved
    and modified for a longer period of time
  6. and in between button clicks,
    it should be global variable.
  7. Okay, so I'm going to go ahead and
    define it now because I have a little
  8. bit of a better idea of where I should
    define it because I'm making it global.
  9. Okay, here I am in the code.
  10. I'm going to go over to the Java file,
  11. you might see that
    a couple things changed.
  12. Actually, really, the only thing that
    changed is I added some comments here to
  13. say what each of these methods does.
  14. Besides that, everything's the same.
  15. Okay.
  16. So, I need to define a global variable.
  17. I'm going to scroll up, and
  18. the reason that I scroll up
    is because by convention,
  19. when I have global variables, I put
    them right here at the top of the class.
  20. The difference in location of where you
    define a global variable versus a local
  21. variable is that local variables
    will be defined within methods,
  22. whereas global variables will
    be outside of the methods.
  23. So I'm putting it outside right here.
  24. Well, I know it's a number, and
  25. because you're always going to score
    at least one point, and you're
  26. not going to score 1.5 points, this
    number isn't going to include decimals.
  27. So, a type of integer is actually
    a pretty good choice here.
  28. So, I'm going to type int.
  29. Okay, and
  30. after the data type comes the variable
    name, and I'm going to use scoreTeamA.
  31. Okay, and if I was just doing
    a declaration, I'd be done.
  32. But, I was also told to
    initialize this to zero.
  33. So, forget the semicolon for now.
  34. I'm going to write the equal sign also
    known as the assignment operator and
  35. I'm going to set it to zero.
  36. Oh, and don't forget the semicolon.
  37. Okay, so, there's the answer for that.
  38. Good work so far.