【问题标题】:Compare strings doesn't work when I use syntax code equals, equalsIgnoreCase and compareTo当我使用语法代码 equals、equalsIgnoreCase 和 compareTo 时比较字符串不起作用
【发布时间】: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 语句内部!"); }

标签: android xml


【解决方案1】:

您的代码的问题在于ffsdf 的声明。您必须在 if 声明之外声明它。

这会起作用

 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;

    int ffsdf;

    if (a1.compareTo(a2) == 0)
    {
        ffsdf = 23;
    }

}

至于为什么会这样,参考这个问题Why can't variables be declared in an if statement?

【讨论】:

  • 您的解决方案有效,我在 C# 中进行编码时从未遇到过此类问题。谢谢你们的帮助!
【解决方案2】:

我在过去尝试比较字符串时遇到过类似的问题。尝试Objects.equals(a1,a2).equals() 测试值是否相等(它们在逻辑上是否“相等”)。

Objects.equals() 只是在调用 .equals() 之前检查空值,所以你不必这样做。

在我看来,当涉及到字符串时,您几乎总是希望使用 Objects.equals()。 您可以在此处找到有关 Objects 类的更多信息:Objects class

比较对象:

将此对象与指定对象进行比较以进行排序。返回一个 负整数、零或正整数,因为此对象较小 大于、等于或大于指定对象。

最适合默认形式的数值,您必须覆盖它才能比较字符串。可以找到有关覆盖的信息here (有关 compareTo 方法的更多信息可以找到here

【讨论】:

  • 谢谢你们的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多