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

问题描述 投票:-2回答:1

我一直在尝试根据要确定三角形类型的三个输入,即等效三角形,等腰三角形或Scalene三角形,但这似乎不起作用。当我是新手时,请对我轻松一点,请提前获得帮助。

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

    }

}

这是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>
java android android-studio input output
1个回答
0
投票

您不比较EditText的入口。您比较对象

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

        }
© www.soinside.com 2019 - 2024. All rights reserved.