代码:
import javax.swing.JOptionPane;
/**
* 类描述:摄氏温度与华氏温度操作类
*
* @author
* @since JDK1.5
* @history 2010-12-15 上午10:39:51 新建
* @version 1.0
*/
public class Temperature {
/** 温度值 */
private float value = 0F;// 初始为0度
/** 单位,字符类型:取值 ’C’ 或 ’F’,C表示Celsius(摄氏温度)、F表示Fahrenheit(华氏温度) */
private char unit = 'C';// 初始为摄氏温度C
/** 摄氏温度单位常量C */
private static final char UNIT_CELSIUS = 'C';
/** 华氏温度单位常量F */
private static final char UNIT_FAHRENHEIT = 'F';
Temperature() {
}
Temperature(float value, char unit) {
this.value = value;
this.unit = unit;
}
public char getUnit() {
return unit;
}
public void setUnit(char unit) {
this.unit = unit;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
public void setValueAndUnit(float value, char unit) {
this.value = value;
this.unit = unit;
}
/** 返回改温度对象的摄氏温度 */
public float getDegreesC() {
// 如果单位已是摄氏温度,则直接返回值
if (this.unit == this.UNIT_CELSIUS) {
return this.value;
}
// 否则返回换算后的值
return 5 * (this.value - 32) / 9;
}
/** 返回改温度对象的华氏温度 */
public float getDegreesF() {
// 如果单位已是华氏温度,则直接返回值
if (this.unit == this.UNIT_FAHRENHEIT) {
return this.value;
}
// 否则返回换算后的值
return (9 * (this.value) / 5) + 32;
}
/**
* 温度的比较方法
*
* @param Temperature t 要比较的温度
* @return 相等返回0,比参数大返回1,比参数小返回-1
*/
public int compare(Temperature t) {
// 将温度同一为摄氏温度做比较
if (this.getDegreesC() - t.getDegreesC() == 0) {
return 0;
} else if (this.getDegreesC() - t.getDegreesC() > 0) {
return 1;
} else if (this.getDegreesF() - t.getDegreesC() < 0) {
return -1;
}
return -2;// 异常
}
public String toString() {
return "温度:" + this.value + " 单位:" + String.valueOf(this.unit);
}
public static void main(String[] args) {
Temperature t1 = createTemperature();
Temperature t2 = createTemperature();
if (t1.compare(t2) == 0) {
JOptionPane.showMessageDialog(null, t1.toString() + " 等于 " + t2.toString());
} else if (t1.compare(t2) < 0) {
JOptionPane.showMessageDialog(null, t1.toString() + " 小于 " + t2.toString());
} else if (t1.compare(t2) > 0) {
JOptionPane.showMessageDialog(null, t1.toString() + " 大于 " + t2.toString());
}
}
/**
* 方法说明:接受输入参数,返回温度对象
*
* @author
* @return Temperature 构造完的温度
*/
private static Temperature createTemperature() {
String value = null;
String unit = null;
Temperature t = new Temperature();
value = JOptionPane.showInputDialog("温度值", "输入温度的度数");
if (value != null && !"".equals(value)) {
while (!value.matches("\\d*.?\\d*")) {
JOptionPane.showMessageDialog(null, "只能输入数值");
String temp = JOptionPane.showInputDialog("温度值", "输入温度的度数");
if (temp != null) {
unit = temp;
} else {
// 值为null,则默认0;
value = "0";
break;
}
}
t.setValue(Float.parseFloat(value));
}
unit = JOptionPane.showInputDialog("", "输入温度的单位(C/F),大小写不限");
if (unit != null && !"".equals(unit)) {
while (!unit.toUpperCase().matches("C|F")) {
System.out.println(unit);
JOptionPane.showMessageDialog(null, "只能输入C或F");
String temp = JOptionPane.showInputDialog("单位", "输入温度的单位(C/F),大小写不限");
if (temp != null) {
unit = temp;
} else {
// 单位为null,则默认C
unit = String.valueOf(Temperature.UNIT_CELSIUS);
break;
}
}
t.setUnit(unit.charAt(0));
}
return t;
}
}
复制保存为Temperature.java
命令行javac Temperature.java编译
java Temperature运行
没加方法啊,把main函数里的代码提出来了而已。老是咋那么死板啊?
全听学校里老是那个,是个人都笨死了。
温馨提示:答案为网友推荐,仅供参考