转换为Float进行数学运算

时间:2016-11-30 06:35:42

标签: java android-studio

在Android Studio 2.2.2(MainActivity.java)中编写程序。基本上有多个输入,一个按钮,多个输出。但是,许多输出交叉引用到许多其他输出中。当我们的教授在一个非编程课上把它扔给我们时,我对这一切都是新手。谁能看到我的一些错误?我希望我在这个过程中大部分都是正确的,但是这些操作都存在string / float / double不兼容问题。感谢您的任何见解!

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnCalc;
private TextView tvaResult;
private TextView tvcResult;
private TextView tvetResult;
private TextView tvphiResult;
private TextView tvMnResult;
private TextView tvphiMnResult;
private TextView tvbeta1Result;
private EditText etB,etD,etH,etAs,etFc,etFy;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}

private void init() {
    btnCalc = (Button)findViewById(R.id.btnCalc);
    etB = (EditText)findViewById(R.id.etB);
    etD = (EditText)findViewById(R.id.etD);
    etH = (EditText)findViewById(R.id.etH);
    etAs = (EditText)findViewById(R.id.etAs);
    etFc = (EditText)findViewById(R.id.etFc);
    etFy = (EditText)findViewById(R.id.etFy);
    tvaResult = (TextView)findViewById(R.id.tvaResult);
    tvcResult = (TextView)findViewById(R.id.tvcResult);
    tvetResult = (TextView)findViewById(R.id.tvetResult);
    tvphiResult = (TextView)findViewById(R.id.tvphiResult);
    tvMnResult = (TextView)findViewById(R.id.tvMnResult);
    tvphiMnResult = (TextView)findViewById(R.id.tvphiMnResult);
    tvbeta1Result = (TextView)findViewById(R.id.tvbeta1Result);

    btnCalc.setOnClickListener(this);
}

@Override
public void onClick(View view) {
        Float B = Float.parseFloat(etB.getText().toString());
        Float D = Float.parseFloat(etD.getText().toString());
        Float H = Float.parseFloat(etH.getText().toString());
        Float As = Float.parseFloat(etAs.getText().toString());
        Float Fc = Float.parseFloat(etFc.getText().toString());
        Float Fy = Float.parseFloat(etFy.getText().toString());
        Float aResult = Float.parseFloat(tvaResult.getText().toString());
        Float cResult = Float.parseFloat(tvcResult.getText().toString());
        Float etResult = Float.parseFloat(tvetResult.getText().toString());
        Float beta1Result = Float.parseFloat(tvbeta1Result.getText().toString());
        Float phiResult = Float.parseFloat(tvphiResult.getText().toString());
        switch(view.getId() ) {
            case R.id.btnCalc:
                tvaResult = (Fy * As) / (0.85 * Fc * B);
                tvcResult = aResult / beta1Result;
                tvetResult = ((D - cResult) / (cResult)) * 0.003;
                if (Fc <= 4000) {
                    beta1Result = 0.85;
                } else if (4000 < Fc <= 8000) {
                    beta1Result= ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
                } else {
                    beta1Result= 0.65;
                }
                if (etResult >= 0.005) {
                    tvphiResult= 0.9;
                } else if (0.002 <= etResult < 0.005) {
                    tvphiResult= 0.65 + ((etResult - 0.002) * ((0.25) / (0.005 - 0.002)));
                } else {
                    tvphiResult= 0.00
                }
                tvMnResult= (Fy * As) * (etD - (aResult / 2));
                tvphiMnResult= phiResult * tvMnResult
        }}
}}

1 个答案:

答案 0 :(得分:0)

要简短,您需要保留每个变量的类型。

例如:

tvaResult = (Fy * As) / (0.85 * Fc * B);

你有

TextView = (Float * Float) / ( Double * Float * Float)
TextView = Double // Not possible

您正在尝试将Float值放入TextView的实例中,当然不能这样做。但是你真的想要更新 Textview的文本,所以使用TextView的方法setText(String),如:

tvaResult.setText((Fy * As) / (0.85 * Fc * B) + ""); // Need to be convert into String

更改要打印的文字

这基本上总是犯同样的错误,你需要更改TextView的文本而不是试图改变实例本身。