/** * * @title:SwitchCase.java * @Package:com.you.hbxs.model * @Description:<h3>一句话描述功能</h3> * @author: * @date:2016-2-24下午11:01:48 * @version V1.0 * */ package com.you.hbxs.model; /** * * 项目名称:HBXS * 类名称:SwitchCase * 类描述: * 创建人: * 创建时间:2016-2-24下午11:01:48 * 修改人: * 修改时间:2016-2-24下午11:01:48 * 修改备注: * @version V1.0 * */ public class SwitchCase { public static int switchCase(int number) { int sum = 0; switch(number) { case 1: sum = sum + number; case 2: sum = sum + number*2; case 3: sum = sum + number*3; case 4: sum = sum + number*4; } return sum; } /** * * 方法名:main * 方法类型:SwitchCase * 参数:@param args * @return :void * @throws */ public static void main(String[] args) { int count = switchCase(2); System.out.println("count="+count); } }
结果为:
count=18
说明:
main函数中调用静态方法switchCase,当number=2,switch-case语句执行到case 2,这时sum=4;由于switch-case语句后没有break,继续执行case3,但是number=2,sum=4+2*3=10;最后,执行case 4,number=2,sum=10+2*4=18
时间: 2024-10-18 06:28:55