标签:
package text; public class t20151014s { public static void main(String[] args) { // TODO Auto-generated method stub Shuibei Shuibei = new Shuibei("黑色","圆形",100);//实例化水杯 Shuibei.Shuiliang = 0;//引用Shuibei.java中的Shuiliang这个量,并对其进行赋值 Shuibei.Zhengfal = 0.4; Shuibei.Jiashui(100);//引用Shuibei.java中的Jiashui这个方法,进行计算与输出数值 Shuibei.Daoshui(10); Shuibei.Jiare(100); } }
package text; public class Shuibei { //颜色 public String Yanse; //形状 public String Xingzhuang; //容量 double Rongliang; //当前水量 double Shuiliang; double endu = 0; //实例化 public Shuibei(String yanse,String xingzhuang,int rongliang) { Yanse = yanse; Xingzhuang = xingzhuang; Rongliang = rongliang; } //水加热1度的蒸发量 public double Zhengfal = 0.3; //加水,倒水运算 public void Jiashui(double Jiasl) { Shuiliang += Jiasl; //剩余容量 double syrl = Rongliang - Shuiliang; if(syrl < 0) { System.out.println("当前水杯已满,请停止加水!"); Shuiliang = Rongliang; syrl = 0; } else { System.out.println("加了 "+ Jiasl+" L水"+"\n"+"当前水量为:" + Shuiliang +" L"+ "\n" + "水杯剩余容量为:" + syrl +" L"); } } public void Daoshui(double Daosl) { Shuiliang -= Daosl; //水杯剩余容量 double syrl = Rongliang - Shuiliang; if(syrl < 0) { System.out.println("当前水杯已无水,请停止倒水!"); Shuiliang = Rongliang; syrl = Rongliang; } else { System.out.println("倒出 "+Daosl+" L水"+"\n"+"当前水量为:" + Shuiliang +" L"+ "\n" + "水杯剩余容量为:" + syrl+" L"); } } //要加热到的温度 Wendu public void Jiare(double Wendu) { double Zfdsl = Wendu * Zhengfal; // //如果剩余的水不足以加热到设定的温度,就会自动提示剩余的水可以加热到的温度 if(Shuiliang <= Zfdsl) { double zfdsl = 0; double wendu = 1; for(wendu = 1;Shuiliang > zfdsl;wendu++) { zfdsl = wendu * Zhengfal; this.endu = wendu; //通过this 指令将for循环中的数值跳转到外部进行计算 } System.out.println("当加热到 " + endu +" 度时,水会蒸发没了"); } else { Shuiliang -= Zfdsl; System.out.println("加热到 " + Wendu +" 度时,水蒸发了 "+ Zfdsl +" L当前水杯中剩余水量为:"+ Shuiliang +" L"); } } }
标签:
原文地址:http://www.cnblogs.com/name-hanlin/p/4878718.html