• 2018/12/06 L1-029 是不是太胖了 Java


    考察Java小数点的运算:

    总共有四种方案:

    1、System.out.printf() 或

    2、DecimalFormat

    3、System.out.print(String.format("%.1f", flnalNum));

    4、NumberFormat


    1、使用System.out.printf()的方案, 代码如下:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
                System.out.printf("%.1f", finalNum);
            } else {
                return;
            }
    
        }
    
    }

    2、使用DecimalFormat方案:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.text.DecimalFormat;
    
    public class Main2 {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            DecimalFormat df = new DecimalFormat("#.0");
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
    //            System.out.printf("%.1f", finalNum);
                System.out.print(df.format(finalNum));
            } else {
                return;
            }
    
        }
    
    }

    3、System.out.print(String.format("%.1f", flnalNum));方案

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main3 {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
    //            System.out.printf("%.1f", finalNum);
    //            System.out.print(df.format(finalNum));
                System.out.print(String.format("%.1f", finalNum));
            } else {
                return;
            }
    
        }
    
    }

    4、NumberFormat方案, 不足的是, 小数位为零的时候, 会省略小数点和后面的小数:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.text.NumberFormat;
    
    public class Main4 {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            double num = Integer.parseInt(br.readLine());
            double finalNum = 0;
            NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMaximumFractionDigits(1);
            if(num > 100 && num <=300) {
                finalNum = (num-100)*0.9*2;
                System.out.println(nf.format(finalNum));
            } else {
                return;
            }
    
        }
    
    }
  • 相关阅读:
    STL的相关知识
    有关欧拉通路/回路的一些资料整理
    差分约束
    BZOJ 2100: [Usaco2010 Dec]Apple Delivery
    BZOJ 2017: [Usaco2009 Nov]硬币游戏(A Coin Game)
    vijos 1282&1283&1284&1285 佳佳的魔法照片/魔法药水/魔杖/魔法阵
    BZOJ 1660: [Usaco2006 Nov]Bad Hair Day
    BZOJ 1602: [Usaco2008 Oct]牧场行走
    BZOJ 1647: [Usaco2007 Open]Fliptile 翻格子游戏
    BZOJ 1646: [Usaco2007 Open]Catch That Cow
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10079315.html
Copyright © 2020-2023  润新知