在Android应用程序中的毕达哥拉斯

时间:2016-05-14 13:59:42

标签: java android math

嘿伙计我不仅要计算c =斜边,还要计算出3个EditText-Views中给出的a或b。但它崩溃了,不会输出结果。有什么建议?我对这些东西很新。 :)

package net.starostka.somefunctions;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Pythagoras extends AppCompatActivity implements View.OnClickListener {

EditText aNumPyt;
EditText bNumPyt;
EditText cNumPyt;

Button pythagorasCalcu;

TextView pythagorasResultFinal;

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

    aNumPyt = (EditText) findViewById(R.id.aNumPyt);
    bNumPyt = (EditText) findViewById(R.id.bNumPyt);

    pythagorasCalcu = (Button) findViewById(R.id.pythagorasCalcu);

    pythagorasResultFinal = (TextView) findViewById(R.id.pythagorasResultFinal);

    // set a listener
    pythagorasCalcu.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    double a=0,b=0,c=0;
    double hypot = 0.0;

    /*
    // check if the fields are empty
    if (TextUtils.isEmpty(aNumPyt.getText().toString()) || TextUtils.isEmpty(bNumPyt.getText().toString()) || TextUtils.isEmpty(cNumPyt.getText().toString())) {
        return;
    }
    */

    // read EditText and fill variables with numbers
    a = Double.parseDouble(aNumPyt.getText().toString());
    b = Double.parseDouble(bNumPyt.getText().toString());
    c = Double.parseDouble(cNumPyt.getText().toString());

    if (a>b && a>c){
        hypot=a;
        if(hypot*hypot==(b*b+c*c)){
            //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
            pythagorasResultFinal.setText(String.valueOf(hypot));
        }
    } else if (b>a && b>c){
        hypot=b;
        if(hypot*hypot==(a*a+c*c)){
            //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
            pythagorasResultFinal.setText(String.valueOf(hypot));
        }
    } else if (c>a && c>b){
        hypot=c;
        if(hypot*hypot==(a*a+b*b)){
            //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
            pythagorasResultFinal.setText(String.valueOf(hypot));
        }
    }
}

}

XML文件                                                                                                                                                 

    </LinearLayout>
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
        android:textSize="12pt"
        android:layout_marginTop="3pt"
        android:id="@+id/pythagorasResultFinal"
        android:gravity="center_horizontal">
    </TextView>

崩溃前:

崩溃后:

3 个答案:

答案 0 :(得分:4)

修改 有趣的是,之前没有人指出这一点,但我刚刚意识到你从未初始化cNumPyt。您初始化其他EditText但从不C。因此,当您尝试获取文本时,您会得到NullPointerException,因为您尝试从中获取信息。

onCreate方法中初始化正确的文本框。

在您的图片中,您显示您为其中一个文本框留下输入空白(在“崩溃后”图片中,我们看到您输入了3,4,然后没有输入任何内容)。这意味着没有Double被解析,因为""不是数字。

来自Double documentation

  

public static double parseDouble(String s) throws NumberFormatException
  [...]
  抛出:
  NullPointerException - 如果字符串为空   NumberFormatException - 如果字符串不包含可解析的double。

如果输入为空,则会出现异常。由于它不在try-catch块中,因此您的应用程序将崩溃。

更具体一点:

//cNumPyt is empty, and "" is not a parseable number, so NumberFormatException
c = Double.parseDouble(cNumPyt.getText().toString());

示例块(您需要分别对每个变量执行此操作,以便您可以判断哪个变量为空):

try{
    a = Double.parseDouble(aNumPyt.getText().toString());
}catch (NumberFormatException e){
    //oops, no number available
    a = 0; //default to 0 (or whatever you choose)
}

答案 1 :(得分:2)

 @Override
public void onClick(View v) {
double a=0,b=0,c=0;


if (!TextUtils.isEmpty(aNumPyt.getText().toString()) &&
    !TextUtils.isEmpty(bNumPyt.getText().toString()) &&
    !TextUtils.isEmpty(cNumPyt.getText().toString())) 

{ 

a = Double.parseDouble(aNumPyt.getText().toString());
b = Double.parseDouble(bNumPyt.getText().toString());
c = Double.parseDouble(cNumPyt.getText().toString());


    if((a*a)==((b*b)+(c*c))){
        pythagorasResultFinal.setText(String.valueOf(a));
    }
    else if ((b*b)==((a*a)+(c*c))){
        pythagorasResultFinal.setText(String.valueOf(b));
    }
    else if ((c*c)==((a*a)+(b*b))){
        pythagorasResultFinal.setText(String.valueOf(c));
    }
}

}

答案 2 :(得分:0)

我发现了问题.Arc676的解决方案如下面的代码所示:

np.linalg.matrix_power(m, N-1)

我发现的另一个基本问题是我没有为c视图定义id。真的很小的问题导致它全部崩溃..

@Override
public void onClick(View v) {
    double a=0.0,b=0.0,c=0.0;
    double hypot = 0.0;

    try{
        a = Double.parseDouble(aNumPyt.getText().toString());
    }catch (NumberFormatException e){
        //oops, no number available
        a = 0.0; //default to 0 (or whatever you choose)
    }
    try{
        b = Double.parseDouble(bNumPyt.getText().toString());
    }catch (NumberFormatException e){
        //oops, no number available
        b = 0.0; //default to 0 (or whatever you choose)
    }
    try{
        c = Double.parseDouble(cNumPyt.getText().toString());
    }catch (NumberFormatException e){
        //oops, no number available
        c = 0.0; //default to 0 (or whatever you choose)
    }

    if (c == 0.0){
        hypot = Math.sqrt((Math.pow(a,2))+(Math.pow(b,2)));
        pythagorasResultFinal.setText("Finding C\n" + String.valueOf(hypot));
    }else if (a == 0.0){
        hypot = Math.sqrt((Math.pow(c,2))-(Math.pow(b,2)));
        pythagorasResultFinal.setText("Finding A\n" + String.valueOf(hypot));
    }else if (b == 0.0) {
        hypot = Math.sqrt((Math.pow(c,2))-(Math.pow(a,2)));
        pythagorasResultFinal.setText("Finding B\n" + String.valueOf(hypot));
    }
}

感谢您的快速反应和帮助! (y)的