简体   繁体   English

无法在android中获得正确的输出

[英]Cannot get correct output in android

I have been trying to take three inputs based on what I will identify the type of triangle, ie Equivalent, Isosceles or Scalene but it doesn't just seem to work.我一直在尝试根据我将识别的三角形类型(即等效、等腰线或不等边线)进行三个输入,但它似乎并不起作用。 Kindly go easy on me as I am a newbie, help is appreciated in advance.请对我放轻松,因为我是新手,提前感谢您的帮助。

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

import androidx.appcompat.app.AppCompatActivity;

public class TriangleActivity extends AppCompatActivity {


    EditText et_x, et_y, et_z;
    Button btnCheck;
    TextView tvResult;


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

        et_x = (EditText) findViewById(R.id.et_x);


        et_y = (EditText) findViewById(R.id.et_y);


        et_z = (EditText) findViewById(R.id.et_z);


        btnCheck = (Button) findViewById(R.id.btn_check);

       // tvResult = (TextView) findViewById(R.id.tv_result);

    }


    public void checkResult(View view) {

//check if it's equivalent
        if(et_x == et_y && et_y == et_z)
        {
            Toast.makeText(this, "Equivalent.", Toast.LENGTH_SHORT).show();
        }
//check if it's Isosceles
        else if (et_x == et_y || et_x == et_z || et_y == et_z)
        {
            Toast.makeText(this, "Isosceles.", Toast.LENGTH_SHORT).show();
        }
//check if it's Scalene
        else if (et_x != et_y && et_x != et_z && et_y != et_z)
        {
            Toast.makeText(this, "Scalene", Toast.LENGTH_SHORT).show();
        }
//check if it's none
        else
        {
            Toast.makeText(this, "Not a triangle.", Toast.LENGTH_SHORT).show();
        }

    }

}

And here is the xml file, can't figure out if the problem really lies here somehow or not.这是xml文件,无法弄清楚问题是否真的出在此处。

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

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="Value of X"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/et_x"
        android:layout_width="78dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView9"
        android:layout_marginTop="58dp"
        android:layout_marginLeft="10dp"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="100dp"
        android:layout_height="45dp"
        android:layout_marginStart="54dp"
        android:layout_marginTop="10dp"
        android:layout_toEndOf="@+id/textView9"
        android:text="Value of Y"
        android:textSize="20dp" />



    <EditText
        android:id="@+id/et_y"
        android:layout_width="87dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView10"
        android:layout_marginStart="79dp"
        android:layout_marginTop="57dp"
        android:layout_toEndOf="@+id/et_x"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginStart="55dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_toEndOf="@+id/textView10"
        android:text="Value of Z"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/et_z"
        android:layout_width="84dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView11"
        android:layout_marginStart="62dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="59dp"
        android:layout_toEndOf="@+id/et_y"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_y"
        android:layout_alignEnd="@+id/et_y"
        android:layout_marginTop="314dp"
        android:layout_marginEnd="-1dp"
        android:onClick="checkResult"
        android:text="Check" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="175dp"
        android:layout_height="41dp"
        android:layout_below="@+id/btn_check"
        android:layout_alignStart="@+id/btn_check"
        android:layout_alignEnd="@+id/btn_check"
        android:layout_marginStart="-45dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="-41dp"
        android:hint="Result"
        android:textSize="10dp" />
</RelativeLayout>

you dont compare the inuts of the EditText.你不比较 EditText 的输入。 you compare the object你比较对象

public void checkResult(View view) {

    //check if it's equivalent
            if(et_x.getText() == et_y.getText() && et_y.getText() == et_z.getText())
            {
                Toast.makeText(this, "Equivalent.", Toast.LENGTH_SHORT).show();
            }
    //check if it's Isosceles
            else if (et_x.getText()== et_y.getText() || et_x.getText() == et_z.getText() || et_y.getText() == et_z.getText())
            {
                Toast.makeText(this, "Isosceles.", Toast.LENGTH_SHORT).show();
            }
    //check if it's Scalene
            else if (et_x.getText() != et_y.getText() && et_x.getText() != et_z.getText() && et_y.getText() != et_z.getText())
            {
                Toast.makeText(this, "Scalene", Toast.LENGTH_SHORT).show();
            }
    //check if it's none
            else
            {
                Toast.makeText(this, "Not a triangle.", Toast.LENGTH_SHORT).show();
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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