package xmeiju;
/*
* 需求:定义一个方法,该方法的作用是计算两个int类型数据的商。
* 如果计算成功则该方法返回1,如果执行失败则该方法返回0
*
* 程序执行成功,但是该程序存在风险,分析:存在什么风险?
*
* 程序中的问题能在编译阶段解决的,绝对不会放在运行期解决。所以以下程序可以引入"枚举类型"。
*/
public class EnumTest01 {
//入口
public static void main(String[] args) {
int a = 10;
int b = 0;
int retValue = divide(a,b);
if(retValue == 1){
System.out.println("成功");
}else if(retValue == 0){
System.out.println("失败");
}
}
//实现需求
public static int divide(int a, int b){
try{
int c = a / b;
return 1; //成功
}catch(Exception e){
return 0; //失败
}
}
}
package xmeiju;
public class EnumTest02 {
//入口
public static void main(String[] args) {
int a = 10;
int b = 0;
Result retValue = divide(a,b);
if(retValue == Result.SUCCESS){
System.out.println("成功");
}else if(retValue == Result.FALL){
System.out.println("失败");
}
}
//实现需求
public static Result divide(int a, int b){
try{
int c = a / b;
return Result.SUCCESS; //成功
}catch(Exception e){
return Result.FALL; //失败
}
}
}
//定义一个枚举类型
enum Result{ //Result这里只要是合法标识符就可以
//成功和失败
//规范要求:大写
SUCCESS,FALL //有限的
}
//四季
enum Season{
SPRING,SUMMER,AUTUMN,WINTER //有限的
}
//颜色
enum Color{
GREEN,BLUE,RED //有限的
}