定义一个River河流类, 属性:1。当前水位  2。警戒水位
方法: flow 流动,如果当前水位减去警戒水位大于10,则 throw new Exception("决堤啦");
否则 打印  "安静的留"
然后定义一个测试类。
并非我没想,我实在做出出来。 我结贴率100% ,请高手援助!!

解决方案 »

  1.   

    int warningLevel;//警戒水位
    public void flow(int currents) throws Exception
      {
      if(currents-warningLevel>=10)
      {
      throw new Exception("决堤了!");
      }else
      {
      System.out.println("安静的流淌……");
      }
      }
      

  2.   

    int warningLevel=10;//警戒水位
    public void flow(int currents) throws Exception 
      {
    try {
    if (currents - warningLevel >= 10) {
    throw new Exception("决堤了!");
    } else {
    System.out.println("安静的流淌……");
    }
    } catch (Exception ex) {
    System.out.println("河流出问题了,请联系管理员……");
    }
      }
    这样也可以..
    处理异常有两种方法
      

  3.   

    //River类
    public class River {
    private double current;//当前水位
    private double limit;//警戒水位

    public River() {
    current = 0;
    limit = 10;
    }

    public River(double current, double limit) {
    this.current = current;
    this.limit = limit;
    }

    public double getCurrent() {
    return current;
    }

    public void setCurrent(double current) {
    this.current = current;
    }

    public double getLimit() {
    return limit;
    }

    public void setLimit(double limit) {
    this.limit = limit;
    }

    public void flow() throws Exception {
    if(current - limit > 10) {
    throw new Exception("决堤了");
    } else {
    System.out.println("安静的流淌");
    }
    }
    }//Test类public class Test {
    public static void main(String[] args) {
    River river = new River(23,10);
    try {
    river.flow();
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }

    river.setCurrent(14);
    try {
    river.flow();
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    }
      

  4.   

    river类package csdn;public class River {    private double curWaterLevel;
        private double warnningLevel;    public River() {
        }    public River(double curWaterLevel, double warnningLevel) {
            this.curWaterLevel = curWaterLevel;
            this.warnningLevel = warnningLevel;
        }    public void flow() throws Exception {
            System.out.println(doFlow());
        }    //doFlow保证flow方法可测试!flow直接打印doFlow()方法返回值
        public String doFlow() throws Exception {
            if (curWaterLevel - warnningLevel >= 10) {
                throw new Exception("决堤啦");
            }        return "安静的流";
        }    public double getCurWaterLevel() {
            return curWaterLevel;
        }    public void setCurWaterLevel(double curWaterLevel) {
            this.curWaterLevel = curWaterLevel;
        }    public void setWarnningLevel(double warnningLevel) {
            this.warnningLevel = warnningLevel;
        }    public double getWarnningLevel() {
            return warnningLevel;
        }}
    测试用例package csdn.junit;import junit.framework.TestCase;
    import csdn.River;public class RiverNormalTestor extends TestCase {    public void testNormal() {
            // 测试没有异常
            try {
                River river = new River(19, 10);
                assertEquals("安静的流", river.doFlow());
            } catch (Exception e) {
                assertTrue(false);
            }
        }    public void testException() {
            // 测试存在异常 1
            try {
                new River(20, 10).doFlow();
                assertTrue(false);
            } catch (Exception e) {
                assertTrue(true);
            }        // 测试存在异常 2
            try {
                new River(20, 10).doFlow();
                assertTrue(false);
            } catch (Exception e) {
                assertTrue(true);
            }    }}
      

  5.   

    class River{
    private int now ;
    private int warning ; public River(int now,int warning){
    this.now = now;
    this.warning = warning;
    } public void flow() throws Exception{
    if(now - warning > 10){
    throw new Exception("决堤啦");
    }else{
    System.out.println("安静地流.");
    }
    }
    }public class TestRiver { /** 定义一个River河流类, 属性:1。当前水位 2。警戒水位
    方法: flow 流动,如果当前水位减去警戒水位大于10,则 throw new Exception("决堤啦");
    否则 打印 "安静的留"
    然后定义一个测试类。
     * @param args
     */
    public static void main(String[] args) {
    try {
    int now = Integer.parseInt(javax.swing.JOptionPane.showInputDialog("输入当前水位:"));
    int warning = Integer.parseInt(javax.swing.JOptionPane.showInputDialog("输入警戒水位水位:"));
    River r = new River(now,warning);
    r.flow();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }看看吧,不知道是不是你需要的。
    不行,再看楼上几位大佬的。
      

  6.   

    int warningLevel;//警戒水位
    public void flow(int currents) throws Exception
    {
    if(currents-warningLevel>=10)
    {
    throw new Exception("决堤了!");
    }else
    {
    System.out.println("安静的流淌……");
    }
    }就是这样啊
      

  7.   

    //River类
    public class River {
        private double current;              //当前水位
         private double warning;              //警戒水位
        
        public River() {
            current = 0;
            warning = 10;
        }
        
        public River(double current, double warning) {
            this.current = current;
            this.warning = warning;
        }
        
        public double getCurrent() {
            return current;
        }
        
        public void setCurrent(double current) {
            this.current = current;
        }
        
        public double getWarning() {
            return warning;
        }
        
        public void setWarning(double warning) {
            this.warning = warning;
        }
        
        public void flow() throws Exception {
            if(current - warning > 10) {
                throw new Exception("决堤了");
            } else {
                System.out.println("安静的流淌");
            }
        }
    }//Test类public class Test {
        public static void main(String[] args) {
            River river = new River(23,10);
            try {
                river.flow();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            
            river.setCurrent(14);
            try {
                river.flow();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }