如何使用用户在输入框中输入的数字的平方根,而不是数字本身?

时间:2012-02-01 03:05:36

标签: java android

我正在尝试制作Android应用,我正在处理的功能是计算用户输入的数字的平方根。

如何获取用户在文本框中输入的数字,并在我的程序的doCalc部分中使用该数字的平方根?我将数字限制为1到20之间的整数。例如,如果用户在输入框中输入2,我想在1.41方法中使用doCalc

这是我的.java代码:

package learn.text;

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

public class LearntextActivity extends Activity {
    TextView text;
    EditText input;
    TextView text2;
    EditText input2;
    TextView text3;
    EditText input3;
    Button calc;
    TextView output;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (TextView) findViewById(R.id.text);
        text.setText("Enter the design GPM for Chiller");
        input = (EditText) findViewById(R.id.input);
        text2 = (TextView) findViewById(R.id.text2);
        text2.setText("Enter the Square root of the actual pressure drop across the coil");
        input2 = (EditText) findViewById(R.id.input2);
        text3 = (TextView) findViewById(R.id.text3);
        text3.setText("Enter the design pressure drop of coil");
        input3 = (EditText) findViewById(R.id.input3);
        calc = (Button) findViewById(R.id.calc);
        output = (TextView) findViewById(R.id.output);
    } 
    public void doCalc (View view)  {
        double mInput = Double.parseDouble(input.getText().toString());
        double mInput2 = Double.parseDouble(input2.getText().toString());
        double mInput3 = Double.parseDouble(input3.getText().toString());

        double mOutput = (mInput*mInput2)/(mInput3);
        output.setText("GPM is" + mOutput);
    }    
}

这是.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/text"></TextView>
    <EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="@+id/input"></EditText>
    <TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/text2"></TextView>
    <EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="@+id/input2"></EditText>
    <TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/text3"></TextView>
    <EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="@+id/input3"></EditText>

    <Button android:layout_height="wrap_content" android:text="Get GPM" android:layout_width="wrap_content" android:id="@+id/calc" android:password="false" android:onClick="doCalc"></Button>
    <TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/output"></TextView>
</LinearLayout>

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="learn.text"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher" android:label="string/app_name"> 
        android:label="@string/app_name" >
        <activity
            android:name=".LearntextActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:3)

可能我误解了一些事情,但是 - 你不能只是改变

Double.parseDouble(input2.getText().toString())

Math.sqrt(Double.parseDouble(input2.getText().toString()))

? (有关Math.sqrt的文档,请参阅http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double)。)