简体   繁体   中英

How can I use the square root of a number that the user enters in an input box, instead of the number itself?

I'm trying to make an Android app, and the feature that I'm working on right now is calculating the square root of a number entered by the user.

How can I take a number that the user enters in a text box, and use the square root of that number in the doCalc part of my program? I'm limiting the number to be an integer between 1 and 20. For example, if the user enters 2 in the input box, I want to use 1.41 in the doCalc method.

Here is my .java code:

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);
    }    
}

Here is the .xml file:

<?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>

Here is the manifest file:

<?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>

Probably I'm misunderstanding something, but — can't you just change

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

to

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

? (See http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double) for documentation of Math.sqrt .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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