需要在Java中一些房地产数学帮助 [英] Need help with some real estate math in java

查看:106
本文介绍了需要在Java中一些房地产数学帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在Java中一些房地产数学题这每一次炸毁

 公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        //这个程序共有房产的费用为客户卖房子        Button按钮=(按钮)findViewById(R.id.Button01);         //为priceText 360000的样本数据
        最终的EditText priceText =(EditText上)findViewById(R.id.EditText01);
        //为rateText 0.04的样本数据
        最终的EditText rateText =(EditText上)findViewById(R.id.EditText02);        button.setOnClickListener(新OnClickListener()
 {
 公共无效的onClick(视图v)
 {
 Toast.makeText(jsclosingcost.this,做闭幕成本分解,Toast.LENGTH_SHORT)
                //为priceText 360000的样本数据
  浮fPrice = Float.parseFloat(priceText.getText()的toString()+);
                 //为rateText 0.04的样本数据
  Frate的浮动= Float.parseFloat(rateText.getText()的toString()+);  浮fRealEsate = fPrice * Frate的;
  Toast.makeText(jsclosingcost.this,房地产经纪费:+ fRealEsate,Toast.LENGTH_SHORT).show();
 }
 });
    }


解决方案

像其他人所说的,并没有给我们提供了很多去这每一次吹起来。这么说,我这扔成一个测试项目,发现它不编译 - 你在这行的末尾缺少一个分号:

 
Toast.makeText(jsclosingcost.this,做闭幕成本分解,Toast.LENGTH_SHORT)

通过添加一个分号到该行,我是能够编译这个并没有问题运行它。

请注意的几件事情,但是...


您不需要为空字符串添加到您在此字符串:

 
浮fPrice = Float.parseFloat(priceText.getText()的toString()+);
Frate的浮动= Float.parseFloat(rateText.getText()的toString()+);

以这种方式追加一个空字符串是一个老把戏,以确保事情都转换为String对象,而是通过调用对象的toString,你已经保证,他们是String对象。


我不知道什么被认为是最佳做法,但对Toast.makeText方法的第一个参数是上下文对象。我会感觉更舒服越来越从传递给你的onClick处理程序视图对象的背景下,像这样的:

 
Toast.makeText(v.getContext(),做闭幕成本分解,Toast.LENGTH_SHORT);


您没有检查任何种类上,以防有人失败,以填补他们在您的编辑字段。例如,如果有人无法输入一个值EditText01他们preSS你的按钮,你会一个NullPointerException异常落得在这里:

 
浮fPrice = Float.parseFloat(priceText.getText()的toString()+);

您可以很容易地防止这种做这样的事情,而不是:

 
公共无效的onClick(视图v)
{
   Toast.makeText(v.getContext(),做闭幕成本分解,Toast.LENGTH_SHORT);   浮fPrice,Frate的;   尝试
   {
      fPrice = Float.parseFloat(priceText.getText()的toString());
      Frate的= Float.parseFloat(rateText.getText()的toString());      浮fRealEsate = fPrice * Frate的;
      Toast.makeText(v.getContext(),房地产经纪费
         + fRealEsate,Toast.LENGTH_SHORT).show();
   }
   赶上(NumberFormatException的NFE)
   {
      Toast.makeText(v.getContext(),
         请输入价格和速率的数值。
         Toast.LENGTH_SHORT).show();
   }
}


请命名您的按钮和盒子的EditText东西比Button01和EditText02更好。使用好名字会使你的生活(和其他人的生活)更容易。

Need help with some real estate math in java this blows up each time

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // This app total real estate fees for a client selling a house

        Button button = (Button) findViewById(R.id.Button01);

         //  Sample data for priceText 360000
        final EditText priceText = (EditText) findViewById(R.id.EditText01);
        // Sample data for rateText .04
        final EditText rateText = (EditText) findViewById(R.id.EditText02);

        button.setOnClickListener(new OnClickListener()
 {
 public void onClick(View v) 
 {
 Toast.makeText(jsclosingcost.this, "Doing Closing Cost Breakdown", Toast.LENGTH_SHORT)
                //  Sample data for priceText 360000
  float fPrice=Float.parseFloat(priceText.getText().toString() + "");
                 // Sample data for rateText .04
  float fRate=Float.parseFloat(rateText.getText().toString() + "");

  float fRealEsate = fPrice * fRate;
  Toast.makeText(jsclosingcost.this, "Real Estate Brokerage Fee: " + fRealEsate, Toast.LENGTH_SHORT).show();
 }
 });


    }

解决方案

Like others have said, "This blows up every time" doesn't give us a lot to go on. That said, I tossed this into a test project and found that it doesn't compile - you're missing a semicolon at the end of this line:


Toast.makeText(jsclosingcost.this, "Doing Closing Cost Breakdown", Toast.LENGTH_SHORT)

By adding a semicolon to that line, I was able to compile this and run it without issue.

A few things of note, however...


You don't need to append an empty string to your strings here:


float fPrice=Float.parseFloat(priceText.getText().toString() + "");
float fRate=Float.parseFloat(rateText.getText().toString() + "");

Appending an empty string in this way is an old trick to ensure things are cast as String objects, but by calling toString on the objects, you've already guaranteed that they're String objects.


I don't know what's considered "best practice", but the first parameter to the Toast.makeText method is a "Context" object. I'd feel more comfortable getting the Context from the view object passed to your onClick handler, like this:


Toast.makeText(v.getContext(), "Doing Closing Cost Breakdown", Toast.LENGTH_SHORT);


You don't have any sorts of checks on your edit fields in case someone fails to fill them in. For example, if someone fails to enter a value into EditText01 and they press your button, you're going to end up with a NullPointerException right here:


float fPrice=Float.parseFloat(priceText.getText().toString() + "");

You could easily guard against this by doing something like this, instead:


public void onClick(View v) 
{
   Toast.makeText(v.getContext(), "Doing Closing Cost Breakdown", Toast.LENGTH_SHORT);

   float fPrice, fRate;

   try
   {
      fPrice = Float.parseFloat(priceText.getText().toString());
      fRate = Float.parseFloat(rateText.getText().toString());

      float fRealEsate = fPrice * fRate;
      Toast.makeText(v.getContext(), "Real Estate Brokerage Fee: " 
         + fRealEsate, Toast.LENGTH_SHORT).show();
   }
   catch (NumberFormatException nfe)
   {
      Toast.makeText(v.getContext(), 
         "Please enter numeric values for Price and Rate.", 
         Toast.LENGTH_SHORT).show();
   } 
}


Please name your buttons and edittext boxes something better than Button01 and EditText02. Using good names will make your life (and everyone else's lives) easier.

这篇关于需要在Java中一些房地产数学帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆