The problem is, I want to know if, in fact, I've got something that's not of the form I expect.
问题是,我希望知道如果输入的。
By putting it in a function bug if I find a bug and I change my program I can just run the function again.
把我输入的值放到一个函数里,如果以后我在程序中发现了一个,并对程序进行更改的话,我可以直接。
As an editor, I'm constantly taking all these inputs and deciding on that one or that intersection of a few that makes sense for what we're doing.
作为一个编辑,我不断地吸收这些输入程序,决定哪一点或是哪一部分,是值得去做的。
Now, if I've asked the user for a string and then a moment later I ask the user for another string, well, they're going to end up in different locations in memory just by nature of get string.
现在,如果我要用户输入一个字符串,一会儿之后我要用户输入另一个字符串,他们最终是在,不同的内存地址中。
The next I knew, she had literally typed my password.
然后她就真的帮我输入了"密码"
Or maybe I could donate bone marrow and those stem cells could be given to other people in the same way that blood can be given to other people by matching and making sure that immunologically my cells were compatible with you; there's a lot of interest in that.
或者我可以捐献骨髓,而那些干细胞,可以使用和输血同样的方法,通过配型来确认我的免疫细胞和你的匹配,从而把我的干细胞,输入给你,这里面有很多有趣的东西
It could be, preconditions I want to have on input It could be, explanations of specific things you're doing.
也可以是我对输入的预期是什么,也可以是,对代码,正在进行的特定操作的解释。
if I could type, in order to just to get into that, in order to get into the habit.
如果我可以输入,为了不在这里陷入,为了养成这个习惯。
Obviously the algorithm is likely to depend on the size of the input, so this is not a great idea.
很明显这个算法是与我的,输入的大小相关的,因此这不是个好方法。
But if it is not, it's going to print out a message here saying, you screwed up, somewhat politely, and it's going to go back around. So it'll just cycle until I get something of the right type.
但是输入不是浮点数的话,我这里就会显示一个消息说,你搞砸了,该稍微有点礼貌吧?,然后我们继续执行循环,一直到得到正确类型的输入。
2 2 I can move 9 down, I can move 12 down, I can move 2 down, God and I can cheat by entering God mode and if I hit God and hit Enter well now the problem set solves itself for me.
我可以将9移下来,移动,而且我也可以输入无敌模式,如果我输入,并回车,然后此问题将会自动开始,解决。
OK. I get the two values in, what do I need to do, well, you sort of know that, right?
好,有了这两个输入值了,我接下来将要做的,你知道对不对?
I could generalize this pretty nicely. Imagine the same kind of idea where I want to simply say I want to take input of anything and try and see how to make sure I get the right kind of thing. I want to make it polymorphic.
对吧,我试着输入浮点数,我能更好的一般化它,想象下类似的想法,我需要输入些什么,然后看看输入的是否正确,我想使他更多元化些。
OK, in fact if we look at this, if I say, print of p, it prints it out in that form.
好,实际上如果,我们来看看这个,如果我输入,p的显示。
The second thing is, what do I print out if I actually don't get the right input in?
第二件事是我没得到,正确类型的输入时输出什么?
You notice what it did, it printed out that little, right up here I'd said enter a number and that's what it's printed out, so I can enter a number and it prints it out.
注意这个程序做了些什么,显示出了指定值,然后显示需要输入个数字,然后程序会显示这个数字,我输入一个数字然后程序显示了它。
I give it some character. It gives me back an integer representing. It looks weird.
我输入一些字符,程序返回给我一个整数的表示。
And now notice as promised, I'm using a Mac here so my cursor is not actually blinking, but the cursor is waiting there for me.
现在注意正如所承诺的,这里我用的是苹果电脑,所以我的光标不会闪烁,但是光标在那里等我输入。
In this case, I'm going to get back the answer 3 F-O-O because the user typed in F-O-O, but wait a minute, what's with this?
假若这样,我得到了答案3,因为用户输入了,等一下,用这个干吗?
And I could take that information, type it up into a program, make some assumptions about speed and direction and what routes they're going to take and actually watch the ambulance go to St. John's Mercy.
我得到这条信息之后,将它输入程序里,假设它们的速度和方向,以及它们应走哪条线路,就能看到救护车进入圣约翰医院。
I'm going to have it take in as input how many legs I got, how many heads do I have, and I just want to write a little loop.
这就是解决问题的思路,我将要输入我有多少条腿,以及我有多少只头。
If I'm expecting mostly numbers in, I can certainly try and do the coercion.
数字输入,我能够肯定的来做强制转换。
First of all, that depends on which input I've picked. All right?
首先,这方法的结果跟我选中的,输入有关对不对?
So I predicted what answer I'm expecting to get.
然后我输入我的计划输入到要。
Now I'm typing dot slash as a convention.
现在我输入。
If I get 0 I return 1.
如果我输入的值是0我会返回。
But there's an important thing going on here, if I in fact wanted to combine those into a string I should have told the machine to do that, and I can do that, by explicitly saying, take that, which is a number, then add it to that string.
但是这里有个很重要的事情,实际上想要把这些加到一个字符串里面去,我应该告诉计算机去做这个,我应该这么做,通过明确的输入,也就是把这个对象,一个数字,然后把它加到一个字符串里面去。
That's the question we're going to try and address. If we can do this, this is good, because first of all, it removes any questions about what machine I'm running on, it's talking about fundamentally, how hard is this problem, and the second thing is, it is going to do it specifically in terms of the input. Which is one of the things that I was worried about.
这是一个我们接下来要去,尝试和解决的问题,如果我们能做到的话会很不错,因为首先,它解决了结果可能和我,运行的计算机有关系的问题,因为这从根本了告诉了,我们这个问题的复杂度,第二件事情,就是这种方法可以依据输入的大小来计算,而这正是我最担心的地方。
OK, says it says enter a float. I give it something that can be converted into a float, it says fine. I'm going to go back and run it again though. If I run it again, it says enter a float.
好了,看到他说输入一个浮点数,我输入它可以转换为浮点数的值,那没问题,我回过来再运行一遍,如果我再运行一遍。
应用推荐