为什么我的计算器中不会显示我的计算结果?

时间:2016-04-19 03:26:51

标签: java android android-studio

我正在尝试制作一个简单的弹道应用程序来计算子弹落差和目标时间。我已经把所有东西放在了我需要它的地方,但是当我测试它并等待我的结果时,它似乎没有进行我在那里的计算。对我有什么建议吗?

package com.example.koryhershock.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    Button btn = (Button)findViewById(R.id.button1);
    final EditText et1 = (EditText)findViewById(R.id.muzzleVelocity);
    final EditText et2 = (EditText)findViewById(R.id.targetRange);
    final TextView time = (TextView)findViewById(R.id.textView1);
    final TextView bulletdrop = (TextView)findViewById(R.id.textView2);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int x = new Integer(et1.getText().toString());
            int y = new Integer(et2.getText().toString());
            double timetotarget = x / y;
            double grav = 4.9;
            double timesquared = timetotarget * timetotarget;
            double drop = grav * timesquared;
            time.setText("Time to Target: " + timetotarget + " seconds");
            bulletdrop.setText("Bullet Drop: " + drop + " meters");
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

好的,这里可能有问题:double timetotarget = x / y;因为 x和y是整数。例如,如果x 150和y等于100,你的计算结果等于1.尝试使用下一个计算:double timetotarget =(double)x /(double)y;