我正在教/帮助学生编程。
我记得当我开始时,以下过程总是对我有帮助;它看起来很直观,我想知道其他人是否有类似的方法。
- 阅读问题并理解它(当然)。
- 识别可能的“功能”和变量。
- 写下我将如何一步一步做(算法)
- 把它翻译成代码,如果有什么你不能做的,创建一个为你做的函数并继续前进。
随着时间的推移和实践,我似乎已经忘记了从问题描述到编码解决方案是多么困难,但是,通过应用这种方法,我设法学习了如何编程。
所以对于像这样的项目描述:
系统必须根据以下规则计算商品的价格(规则的描述......客户、折扣、可用性等......等等)
我的第一步是了解问题所在。
然后识别项目、规则、变量等。
伪代码类似于:
function getPrice( itemPrice, quantity , clientAge, hourOfDay ) : int
if( hourOfDay > 18 ) then
discount = 5%
if( quantity > 10 ) then
discount = 5%
if( clientAge > 60 or < 18 ) then
discount = 5%
return item_price - discounts...
end
然后将其传递给编程语言..
public class Problem1{
public int getPrice( int itemPrice, int quantity,hourOdDay ) {
int discount = 0;
if( hourOfDay > 10 ) {
// uh uh.. U don't know how to calculate percentage...
// create a function and move on.
discount += percentOf( 5, itemPriece );
.
.
.
you get the idea..
}
}
public int percentOf( int percent, int i ) {
// ....
}
}
您是否采用了类似的方法?..有人教您类似的方法还是您发现了自己(就像我一样:()