java写关于温度的算法_摄氏温度和华氏温度的转换之java算法

package pm;

import java.util.Scanner;

public class SwitchTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

while(true){

System.out.println("请输入目前需要转换的温度类型:℃或C或c或℉或F或f");

String s=sc.next().trim();

if("c".equalsIgnoreCase(s)||"℃".equalsIgnoreCase(s)){

System.out.println("请输入要转换的摄氏度的温度的数值:");

double wd1=sc.nextDouble();

double wd2=wd1*1.8+32;

System.out.println("对应的华氏温度:"+wd2+"℉");

}

else if("f".equalsIgnoreCase(s)||"℉".equalsIgnoreCase(s)){

System.out.println("请输入要转换的华氏度的温度的数值:");

double wd1=sc.nextDouble();

double wd2=(wd1-32)/1.8;

System.out.println("对应的摄氏温度:"+wd2+"℃");

}

else

System.out.println("你输入的字符有误,退出程序!");

break;

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中封装的摄氏温度与华氏温度转换和英寸和厘米转换算法: ```java public class Converter { private double celsius; private double fahrenheit; private double inches; private double centimeters; public Converter() {} public void setCelsius(double celsius) { this.celsius = celsius; } public double getCelsius() { return this.celsius; } public void setFahrenheit(double fahrenheit) { this.fahrenheit = fahrenheit; } public double getFahrenheit() { return this.fahrenheit; } public void setInches(double inches) { this.inches = inches; } public double getInches() { return this.inches; } public void setCentimeters(double centimeters) { this.centimeters = centimeters; } public double getCentimeters() { return this.centimeters; } public double celsiusToFahrenheit() { return (this.celsius * 1.8) + 32; } public double fahrenheitToCelsius() { return (this.fahrenheit - 32) / 1.8; } public double inchesToCentimeters() { return this.inches * 2.54; } public double centimetersToInches() { return this.centimeters / 2.54; } } ``` 这个类包含了四个私有属性:摄氏温度、华氏温度、英寸和厘米。它们都有对应的getter和setter方法。 除此之外,这个类还包含了四个公有方法:`celsiusToFahrenheit()`用于将摄氏温度转换为华氏温度,`fahrenheitToCelsius()`用于将华氏温度转换为摄氏温度,`inchesToCentimeters()`用于将英寸转换为厘米,`centimetersToInches()`用于将厘米转换为英寸。 你可以根据需要,使用这些方法进行温度和长度单位的转换

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值