高分求解一个Java语言程序设计,Java程序高手来试一试

2024年11月19日 00:30
有6个网友回答
网友(1):

用这个肯定可以了,选用测试数据
12摄氏温度= 53.6华氏温度

import java.util.InputMismatchException;
import java.util.Scanner;

public class Temperature {

private float temprature;//温度数值
private char unit; //'C' for Celsius, 'F' Fahrenheit//温度单位

public Temperature() {//无参构造
this.temprature = 0.0F;
this.unit = 'C';
}

public Temperature(float temprature, char unit) {//2参数构造
this.temprature = temprature;
this.unit = String.valueOf(unit).toUpperCase().charAt(0);//确保计量单位都为大写字//母方便后面比较
}

public static void main(String args[]){
Temperature t1 = new Temperature();//第一种构造应用
Temperature t2 = new Temperature(0.0F, 'c');//第二种构造

getTemperatureValueFromInput(t1, t2);//接收键盘输入保存温度数
getTemperaturUnitFromInput(t1, t2);//接收键盘输入保存温度单位

System.out.println("Tempearture 1 is: " + t1.toString());//输出要比较的温度1
System.out.println("Tempearture 2 is: " + t2.toString());//输出要比较的温度2

System.out.println(t1.toString() + " = " + t2.toString() + "? "+ t1.equal(t2));//相等么
System.out.println(t1.toString() + " > " + t2.toString() + "? "+ t1.isHigher(t2));//大于么?
System.out.println(t1.toString() + " < " + t2.toString() + "? "+ t1.isLower(t2));//小于?
}

//从键盘依次接受温度值
private static void getTemperatureValueFromInput(Temperature t1, Temperature t2) {
// input value for temperature t1 and t2
int count = 1;
while(count <= 2){
try{
System.out.println("Please input value for temperature " + count);

Scanner scanner = new Scanner(System.in);

if(count == 1){
t1.setTempratureValue(scanner.nextFloat());
}else{
t2.setTempratureValue(scanner.nextFloat());
}
count++;
}catch(InputMismatchException mismatchExp){
System.out.println("Invalid value for tempartue!");
continue;
}
}
}

//从键盘依次接受单位并提供相应出错处理
private static void getTemperaturUnitFromInput(Temperature t1, Temperature t2) {
//input unit for temperature t1 and t2
int unitCount = 1;

while(unitCount <= 2){
try{
System.out.println("Please input unit for temperature " + unitCount);

Scanner scanner = new Scanner(System.in);

String str = scanner.next();

if(str.trim().length() > 1){
System.out.println("Invalid length for tempartue unit!");
continue;
}

char unit = str.trim().toUpperCase().charAt(0);

if(str.trim().equalsIgnoreCase("C") || str.trim().equalsIgnoreCase("F")){

if(unitCount == 1){
t1.setUnit(unit);
}else{
t2.setUnit(unit);
}
}else{
System.out.println("Invalid unit character for tempartue unit!");
continue;
}

unitCount++;
}catch(InputMismatchException mismatchExp){
System.out.println("Invalid value for tempartue unit!");
continue;
}
}
}

//转换成摄氏温度
public Temperature toCelsius(){

if(unit == 'C'){
return this;
}else{
return new Temperature((float)5F*(temprature - 32F)/9F, 'C');
}
}

//转换成华氏温度
public Temperature toFahrenheit(){
if(unit == 'F'){
return this;
}else{
return new Temperature((float)(9F*(temprature)/5F)+32F, 'F');
}
}

public void setTempratureValue(float temprature) {//设置温度值
this.temprature = temprature;
}

public void setUnit(char unit) {//设置单位
this.unit = unit;
}

public void setTemprature(float value, char unit){
this.temprature = value;
this.unit = unit;
}

public boolean equal(Temperature t2){//判断相等
t2 = convertToSameUnit(t2);
return t2.temprature == this.temprature;
}

public boolean isHigher(Temperature t2){//判断大于
t2 = convertToSameUnit(t2);
return this.temprature > t2.temprature;
}

public boolean isLower(Temperature t2){//判断小鱼
t2 = convertToSameUnit(t2);
return this.temprature < t2.temprature;
}

private Temperature convertToSameUnit(Temperature t2) {//转换成同样计量单位
if(t2.unit != unit){
if(t2.unit == 'C'){
return t2.toFahrenheit();
}else{
return t2.toCelsius();
}
}

return t2;
}

public String toString(){//重写toString方法
return temprature + " " + unit;
}
}

----------------
Please input value for temperature 1
a
Invalid value for tempartue!
Please input value for temperature 1
0
Please input value for temperature 2
bbbb
Invalid value for tempartue!
Please input value for temperature 2
32
Please input unit for temperature 1
a
Invalid unit character for tempartue unit!
Please input unit for temperature 1
ef
Invalid length for tempartue unit!
Please input unit for temperature 1
c
Please input unit for temperature 2
ss
Invalid length for tempartue unit!
Please input unit for temperature 2
F
Tempearture 1 is: 0.0 C
Tempearture 2 is: 32.0 F
0.0 C = 32.0 F? true
0.0 C > 32.0 F? false
0.0 C < 32.0 F? false

-----测试结果2
Please input value for temperature 1
12
Please input value for temperature 2
66
Please input unit for temperature 1
c
Please input unit for temperature 2
f
Tempearture 1 is: 12.0 C
Tempearture 2 is: 66.0 F
12.0 C = 66.0 F? false
12.0 C > 66.0 F? false
12.0 C < 66.0 F? true

------测试结果3
Please input value for temperature 1
12
Please input value for temperature 2
52
Please input unit for temperature 1
c
Please input unit for temperature 2
f
Tempearture 1 is: 12.0 C
Tempearture 2 is: 52.0 F
12.0 C = 52.0 F? false
12.0 C > 52.0 F? true
12.0 C < 52.0 F? false

网友(2):

大哥你的这个要求也太高了吧   给你一个地方你可以自己去看看  很快就能解决

网友(3):

原来是自己的作业 现在小学生也学会百度找人写作业了
大人教的..

网友(4):

已经发到你邮箱了,请查收。

网友(5):

限时多久?

网友(6):

郁闷,这么简单,能说清楚就不能自己写一个么.