Movie review score
5


professor: continuing on. so we've got validinput now. we know we will never get out of this loop unless we got an odd or an even.so we've got the valid input. we've got… they are calling whether it is odd or even.now we need to do what? now we want to ask what they want to throw, an odd or an even. [repeating on-screen text] then we are going to read their throw in,and i believe we called that userthrow. now could we have them say--or could we sayto them, "enter a 1 or a 2; 1 if you want to throw a 1, or 2 if you want to throw a2"? yeah. you could. i'm just doing this so i can prove a little point here. not provea point, but teach you a little lesson.

the lesson i'm getting at is it will haveto do with the computer's choice. so for now let's read in whether they want to do oddor even. so i'll say… ...to read in their choice, whether it's oddor even. then that's going to be stored. again, i'm going to need to check for valid input. so i can do this exact same thing again. ialmost was tempted there to cut and paste, or copy and paste. but whenever i do that,it seems to end badly; i forget to change something. like i probably would've forgotto change usercall there. i might have changed here and here, but not there and then thatwould mess up the program. so in my mind, again, we are going to check for valid input.

truthfully, i'm seeing i'm doing the exactsame thing twice. that tells me this should be in a method somewhere and i should be callingthat method. so maybe we will address that later on in the program. but for now, i'mgoing to try to keep it kind of simple. so it's going to be the same thing. so… so while it's not one of those two things,then i'm going to say… then i can read in their new input... again, you will be bugged because you couldsay, "i could have just copied this. it would've been so much faster." believe me, in the longrun you will save more time by just typing it so you don't make that mistake. noticei didn't even look at the other one. i'm writing

it completely new. because i can't tell youhow many times i've spent trying to solve problems that came from copying and pastingcode. so now we've got valid input for that. nowwe need to… so how do we do that? what we are going togenerate for the computer is an odd or an even. right? but the computer--how do we generate a randomstring? how do we say--just like we've done with the random numbers, how do we say randomstring? well, sometimes in programming the easiestway is just kind of the simplest way. we don't know how to do a random string. but can weget a random number? yeah, we know how to

get a random number. once we have a randomnumber, can we decide on a string based on that number? sure. so let's do that. so what i'm going to do is get a random number.so i'm going to create one more variable here for the computer throw and call it the… what do we want to call it? this is just a temp variable... to decide cputhrow. so what we're going to do, then, is come inhere and get a random number. so we are going to set this cpurandomnumber variable equalto a random number. how do we get a random

number? again, in this class that we builtthere is no random--we can use the math.random, but i'm going to use the java.util.random.so this is a class that is sitting out in the java system memory. then i like to kindof compartmentalize everything; keep everything together. because i am building objects herethat are system objects, i'll probably just build the random right next to that, eventhough i could build it anywhere down there. i could have put that line down here somewhere.but i do like to kind of group together when i'm declaring my variables and if there aredifferent variables for different sections, sometimes i'll group those together. you justwant to make this thing really easy to read for whoever is going to look at it next whichcould be you in a couple of months. you're

looking at your own code and you're saying,"what was i doing here?" it's much easier if everything is kind of grouped together. i could have declared all these variablesjust when i used them down in the code, but then it becomes very hard when you were debuggingto find where the variable declaration is. it's nice to have them all up in one group. so i have declared my random number generator.again, random is the blueprint and i said go out and build me one of those… that iam going to call "r" from now on. it goes out in memory, builds you a random class thatis referred to as r or an instance of a random class that we refer to as r. now down here,i can say let's take that random number generator

and get a nextint, out of how many, is whatwe pass in the parentheses. we only really have two choices. we have an odd or an even.so we've only got two choices and that will give me a 0 or a 1. can i make a decision based on that now? yeah,again because i can say... ...then do what? let's set the cputhrow equalto odd. again, i could have used… what am i trying to say? an else statement. but, again, if for some reason i accidentallyput a 3 here or something, if i would have used an else, it would still be saying thecomputer's throw is even and there would be

a mistake in my program that i wouldn't catch.so i am safeguarding myself against myself by just using two if statements. if the computer'sthrow comes out to be blank, then i will pick up on that error quicker than i would if ihad an if and an else. so it just depends on the situation. you've just got to be carefulis what i'm saying. there's no hard, fast rule for that. it becomes a matter of feel. so we made the random choice for the computerand now we need to decide... so we're going to do that through a seriesof if statements. we have gone in and the user is picking odd or even. then we ask ifthey are going to throw an odd or an even… i guess the only thing i'm thinking is, really,what does odd or even mean the second time?

so when we ask at the first time… we're saying, "what is the outcome going tobe, odd or even?" but, really, the user picks an odd or an even. they are just picking a1 or a 2. do you want to pick a 1 or a 2 and the computer will pick a 1 or a 2. so i guessthe dilemma i'm having as i look at the code is i wanted to do this to show you how wecould generate a random string based on the choice. but, eventually, we're going to needto add the user's choice together with the computer's choice in order to get the totaland then decide whether it is an odd or even number. we can do that through if statementsby saying: if the computer chose odd--and maybe that is the easiest way to do it. ifthey both chose odd, then that means that

the number is even. if one chose odd and onechose even, then it is an odd number. i guess that is easier than i was thinking. so we will need to decide who wins the round.before we can decide who wins the round... we need to decide… or can we do it all at the same time? i'mgoing to come back and watch this later and realize that there was a much better way todo it, but when it is in the pressure of the moment and trying to do this...i'm trying to decide which way will best with this. what i am going through in my mind iswe need to be able to say whether the number was odd or even now. in our pseudocode wehave just said if the user picked odd and

the number was odd. the question i'm havinghere is how did we determine whether the number was odd or even? we can make a messy if statement.we can say... so i picked the outcome to be an odd number.the user throw was odd, the computer throw was even, then we do get an odd number, theni can say, the user wins... and i can go in and increment the number ofcomputer wins. why do i keep wanting the computer to win? the user wins. we've got this variablefor userwins up here. so i could say: take whatever was in that variable, userwins, andincrement it by 1. this is one way i could do it, but then i would have to do it foreach scenario. if the user chose even and the computer chose odd, it would still bea user win.

let me think this over for a second, and iwill come back in the next video and try to work this all out in what might be the bestway to do this. spencer out.

Leave a Reply