十进制格式:我书中的例子太基础了

时间:2013-10-07 19:58:50

标签: decimalformat

// This is mpg calculator

// September 25th

import javax.swing.JOptionPane;

public class Green 
{

public static void main(String []args)
{
    String papaMpgStr, mamaMpgStr, broMpgStr, sisMpgStr, output ="";
    double papaMpg, mamaMpg, broMpg, sisMpg, familyAverage;

    papaMpgStr = JOptionPane.showInputDialog("What is Papa's MPG: ");
    papaMpg = Double.parseDouble(papaMpgStr);

    mamaMpgStr = JOptionPane.showInputDialog("What is Mama's MPG: ");
    mamaMpg = Double.parseDouble(mamaMpgStr);

    broMpgStr = JOptionPane.showInputDialog("What is Bro's MPG: ");
    broMpg = Double.parseDouble(broMpgStr);

    sisMpgStr = JOptionPane.showInputDialog("What is Sis's MPG: ");
    sisMpg = Double.parseDouble(sisMpgStr);

    familyAverage = (papaMpg + mamaMpg + broMpg + sisMpg)/3;

    if (familyAverage>30)
    {
        output = "Congratulations you are a green family!";
    }   

    else
    {
        output = "Sorry, you are not a green family. Perhaps change   vehicles."; 
    }

    JOptionPane.showMessageDialog(null,output);
    }

}

这是一个非常简单的程序,看到我们刚刚学习了DecimalFormat Class,我们必须使用它来重做这个程序。我对使用十进制格式类并不感到困惑,我的问题是我的老师说我们不能像上面那样连接用户输入。我不是要求有人使用DecimalFormat重写整个代码,但是帮我弄清楚我究竟能找到平均值吗?

0 个答案:

没有答案