Java英尺和米的转换

英尺和米的转化公式:
1英尺=12英寸=0.3048米

方法一:

    int foot; 
    int inch; 
    Scanner in = new Scanner(System.in);
    
    foot = in.nextInt(); 
    inch = in.nextInt();
    
    System.out.println("foot="+foot+",inch="+inch);
    System.out.println((foot+inch/12.0)*0.3048);

方法二:

    int foot; 
    double inch;
    Scanner in = new Scanner(System.in);
    
    foot = in.nextInt(); inch = in.nextInt();
    
    System.out.println("foot="+foot+",inch="+inch);
    System.out.println((foot+inch/12)*0.3048);

手动强制转化为厘米:

    int foot; 
    double inch;
    Scanner in = new Scanner(System.in);
    
    foot = in.nextInt(); 
    inch = in.nextInt();
    
    System.out.println("foot="+foot+",inch="+inch);
    System.out.println((foot+inch/12)*0.3048);
    //手动强制类型转换
    System.out.println((int)((foot+inch/12)*0.3048*100)+"cm");
发布了20 篇原创文章 · 获赞 25 · 访问量 930

猜你喜欢

转载自blog.csdn.net/weixin_43699840/article/details/104076992