[Java基础教程]第七章-Java流程控制
小明帮妈妈打酱油,临出门了妈妈就会说:"如果有瓶装的就买个瓶装的,否则就买一个袋装的。"这种场景在计算机中非常常见,一起来看下怎么用Java实现吧。
publicstaticvoidmain(String[] args) {
booleanbottled = true;
if(bottled) {
System.out.println("打的瓶装酱油");
} else{
System.out.println("打的袋装酱油");
}
} 上面实现的就是最常用的if,else语句的实现。
使用面向对象的方式,重构上述代码:
packagecom.sunhaojie.learntest.seventh;
/**
* @ClassName Shop
* @Description 售卖酱油的店铺
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 上午11:00:10
*/
publicclassShop {
/**
* 瓶装酱油
*/
privateintbottled = 1;
/**
* 袋装酱油
*/
privateintbag = 2;
publicvoidsell(inttype) {
if(type == bottled) {
System.out.println("销售一瓶酱油");
} else{
System.out.println("销售一袋酱油");
}
}
}
publicstaticvoidmain(String[] args) {
Shop shop = newShop();
shop.sell(1);
}上面的代码新创建了一个Shop对象,并新增了一个sell方法,可以根据入参type销售不同类型的酱油。其中"type == bottled"是比较运算符,"=="两边的值相同返回true,否则返回false。
假如该店不仅只有瓶装和袋装两种类型,还包括纸袋装,自带瓶,等类型,上面的逻辑就被重构为:
publicvoidsell2(inttype) {
if(type == bottled) {
System.out.println("销售一瓶酱油");
} elseif(type == bag) {
System.out.println("销售一袋酱油");
} elseif(type == paperBag) {
System.out.println("销售一纸袋酱油");
} else{
System.out.println("销售一次无容器酱油");
}
}上面的代码看是不是比较累赘,并且不容易阅读,如果再加几种类型,有会增加一些if,else判断。Java对于这种场景有一个更好的支持:
publicvoidswitchSell(inttype) {
switch(type) {
case1:
System.out.println("销售一瓶酱油");
break;
case2:
System.out.println("销售一袋酱油");
break;
case3:
System.out.println("销售一纸袋酱油");
break;
case4:
System.out.println("销售一次无容器酱油");
break;
default:
System.out.println("没有这种类型");
break;
}
}switch语句可以实现if else的效果,但是更优雅。其中注意点:每个case:后的语句执行了,如果没有"break;"语句,后面的所有case都会被执行,直到遇到"break;"或者代码结束。这种场景比较少见,大部分使用的是if else语句。
小明家是开饭店的,一次要买20袋酱油。程序实现,可以调用shop.sell(1);20次,但是这样太繁琐了,Java中有while和for两种方式支持循环。
publicstaticvoidmain(String[] args) {
intcount = 0;
Shop shop = newShop();
while(count < 20) {
shop.sell(1);
count = count + 1;
}
for(inti = 0; i < 20; i++) {
shop.sell(2);
}
}先看一个比较运算count < 20,左边的小于右边为true,否则为false;还有一些和小于"<"类似的,大于">",大于等于">=",小于等于"<="。
while语句,括号()中的语句为true,执行{}中的语句,直到()中的语句为false;
for语句,和while类似,但是代码更紧凑。语句" for (int i = 0; i < 20; i++)"中,"int i = 0"仅在首次执行,满足"i < 20;"执行{}中的语句,并且执行i++;for()内的语句分别对应了while的"int count = 0;",count < 20,count = count + 1;
小明要去买20袋酱油,每瓶10块钱,但是他只有100块钱,请问小明买到了多少酱油呢?代码如下:
publicstaticvoidmain(String[] args) {
intcount = 0;
intmoney = 100;
Shop shop = newShop();
while(count < 20) {
if(money < 10) {
break;
}
shop.sell(1);
count++;
money = money - 10;
}
count = 0;
money = 100;
while(count < 20&& money >= 10) {
shop.sell(1);
count++;
money = money - 10;
}
}第一个while循环中有一个if判断,表示money<10的时候进入{},执行bread;语句,while循环结束。类似的有continue;语句,结束本次循环,while语句仍然可以继续执行。
第二个while循环中"count < 20 && money >= 10",出现了一个新的运算符,与"&&",这个符号的意思是&&左右两边分别为true时返回true,否则返回false。类似的运算符是"或||",||左右两边都为false时返回false,否则存在一个或者两个为true返回true。
目前一共介绍了3种运算符,数学运算符(+-*/%),比较运算符(<,>,<=,>=)和逻辑运算符(&&,||),基本能满足日常开发。当一起使用的时候优先级是:数学运算符>比较运算符>逻辑运算符,"()"大于数学运算符。
流程控制:分支流程if else,switch case,循环流程while和for,以及中断循环的语句break,和continue。
小练习:求打印输出5000以内的质数;
质数的定义:
质数(prime number)又称素数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数;否则称为合数。
根据算术基本定理,每一个比1大的整数,要么本身是一个质数,要么可以写成一系列质数的乘积;而且如果不考虑这些质数在乘积中的顺序,那么写出来的形式是唯一的。最小的质数是2。
课程中的代码如下:
packagecom.sunhaojie.learntest.seventh;
/**
* @ClassName DaJiangYou
* @Description 实现打酱油程序
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 上午10:52:01
*/
publicclassDaJiangYou {
/**
* @Title main
* @Description 实现打酱油程序
* @param args
* @return void
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 上午10:52:01
*/
publicstaticvoidmain(String[] args) {
booleanbottled = true;
if(bottled) {
System.out.println("打的瓶装酱油");
} else{
System.out.println("打的袋装酱油");
}
}
}
packagecom.sunhaojie.learntest.seventh;
/**
* @ClassName Shop
* @Description 售卖酱油的店铺
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 上午11:00:10
*/
publicclassShop {
/**
* 瓶装酱油
*/
privateintbottled = 1;
/**
* 袋装酱油
*/
privateintbag = 2;
/**
* 纸袋酱油
*/
privateintpaperBag = 3;
/**
* 没有容器
*/
privateintnoContainer = 4;
publicvoidsell(inttype) {
if(type == bottled) {
System.out.println("销售一瓶酱油");
} else{
System.out.println("销售一袋酱油");
}
}
publicvoidsell2(inttype) {
if(type == bottled) {
System.out.println("销售一瓶酱油");
} elseif(type == bag) {
System.out.println("销售一袋酱油");
} elseif(type == paperBag) {
System.out.println("销售一纸袋酱油");
} else{
System.out.println("销售一次无容器酱油");
}
}
publicvoidswitchSell(inttype) {
switch(type) {
case1:
System.out.println("销售一瓶酱油");
break;
case2:
System.out.println("销售一袋酱油");
break;
case3:
System.out.println("销售一纸袋酱油");
break;
case4:
System.out.println("销售一次无容器酱油");
break;
default:
System.out.println("没有这种类型");
break;
}
}
}
packagecom.sunhaojie.learntest.seventh;
/**
* @ClassName DaJiangYou2
* @Description 打酱油2
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 上午11:04:22
*/
publicclassDaJiangYou2 {
/**
* @Title main
* @Description 打酱油2
* @param args
* @return void
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 上午11:04:22
*/
publicstaticvoidmain(String[] args) {
Shop shop = newShop();
shop.sell(1);
shop.sell2(3);
shop.switchSell(4);
}
}
packagecom.sunhaojie.learntest.seventh;
/**
* @ClassName DaJiangYouWhile
* @Description while循环打酱油
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 下午2:38:45
*/
publicclassDaJiangYouWhile {
/**
* @Title main
* @Description 循环打酱油20瓶子
* @param args
* @return void
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 下午2:38:45
*/
publicstaticvoidmain(String[] args) {
intcount = 0;
Shop shop = newShop();
while(count < 20) {
shop.sell(1);
count = count + 1;
}
for(inti = 0; i < 20; i++) {
shop.sell(2);
}
}
}
packagecom.sunhaojie.learntest.seventh;
/**
* @ClassName UnionCompare
* @Description 或和且运算
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 下午3:39:29
*/
publicclassUnionCompare {
/**
* @Title main
* @Description 或和且运算
* @param args
* @return void
*
* @author sunhaojie 3113751575@qq.com
* @date 2016年1月29日 下午3:39:29
*/
publicstaticvoidmain(String[] args) {
intcount = 0;
intmoney = 100;
Shop shop = newShop();
while(count < 20) {
if(money < 10) {
break;
}
shop.sell(1);
count++;
money = money - 10;
}
count = 0;
money = 100;
while(count < 20&& money >= 10) {
shop.sell(1);
count++;
money = money - 10;
}
}
}建议继续学习:
- 使用线框图来简化你的产品设计流程 (阅读:3610)
- 产品UI设计流程 (阅读:2970)
- 研发流程中与其他岗位协作效率的提升 (阅读:2663)
- 产品UED流程及交付物 (阅读:2602)
- 也谈前端开发流程 (阅读:2508)
- 线下项目工作流程(归纳篇) (阅读:2481)
- 关注前端开发流程 (阅读:2245)
- 产品评审那点事 (阅读:2303)
- 线下项目工作流程(分析篇) (阅读:2282)
- 说说产品开发到发布过程中的问题 (阅读:1967)
扫一扫订阅我的微信号:IT技术博客大学习
- 作者:sunhaojie 来源: 孙豪杰的博客
- 标签: 流程
- 发布时间:2016-02-20 14:17:52
-
[897] WordPress插件开发 -- 在插件使用 -
[135] 解决 nginx 反向代理网页首尾出现神秘字 -
[56] 整理了一份招PHP高级工程师的面试题 -
[54] Innodb分表太多或者表分区太多,会导致内 -
[53] 如何保证一个程序在单台服务器上只有唯一实例( -
[52] 全站换域名时利用nginx和javascri -
[52] 分享一个JQUERY颜色选择插件 -
[52] 用 Jquery 模拟 select -
[52] CloudSMS:免费匿名的云短信 -
[52] 海量小文件存储
