用JAVA写一个关于英寸和厘米之间的换算

首先,1英寸=2.54厘米。有这个条件就很好编写程序了。如果要求一个程序可以让英寸和厘米之间相互转换,即既可以厘米换算成英寸,又能英寸换成厘米,那么可以使用条件语句if....else if。设定一个变量X,如果X=1,则运行英寸转厘米;如果X=2,则运行厘米转英寸。具体代码如下:

import java.util.Scanner;
public class Duhai {
public static void main(String[] args){
final double A=2.54;
double b,c;
int x;
Scanner sc=new Scanner(System.in);
System.out.println("请选择转换类型");
   x=sc.nextInt();
   if(x==1){ //x=1,表示英寸转厘米
System.out.print("请输入英寸数:");

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值