admin管理员组文章数量:1794759
情人节送玫瑰花Java实现
//RoseException.Java package cn.campsg.java.experiment.exception; public class RoseException extends Exception { public RoseException() {} public RoseException(String msg){ super(msg); } } //Rose.java package cn.campsg.java.experiment; import cn.campsg.java.experiment.exception.RoseException; public class Rose { private int total; public Rose() {} public Rose (int total){ this.total = total ; } public void giveRose(int num) throws RoseException { if(num < 1) { System.out.println("送花的数量不正确:数量不能小于1!"); } else if (total<= 0||num>total) { throw new RoseException("没钱买" + num + "支玫瑰花"); } System.out.println("亲爱的,送你" + num + "朵玫瑰花!"); this.total -= num; } } //MainClass.java package cn.campsg.java.experiment; import cn.campsg.java.experiment.exception.RoseException; import java.util.Scanner; public class MainClass { public static void main (String gras[]) { System.out.println("你想送女朋友多少朵玫瑰(数字):"); Scanner in = new Scanner(System.in); Rose rose = new Rose(100); try { int num = in.nextInt(); rose.giveRose(num); } catch (RoseException e) { System.out.println(e.getMessage()); } finally { in.close(); System.out.println("无论如何,我都是爱你的!\\n"); } } }
版权声明:本文标题:情人节送玫瑰花Java实现 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686840163a108920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论