What did you get for synchronization? Did you write down "iteration" like I did? And then what gets iterated? I wanted to write that it prints out "One..." on the first iteration, then "Two..." on the second, but there's a problem with that. We can set up a variable (fourth of the Five Concepts in the table below) and add +1 to it on each iteration, but that only gets us "1..." and "2..." but not "One..." and "Two..." So I settled for a sequence of outputs.
And we can't have it just print the lines in rapid succession, the computer goes so fast, they would all print in less than one second. Of course it's only fake anyway, but the computer does not tell us what the program should do, we tell it. However, we have limits that we must work within, and sometimes we need to be a little creative. Most programming languages give you some kind of "wait" or "delay" operation, but I don't know where that fits in the Five Concepts table. I guess if you look under the hood, when you say "wait 5 seconds" what it really does is sit there in a tiny iteration, reading the clock chip until the five seconds are up. So maybe it's input or iteration, or some combination of the two. Whatever.
Most programming languages (including Java, when you get to it) give you some kind of random number generator. It's not truly random -- the computer does exactly what you tell it to do, exactly, not randomly -- usually they do something with a non-linear counter that repeats after a couple billion times, but Tom's Kitchen computer doesn't even do that. You could write into your design "pick a random number between 0 and 2," and then when we get there, I'll tell you how to get pretty close to random.
In fact, I'll tell you now: take the time of day and divide it by three and use the remainder:
variable now = the secondsBecause the actual time this runs is dependent on when the user started the program, and after that on how long it took them to give their input, it's really hard to get that very reliably accurate to a small number of seconds, so it works pretty good.
variable temp = now/3
whole temp {discards the fractional part, so it's a whole number}
variable comPlay = now-3*temp {0 or 1 or 2}
User input is pretty simple, you just need to know how to spell it. If you already did one of the other programs (Calculator or Guessing Game) then you already have done that. Otherwise, this is the line you need:
input variablename 1You need a variable (fourth item in the Five Concepts list in the table below), which you need to give to give it a reasonable name (obviously not "variablename" -- I chose "humPlay" which is short for "human play" but you can name it anything you like), and the "1" tells the computer that you want one letter only, so the user does not need to press Enter.
That gets you a capital letter in your variable. Maybe you already did the scoring part, but if not, think about it and put your ideas down in writing, then turn the page.
Five Basic Concepts Sequence Iteration Conditional Variables Input/Output
<<Previous | ToC | Next >>
[2021 May 12]