【发布时间】:2018-02-04 13:08:44
【问题描述】:
目标:
比较字符串
问题:
当我使用语法代码 equals、equalsIgnoreCase 和 compareTo 时,它不起作用。
我缺少的部分是什么?
信息:
*我是安卓新手
谢谢!
新图片:
private String _EASY = "Easy", _MEDIUM = "Medium", _HARD = "Hard";
public void calculateMath(View view)
{
TextView status = (TextView)findViewById(R.id.txtvw_status);
String a1 = status.getText().toString();
String a2 = _EASY;
if (status.getText().toString().equals(_EASY));
{
int ffsdf = 23;
}
if (a1.equals(a2));
{
int ffsdf = 23;
}
if (a1.equalsIgnoreCase(a2))
{
int ffsdf = 23;
}
if (a1.compareTo(a2) == 0)
{
int ffsdf = 23;
}
}
布局/activity_play.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtvw_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="50sp"
android:text="Easy"
android:textSize="30sp" />
<Button
android:id="@+id/btn_calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="calculateMath"
android:text="Calculate" />
</LinearLayout>
【问题讨论】:
-
尝试打印出
compareTo的返回值。 -
我上传了一张关于compare返回值的新图
-
要确认这不是 Android Studio 中的错误,您可以在每个 if 语句的主体中添加一个打印语句吗?
-
你有我可以粘贴的代码吗?
-
if (a1.compareTo(a2) == 0) { int ffsdf = 23; System.out.println("if 语句内部!"); }