if else 的妙用 —— 顾客视角

2023-05-04,,

if (storedCash % 100 != 0) {
System.out.println("请输入100的倍数!!!");
} else if(storedCash % 100 < 0) {
System.out.println("请输入正数!!!");
} else if(storedCash > MAX_CASH - this.cash) {
System.out.println("对不起,您的存款已达到本ATM能容纳的上限!!!");
} else{
this.cash += storedCash;
this.theUser.setAccount(this.theUser.getAccount()+storedCash);
System.out.println("恭喜您,存款成功!!!");
}

以上是ATM机存款中的一段代码,这里存在一个种重要的思维方式——顾客视角

如果我们以平常程序猿的思维,会将关注点集中在下面代码上,即ATM机内现有现金的增减情况及用户账户中余额的增减情况上。于是可能会写出三个if和else嵌套的代码。

            this.cash += storedCash;
this.theUser.setAccount(this.theUser.getAccount()+storedCash);
System.out.println("恭喜您,存款成功!!!");

但是,如果我们换个角度思考,即站在顾客的角度上思考,关注点就变为了System.out.println();中打印出的内容上,写法就会简单许多。

在以后的编程之路上,顾客视角 是必不可少的。我们写出来程序其根本是为顾客服务的,一个程序写得好不好,关键在于用会体验好不好。

if else 的妙用 —— 顾客视角的相关教程结束。

《if else 的妙用 —— 顾客视角.doc》

下载本文的Word格式文档,以方便收藏与打印。