如何计算java中的手机账单,计算问题

时间:2015-09-29 23:19:20

标签: java

我的任务是计算java中的每月手机账单,我是java新手,我还在学习。我似乎无法理解计算以及如何到达那里。我已经命名了所有变量和常量并初始化它们,现在我只需要进行计算以找出总账单的成本。我想要达到的输出是:运行:使用的分钟数=发送/接收的675条短信=使用的1031数据= 605兆字节。你有数据播放吗? (是/否):Y你有短信计划(是/否)?:Y 每月手机费用:每月计划= 39.99短信计划= 15.00数据计划= 20.00收费的额外分钟= 79.85数据超额收费= 40.00

public static void main(String[] args) {
        // TODO code application logic here
        Scanner user_input = new Scanner(System.in);

        final double MONTHLY_PLAN_VOICE;
        final int ALLOWABLE_MINUTES;
        final double ADDITIONAL_MINUTE_RATE;
        final double TEXT_MSG_PLAN;
        final double TEXT_MSG_RATE;
        final double DATA_PLAN_RATE;
        final int ALLOWABLE_DATA_INCR;

        MONTHLY_PLAN_VOICE = 39.99; //double cause of decimal
        ALLOWABLE_MINUTES = 500; // int because no need for decimal
        ADDITIONAL_MINUTE_RATE = 0.45; // has decimal 
        TEXT_MSG_PLAN = 15.00; // rate could very depending on user input(double)
        TEXT_MSG_RATE = 0.12; // rate could very depending on user input (double)
        DATA_PLAN_RATE = 20.00; // double cause of decimal
        ALLOWABLE_DATA_INCR = 300; //int cause its an increment

        String minutes;
        System.out.print("Enter the number of minutes used this month: ");
        minutes = user_input.next();
        String texts;
        System.out.print("Do you have a text message plan? (Y/N): ");

        texts = user_input.next();
        System.out.print("Enter the number of sent and recieved texts: ");
        texts = user_input.next();
        String data;
        System.out.print("Do you have a Data plan? (Y/N): ");
        data = user_input.next();
        System.out.print("Enter the amount of Data used: ");
        data = user_input.next();



        if (ALLOWABLE_DATA_INCR < 300) 
        {
        System.out.print("The Cost of data is 20.00$ ");
        }
        else 
        {    
        System.out.println("The cost of data is 40.00$" );
        }


        if (ALLOWABLE_MINUTES > 500)
        {
        System.out.print("");
        }

2 个答案:

答案 0 :(得分:0)

首先,我个人的选择是保持代码清洁,并一步启动和分配:

 final double MONTHLY_PLAN_VOICE = 39.99; //double cause of decimal

其次是美元符号&#34; $&#34;应始终用作数字的前缀。前5.99美元 您的打印声明如下:

 System.out.print("The Cost of data is 20.00$ ");

我建议只更改每一项内容,使其看起来更专业。

 System.out.print("The cost of data is $20.00.");

现在回答你的问题。 要查找总数,请创建double类型的新变量。 :

double totalMonthlyBill = 0;

现在,在每个if else语句中,将每个部分的费用添加到此总变量中。

例如:

 if (ALLOWABLE_DATA_INCR < 300) 
    {
    System.out.print("The Cost of data is 20.00$ ");
    totalMonthlyBill += 20;
    }
    else 
    {    
    System.out.println("The cost of data is 40.00$" );
    totalMonthlyBill += 40;
    }

+ =将增加或减去(totalMonthlyBill)的值,否则传递任何值。您可以使用+ = 2变量或传递int或双分机。

假设先前已初始化Variable1和Variable2,并且两者都被赋予非空值

Variable1 += Variable2;
Variable1 += 2;

这与:

相同
Variable1 = Variable1 + Variable2;
Variable1 = Variable1 + 2;

但是在你的情况下,因为你有一个总计并且将根据每个步骤定义成本,所以可以通过定义一个总变量然后在每个if / else语句中使用+ =将值添加到然后您可以稍后调用最终价格

System.out.println("Total monthly bill: " + totalMonthlyBill);

希望这能解决问题。

答案 1 :(得分:0)

我必须创建一个新答案才能提交所有代码,因为评论时间过长,但如果您对此工作有任何疑问,请查看此评论并发表评论,但我认为这就是您想要的。

    boolean textPlan;
    double numTexts = 0;

    String texts;
    System.out.print("Do you have a text message plan? (Y/N): ");
    texts = user_input.next();
    if (texts.equalsIgnoreCase("y")){
        textPlan = true;
    }
    else{
        textPlan = false;
    }
    System.out.print("Enter the number of sent and recieved texts: ");
    numTexts = user_input.next();

    if (textPlan && numTexts <= 500){
        totalMonthlyBill += TEXT_MSG_PLAN;
    }
    else if (textPlan && numTexts > 500){
        numTexts -= 500; 
        totalMonthlyBill += (numTexts * TEXT_MSG_RATE);
    }
    else{
        totalMonthlyBill += (numTexts * TEXT_MSG_RATE);
    }