If statement not working for android programming [duplicate]

☆樱花仙子☆ 提交于 2020-01-26 03:56:11

问题


I am trying to create a type of triangle app for my homework but when I tried to use if statement in java, for some reason, when I click on the "Generate Button", it keeps on showing the " Scalene Triangle: No Congruent Sides" , even thought my inputs are : 2,2,3 which should be an Isosceles Triangle. Please help. Is my logic wrong or something? Thanks a lot.

Java code: package com.example.trianglegame;

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

public class TriangleGame extends Activity {// implement-have to use all of the
                                        // methods
// set up the variables here

Button Gen;
EditText Input1;
EditText Input2;
EditText Input3;
TextView Output1;
TextView Output2;
TextView Output3;
TextView Display;

Editable a;
Editable b;
Editable c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    Input1 = (EditText) findViewById(R.id.editText1);
    a = Input1.getText();

    Input2 = (EditText) findViewById(R.id.editText2);
    b = Input2.getText();

    Input3 = (EditText) findViewById(R.id.editText3);
    c = Input3.getText();

    Display = (TextView) findViewById(R.id.textView5);

    // display edit text

    Gen = (Button) findViewById(R.id.button1);

    Gen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if ((a == b && b != c) || (a == c && b != c)
                    || (b == c && a != c)) {
                Display.setText("Isosceles Triangle: 2 Congruent Sides");
            } else if (a == b && a == c && b == c) {
                Display.setText("Equilateral Triangle:All sides are equal");
            }

            else if (a != b && a != c && b != c) {
                Display.setText("Scalene Triangle: No Congruent Sides");
            } else {
                Display.setText("Error");
            }

        }
    });

 }

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

xml code

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/enter_text" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/side_1" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/side_2" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/type_hint"
    android:text="@string/side_3" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/generate" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

回答1:


click on the "Generate Button", it keeps on showing the " Scalene Triangle: No Congruent Sides"

Because you are getting all EditText Values before onClick of "Generate Button".

To fix issue call EditText.getText() from onClick of Button




回答2:


You are comparing String references, not numbers. You should use Integer.parseInt (or Float.parseFloat if you allow float values) to convert the text entered by the user before doing any comparisons.



来源:https://stackoverflow.com/questions/25956890/if-statement-not-working-for-android-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!