1 00:00:00,420 --> 00:00:01,980 ‫Welcome back to the second. 2 00:00:01,980 --> 00:00:08,040 ‫If statements challenge the high score challenge, I hope you manage to solve the issue and write your 3 00:00:08,040 --> 00:00:08,580 ‫code. 4 00:00:08,580 --> 00:00:11,970 ‫And in this video I'm just going to show you how I would solve it. 5 00:00:11,970 --> 00:00:16,770 ‫And as usual, there are multiple different approaches to solving this challenge. 6 00:00:16,770 --> 00:00:19,350 ‫So first of all, I create variables. 7 00:00:19,350 --> 00:00:26,640 ‫So the first one is going to be a static int high score, which I'm going to set to 300. 8 00:00:26,640 --> 00:00:33,570 ‫So the old high score was 300, now static string, high score player. 9 00:00:33,570 --> 00:00:36,150 ‫So that's the person who holds the high score. 10 00:00:36,150 --> 00:00:37,560 ‫And I'm just going to say it's me. 11 00:00:37,560 --> 00:00:39,690 ‫So my high score is 300. 12 00:00:39,810 --> 00:00:48,780 ‫Now I need to create a method which checks if the new score by a player is higher than the high score. 13 00:00:48,780 --> 00:00:59,850 ‫And I do that best with a public static void check, high score method. 14 00:01:00,030 --> 00:01:01,800 ‫And then here I need two parameters. 15 00:01:01,800 --> 00:01:07,950 ‫One of them is the score, so I'm going to say in score and the other one is a string, which is the 16 00:01:07,950 --> 00:01:09,240 ‫player name. 17 00:01:10,590 --> 00:01:17,010 ‫And in here what I want to do is I want to check if the score of the player is higher than the high 18 00:01:17,010 --> 00:01:17,310 ‫score. 19 00:01:17,310 --> 00:01:31,320 ‫So if score higher, high score, then go ahead and update the old high score with the score and also 20 00:01:31,920 --> 00:01:41,460 ‫update the high score player with the player name high score player is equal to player name. 21 00:01:43,410 --> 00:01:47,820 ‫Now I also want to inform the console about that. 22 00:01:47,820 --> 00:01:52,290 ‫So let's update that onto the console as well or write it on the console. 23 00:01:52,290 --> 00:01:59,160 ‫And I'm just going to write something like New High Score is and score 24 00:02:01,950 --> 00:02:05,190 ‫and console dot right line 25 00:02:07,920 --> 00:02:16,320 ‫it is now held by and let's write by whom is held it's by player name. 26 00:02:16,920 --> 00:02:22,860 ‫So the player who broke the record is going to be written on there as well. 27 00:02:22,860 --> 00:02:26,610 ‫And also the high score player is overridden. 28 00:02:26,610 --> 00:02:33,420 ‫So in both cases you could have either use score and player name or high score and high score player 29 00:02:33,420 --> 00:02:35,790 ‫because those two are updated already. 30 00:02:36,120 --> 00:02:37,530 ‫Now we have our if statement. 31 00:02:37,530 --> 00:02:39,210 ‫Let's go with the else. 32 00:02:39,660 --> 00:02:44,250 ‫So what should happen if the score is not higher than the old score? 33 00:02:44,280 --> 00:02:48,540 ‫Well, we simply write onto the console right line. 34 00:02:49,620 --> 00:02:55,080 ‫The old high score could not be broken. 35 00:02:56,340 --> 00:02:58,080 ‫It is still. 36 00:03:01,050 --> 00:03:08,580 ‫And then the high score and held by and then the player who holds it. 37 00:03:08,580 --> 00:03:10,950 ‫So here we need a plus as well. 38 00:03:10,950 --> 00:03:16,530 ‫So what I'm going to do is in order to make it more readable, I'm going to put it into the next line 39 00:03:17,010 --> 00:03:22,530 ‫and by the high score player. 40 00:03:24,840 --> 00:03:26,790 ‫So that's pretty much it. 41 00:03:26,790 --> 00:03:29,010 ‫That's our check high score method. 42 00:03:29,010 --> 00:03:35,340 ‫So this method is always going to check if the score is given as an argument is higher than the old 43 00:03:35,340 --> 00:03:35,940 ‫score. 44 00:03:35,940 --> 00:03:39,150 ‫And if it is, then we simply update the high score. 45 00:03:39,150 --> 00:03:42,870 ‫And we also write Who was the player who broke the score? 46 00:03:42,870 --> 00:03:48,630 ‫So now in order to figure out if a player has broken the score, we also need to call this method, 47 00:03:48,630 --> 00:03:49,080 ‫of course. 48 00:03:49,080 --> 00:03:57,690 ‫So I'm going to call this method in here, and I'm going to start off with a value of 250, which was 49 00:03:59,070 --> 00:04:00,450 ‫achieved by Maria. 50 00:04:00,450 --> 00:04:02,430 ‫And then we have another one. 51 00:04:02,850 --> 00:04:06,660 ‫Let's go with 315 by Michael. 52 00:04:08,130 --> 00:04:12,360 ‫And then I saw that Michael broke my high score. 53 00:04:12,360 --> 00:04:15,780 ‫So I went in and broke my own as well. 54 00:04:15,780 --> 00:04:22,560 ‫So I made 350 points and so I'm the high score leader again. 55 00:04:22,920 --> 00:04:24,000 ‫That's highly important to me. 56 00:04:24,000 --> 00:04:25,320 ‫That's why I do it now. 57 00:04:25,320 --> 00:04:25,920 ‫Just kidding. 58 00:04:25,920 --> 00:04:30,990 ‫So then what we have next is to write that onto the console. 59 00:04:30,990 --> 00:04:34,410 ‫So console, dot read. 60 00:04:34,920 --> 00:04:41,580 ‫So we do write it by calling these methods, but we don't see it unless we make the console stall. 61 00:04:41,580 --> 00:04:47,430 ‫So we need to use the read method here so that we are the old high score could not be broken. 62 00:04:47,430 --> 00:04:51,180 ‫It's still 300 and held by tennis. 63 00:04:51,180 --> 00:04:53,010 ‫Then the new high score is 350. 64 00:04:53,010 --> 00:04:54,600 ‫It is now helped by Michael. 65 00:04:54,810 --> 00:04:56,400 ‫New high score is 350. 66 00:04:56,400 --> 00:04:57,870 ‫It is now held by Dennis. 67 00:04:58,080 --> 00:04:59,940 ‫So now you see how you can. 68 00:05:00,440 --> 00:05:06,710 ‫Quite easily create a functionality that checks if a high score is broken or not. 69 00:05:06,710 --> 00:05:12,410 ‫And you could simply update, well, if you have a bigger game, let's say you could easily update the 70 00:05:12,410 --> 00:05:17,660 ‫high score now and well, always show the latest and greatest high score. 71 00:05:18,380 --> 00:05:19,120 ‫All righty. 72 00:05:19,130 --> 00:05:21,410 ‫I hope you manage to do that yourself. 73 00:05:21,710 --> 00:05:24,200 ‫There are not too many tricky things. 74 00:05:25,130 --> 00:05:29,210 ‫It's pretty much only things that we have covered in this chapter and before. 75 00:05:29,210 --> 00:05:31,580 ‫So now you have created your own method. 76 00:05:31,580 --> 00:05:39,920 ‫You have used if an LS and you call it the method and you even use global variables and local variables. 77 00:05:39,950 --> 00:05:44,780 ‫Now, we haven't used global ones, but we have used parameters. 78 00:05:44,810 --> 00:05:45,230 ‫Great. 79 00:05:45,230 --> 00:05:46,760 ‫So see you in the next video.