【发布时间】: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 && a == c && b == c- 执行 2 次比较就足够了:a == b && a == c。至于你的问题:将 a、b、c 的值打印到 logcat 中,看看你所拥有的是否是你所期望的。 -
嗨,我在 logcat 上没有看到任何错误,一定是人为错误 :(
-
您的最后一句话没有意义:如果您打印 a,b,c 并看到它们的值,您不应该对输入哪种“if”大小写感到惊讶。这样的事情怎么可能是人为错误???
标签: java android xml if-statement