繁体   English   中英

减少android java程序中代码的行数

[英]Reduce the number of lines in code in android java program

我自己苏杰。 我对android编程很陌生。 我编写了一个程序来转换我们在纺织领域使用的值。 有些值是成正比的,有些是间接成正比的。 我给出了我在下面写的代码可以帮助我减少程序中使用其他一些 JAVA 代码的行数

  public void onClick(View v)
{

    int index1 = spinner11.getSelectedItemPosition();
    int index2 = spinner21.getSelectedItemPosition();
    double value = 0;
    if (from.getText().toString().isEmpty())
        Toast.makeText(getApplicationContext(), getResources().getString(R.string.toastmessage1),
                Toast.LENGTH_LONG).show();
    else {
        value = Double.parseDouble(from.getText().toString());
    }
    //From Ne
    if (index1 == 0 && index2 == 0 )//Ne to Ne
    {
        double result = value*1;
        to.setText(result+"");
    }
    if (index1 == 0 && index2 == 1) //Ne to Nm
    {
        double result = value * 1.69;
        to.setText(result+"");
    }
    if (index1 == 0 && index2 == 2)//Ne to Tex
    {
        double result = 591/value;
        to.setText(result+"");
    }
    if(index1 == 0 && index2 == 3)//Ne to kTex
    {
        double result = 0.591/value;
        to.setText(result+"");
    }
    if (index1 == 0 && index2 == 4)//Ne to Denier
    {
        double result = 5314/value;
        to.setText(result+"");
    }
    if (index1 == 0 && index2 == 5)//Ne to Denier
    {
        double result = 5314/value;
        to.setText(result+"");
    }
    if (index1 == 0 && index2 == 6)//Ne to Denier
    {
        double result = 5314/value;
        to.setText(result+"");
    }
    if (index1 == 0 && index2 == 7)//Ne to Denier
    {
        double result = 5314/value;
        to.setText(result+"");
    }

这将稍微减少行数:

double result = 0.0;
if (index1 == 0)//Ne to Ne
{
    if(index2 == 0)
        result = value*1;

    if (index2 == 1) 
        result = value * 1.69;

    if (index2 == 2)
        result = 591/value;

    if(index2 == 3)
       result = 0.591/value;

    if (index2 == 4 || index2 == 5 || index2 == 6 || index2 == 7)
        result = 5314/value;

}
to.setText(result+"");

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM