【问题标题】:If statement not working for android programming [duplicate]如果语句不适用于android编程[重复]
【发布时间】:2014-09-21 07:30:00
【问题描述】:

我正在尝试为我的作业创建一种三角形应用程序,但是当我尝试在 java 中使用 if 语句时,由于某种原因,当我单击“生成按钮”时,它继续显示“不等边三角形:没有全等边”,即使我的输入是:2,2,3,应该是等腰三角形。请帮忙。我的逻辑是错的还是什么?非常感谢。

Java 代码: 包 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代码

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

【问题讨论】:

  • == 运算符是可传递的,您不需要:a == b &amp;&amp; a == c &amp;&amp; b == c - 执行 2 次比较就足够了:a == b &amp;&amp; a == c。至于你的问题:将 a、b、c 的值打印到 logcat 中,看看你所拥有的是否是你所期望的。
  • 嗨,我在 logcat 上没有看到任何错误,一定是人为错误 :(
  • 您的最后一句话没有意义:如果您打印 a,b,c 并看到它们的值,您不应该对输入哪种“if”大小写感到惊讶。这样的事情怎么可能是人为错误???

标签: java android xml if-statement


【解决方案1】:

点击“生成按钮”,它会一直显示“ Scalene 三角形:没有全等边"

因为您在“生成按钮”的onClick 之前获取所有 EditText 值。

从按钮的 onClick 解决问题调用 EditText.getText()

【讨论】:

    【解决方案2】:

    您正在比较字符串引用,而不是数字。在进行任何比较之前,您应该使用 Integer.parseInt(或 Float.parseFloat,如果您允许浮点值)来转换用户输入的文本。

    【讨论】:

    • 嗨,我尝试更改每个人的输入,但它仍然给我相同的结果,现在它只说“所有方面都是平等的”。这是我的新代码。 Input1 = (EditText) findViewById(R.id.editText1); int a=Integer.parseInt(Input1.getText().toString()); Input2 = (EditText) findViewById(R.id.editText2); int b=Integer.parseInt(Input1.getText().toString()); Input3 = (EditText) findViewById(R.id.editText3); int c=Integer.parseInt(Input1.getText().toString()); Display = (TextView) findViewById(R.id.textView5);
    猜你喜欢
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多