ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《卷轴教程之C++入门》#10 条件语句二 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《卷轴教程之C++入门》#10 条件语句二

Using Conditional Statements in C++ Part 2

好了 我们现在继续 开始编辑源代码
All right. So let’s go ahead and start putting in our source code.
先从#include开始
And we ‘ll start with our Pound Includes. We have
(输入)“#include
(insert)”#(pound) include
还需要输入 “#include
And we need to have “#(pound) include “.
因为我们将要用到一些变量和字符串
OK. Since we are gonna have some variables and literals of type string
所以需要输入“using namespace” 空格 “std” 分号
and then “using namespace” space “std(standard);(semicolon)”.
现在我们要开始编写main函数了
OK. So now we’re ready to start typing in the main function.
我们回车几行 输入 “int main”
So let’s come down a couple of lines and type in “int main”,
左括号 右括号
open paren, close paren,
回车 输入左大括号
and come down a line and type in an open brace.
如果你想的话 你可以全选这里的伪代码
And then if you want to, you can select the rest of the pseudocode here
然后用“Tab”键缩进
and we’ll just tab it over.
选中伪代码 按下“Tab”键
Once you select it, you can hit the tab key you know
伪代码全都被Tab键缩进
tab all of that pseodo code over,
这些代码全都在main函数里面
since we want to have all of these inside the main function.
在最下面 输入右大括号
And also at the very bottom here, you can put in a close brace.
好啦 我们现在要在main函数里编写内容了
All right. So now we already start typing in our main
回到下面 在这里
function here. So let’s come down and
编写“获取种子颜色”的内容
do Get the seed color.
为了获取种子颜色 我们需要一个变量来存储
So in order to get the seed color or the color, we need to have a variable just capable of holding
用户输入的任何颜色
whatever color is typed in by the user.
所以 我们声明一个 string变量 命名为“seedColor”
So, we’ll declare a string variable, and we’ll call it seedColor.
对它赋值为空
And we’ll set it to the empty string.
这样就完成了变量声明
So we get our decleration statement here.
我之前没有提到的一点就是
Now, one thing I wanna point out that I haven’t pointed out before
在任何时候
is that whenever we
声明一个变量 变量名由多个单词连结组成的时候
declare variables and there are multiword variable name,
命名习惯 或者说我们当前用的命名习惯就是
the convention is, or the convention that we’re using,
第一个单词小写 之后的单词
is to have the first word lowercase and any subsequent words
全都首字母大写 这种叫做 驼峰命名法
they’ll be what we call camel case, meaning the first letter’s capitalized,
其他的字母都是小写
and then the rest of the letters are lowercase.
这样做有助于简明地阅读
And this actually aids and been able to see
我们当前使用的多单词组成的变量名
that we do in fact have a multiword variable.
你可以看到单词间明显的区分 因为“seed”后面是大写
So you can actually see the delineation cause we have the upper case seed there.
当然 这只是一个命名习惯 并不是编译器要求的规范
Again this is just a convention, it is not required by the compiler,
你可以直接把它命名成“x”
so you could name it x, but that’s not really
但这样体现不了这个变量的意义或者作用
indictive of what this variable does or means.
我想要一个变量来存储种子颜色所以我就把它命名为“seedColor”
So we wanna have a variable to hold the seed color, so I wanna name it seedColor.
好了
OK.
接下来我们给用户一个操作提示 利用“cout”输出显示
So then once we have that, we can then have a prompt to the user using our “cout”.
输入“cout” 输出内容是“Enter the seed color”
So do “cout” and say, “Enter the seed color”.
加上 圆括号 输入“red or blue”
And we’ll put in parentheses here, “red or blue”.
假设用户行为良好 按照提示操作
And we’ll assume that we have good users right now. They type in
输入红色(red)或蓝色(blue) 而不是紫色、绿色、汉堡或其他的什么东西
red or blue. They don’t type in purple or green, or hamburger, or something like that.
现在有了提示 我们可以读取用户在键盘上的输入
So once we have the prompt there, then we can read it read in whatever they type on the keyboards
用“cin”标准输入流
So you could use a “cin”,
我们要做的就是 把用户输入的值存储到变量“seedColor”里
and then we wil want to do is store whatever they type in into the variable “seedColor”.
以上都是基础简明的操作 没有什么新的知识点
And that’s basically it, so this is nothing new. We’re just using
都是我们之前的程序中学过的东西
some of the things we have learned from our previous programs.
接下来 获取温度 就获取温度而言
So, Get tempreture. And in the case of tempreture, we’ll
我们要得到的是某种数值
want to have that is some sort of numeric value
因为我们要将它和别的数值进行比较
cause we will want to compare it to other numeric values.
所以我要把它存储到一个int整型变量
So we will store this into an integer value since we’re
我想你可以把它存到一个double型变量里
I guess you could store it into a double.
这里温度可能有小数部分
Our tempreture may have a fractional part. For right now, we are gonna say that our
不过现在 我假定 温度没有小数部分
tempreture is not gonna have a fractional part. So we’ll just put in
所以我把它放到整型变量里 输入“int temp”
an integer variable. So we do int, temp,
赋值为0 这样就初始化为0了
and set it to zero. So we initialize it to zero,
然后cout输出
and then we’ll do cout.
输入“Enter the temperature”
We’ll say, “Enter the temperature”.
或许在这可以输一个“F” 表明是华氏温度
Maybe we’ll put “F” here indicate Fahrenheit.
尽管我们并没有真正关注单位
Even though we are really not keeping track of units.
单位是很重要的 但在这个程序中
Units are important for, but for this program we are gonna
我会忽略 温度的相关单位
ignore the units related to temperature, and we just
只是暂且把“F”写上去
add that in there for fun, I guess.
下一句 用“cin”和“>>”输入操作符
So then we’ll do “cin”. And then use extraction operator
输入变量名 这个变量存储用户输入的温度值
And type in the variable name, where we wanna store the temperature that they type in,
接下来要做的就是 获取土壤湿度
And then, the next thing we need to do is Get the soil moisture.
一样也是用 string字符串变量
This is also gonna be a string variable.
命名为“soilMiosture”
We’ll just call it “soilMoisture”.
那么 把它赋值为空 或是初始化为空
Ok and we’ll assign it to the empty string or initialize it to the empty string
用“cout”输出 内容是
And “cout”. And then we’ll say,
“请输入土壤湿度”
“Enter the soil moisture”.
设置选择为 湿 或 干 写在小括号里
And we’ll put “wet or dry”, and parentheses here,
这是提示用户 他们需要输入什么内容
sort of prompting the user what we expect them to enter in.
好啦
All right.
如果用户执行了一些我们意料之外的输入
Now if the user goes in and enters in something besides
这种情况 我们的程序目前还没法处理
what we are expecting, our program is not gonna be able to handle that.
至少现在没法处理
At least not what the way we write it now.
不过在之后的视频 我们会回过头来
We may come back and change… later videos will
进行修改 检查输入的有效性
modify this, what actually does the validation of the input.
好了 这里要写入变量—“soilMoisture(土壤湿度)”
So see when you have the variable “soilMoisture” here,
那么 用户进行了输入之后
OK. So now, once we have our input,
有了种子颜色、温度和土壤湿度的值
for seed color, tempreture and soil moisture,
我们可以做一些判断 这样就涉及到了一些新的语法
We can start makimg some decisions. So we are getting into some new syntax here.
目前为止 在我们的工程1、工程2、工程3、工程4中
So so far, in our programs, program one, two, three and four,
所有的语句指令都是一条接一条 顺序执行的
All over our instructions have been one right after the other, in sequence.
现在 我们要实现的是
So now we’re looking at having some sort of
在多个选项里 进行判断选择
choice alternative, we’re making decisions.
基于可能的选择 我们会有一系列的分支语句
And we’ll branch off to one set of code based off of what decision is made here
也就是 如果种子颜色是红色 我们会有这一些选项
So if it’s red we’ll look at these set of choices and if it’s blue
如果是蓝色 我们就看下面的这一部分选项
we’ll look at these set of choices down here.
那么 检查某项是否为真 的语法就是
So the syntax for testing to see if something is true,
一个简单的 “if”语句
is just a simple if.
和这里的伪代码很像 我们输入“if”
So almost like our pseudocode, we can say if,
使用 变量“seedColor(种子颜色)”
and use our variable called “seedColor”,
用等号来判断比较
and check, using our Relational operator Equal,
请注意 进行比较要用“==”两个等号
So, remember, to test for equivalence we have to use “equal equal”s.
不能只用一个等号 一个等号是赋值 两个等号是比较
It’s not just a single equal. Single equal means assignment, double equal means equavalence.
这里 我们先看 种子颜色是否是红色
So here we wanna see if the seed color is red.
你要确保把 red(红色) 写到双引号里
So make sure you put red inside of the
否则编译器不认为它是个字符串
double qoute, otherwise it will not be seen as a string.
这里 我们就是 将一个字符串
So we are trying to compare one string
这个字符串 是一个名为“seedColor”的变量
which is a one variable here, called “seedColor”, which is of type string,
将它和一个 字符串字面量 进行比较 这个字面量是“red”
to a string literal. And the string literal says “red”.
好了 在这里输入左大括号
All right. So I’m gonna put open brace.
然后在这里输入右大括号
And then I’m going to put close brace right above
放在伪代码“if blue seed”的上方
the pseudocode for “If blue seed”.
那么 接下来我们就要
So what we’re trying to
在这里定义if语句的执行体
define here is the body of the if statement.
左大括号和右大括号中间的就是执行体
So the body is defined by the open brace and the close brace. We’re saying that if
我们设定 如果 种子颜色是红色
the seed color turns out to be red,
我们就会测试编写这一系列的操作
then we wanna look at testing these set of actions here,OK.
另外一步要做的 就是下滑到这儿 测试 如果
And the other thing that we wanna do is come down below and test to see if
如果 种子颜色等于蓝色
Say if seed color is equal to blue.
我们会在这下面执行这些操作
We’ll do these set of actions down here.
在下面 确保我们输入了右大括号
So come down to make sure we have our close brace.
关于Eclipse有一个好处 它实际上把我们的大括号排列了起来
And the nice thing about Eclipse it actually aligns up our braces.
有了左花括号和对应的右花括号 它们应该会自动对齐
If you have an open brace and a close brace, it should automatically align those braces for you.
在这里 我需要指出 关于if语句的另一个要点
The other thing I should point out about these if statements here,
if语句需要判断
they have to test a
一个布尔值 所以不管圆括号里是什么内容
Boolean expression here. So whatever we have inside parentheses,
“if” 左括号 某某内容 右括号
If, parenthesis, something, close parenthesis,
括号里面的内容必须是一个布尔表达式
that thing inside the parentheses has to be a boolean expression,
即 最终括号里的返回值是 真/假
meaning it has to resolve down to a true/false value,
但不一定必须是“真/假”值 也可能是一条语句
but doesn’t resolve down to a true/false value, then we’ll end up with a syntax here.
好了 现在我们来处理 种子颜色是红色时的操作
All right. So I’m gonna just focus on the red for right now.?
如果温度大于或等于75(度)
If the tempreture is greater than or equal to 75.
查看这里的源代码 C++代码
So check out how this source code here. The c++ code
和实际上的伪代码是十分相似的
is very similar to the actual pseudocode.
所以 如果 温度
So if, temp,
大于或等于75(度)
is greater than or equal to 75,
我们就会执行这一系列的操作
then we want to do these set of actions,
把右大括号放在这里
Put the close brace at here.
否则 我们就执行这些步骤
Otherwise we do this set of actions.
我们执行“否则”操作 的方式是
And the way we go about doing otherwise
在这里使用“else”关键词
is use the keyword else.
输入“else” 左大括号
So we say else, open brace,
下来几行 在这里输入 右花括号
And then we come down here and type in the close brace, and make sure
确保 包含了 输出蘑菇 的伪代码部分
it’s inclusive of our pseudocode for outputting mushroom.
在这里 对应伪代码 用了关键词“else”
So we could have use else here for
但仅在 “否则”的情况下使用
our pseudocode, but I just use the term otherwise.
所以是这么说的 如果我们有一个if语句 而且if语句不成立
So all that saying is, if we have an if statement, and that if statement fails to be true,
并且我们只有两个选择
and the only other alternative we have
在这个实例里 另外一个选择是
so in this case the only other alternative is,
如果这是假的 如果温度小于75(度)
if that’s not true, if the tempreture is not greater than or equal to 75,
并且我们需要确定我们有一个红色种子
and we have to determine that we have a red seed,
那我们就知道了它最后会是一个蘑菇
Then we know we end up with a mushroom.
所以我们不需要另一个条件
So we don’t need to have another condition here.
你不需要到那里并写上
So you don’t have to go in here and write
(输入)“当温度小于75”
“if tempreture is less than 75”.
因为这是仅有的另一个——哦 我忘记小括号了
’cause that’s the only other – oops, I forgot my open paren.
我们不需要这样表达
So we don’t have to go in there and write this expression.
我们可以这么做 在这里这样表达在语法上是没问题的
Now we could do that. There’s nothing wrong syntactically with doing that expression there.
这和我们之前的表达效果基本一样
It’s basically doing the same thing that we had before.
但不需要通过检验来确定温度是否小于75(度)
But there’s no reason to test to see if the tempreture is less than 75,
因为如果我们失败了 如果这是假的
because if we fail here, if this turns out to be false,
那么 我们就知道温度低于75(度)了
then we know the tempreture is less than 75.
所以我们可以只说“else” 好了
So we can just say else. OK.
好了 那如果土地是湿的
All right. So what about if the soil is wet,
那我们就可以像对种子颜色一样对其进行测试
so we can do a similar test as we did before with the seed color,
那这一次我们就要判定“soilMoisture(土地湿度)”
and this time we’ll do “soilMoisture”
并测试以确认土壤湿度是否为“wet(湿)”
and test to see if the soil moisture is equal to “wet”.
如果这是真的
If that turns out to be true,
那我们就要做输出语句
then we’ll do our output statement,
那如果“soilMousture”是“dry(干)”
and if the “soilMoisture” turns out to be dry,
我们就要做这个输出语句
we will do this output statement.
好了
OK.
事实上 你可以做相同的事——做两个if语句
And you could actually do the same thing here as opposed to doing two if statements,
但事实上你可以写一个if语句,写一个else语句
you could actually do an if, and then you can do an else,
假设用户正在输入“wet(湿)”或“dry(干)”
assuming that the user, you knew, that the user was inputting wet or dry,
如果没输入“wet”就会输入“dry”
so if they didn’t input wet then you knew that they input input a dry.
所以如果你想 我们就可以把它变成else语句
So we could in fact make this an else statement if you wanted to.
我会暂时把这个if语句留在这里,或许我们待会儿回来再把它换掉
I’m going to leave it in here as a, as an if statement for now, maybe we’ll come back and change it,
为了输出 我们应该写上“cout”并输出“会长出向日葵”
so in order to do the the output, we would do “cout” and then just output “A sunflower will grow”.
我会说“会长出向日葵”
I will say “A sunflower will grow.”
下面我们要“cout”并写(会长出)蒲公英
And down here we’ll do “cout” and do a dandelion, let’s see.
好了 有了 我们也许需要一个换行符
All right, so we have that, we may want a new line character in there
所以我要回去加一个换行符
so I’ll going to go back in here and put a new line character.
好了 有了
All right, so we have that.

发表评论

译制信息
视频概述

本届课程介绍了前一节课问题四的代码解决方案。

听录译者

BinWeber

翻译译者

乐然君の个人微信[Smart]

审核员

L

视频来源

https://www.youtube.com/watch?v=iBsc1psJb60

相关推荐