【问题标题】:Android program crashes [duplicate]Android程序崩溃[重复]
【发布时间】:2013-11-26 20:27:38
【问题描述】:

感谢大家的帮助。所有问题都已排序!

【问题讨论】:

  • 下次少发代码。
  • 在发帖前尝试搜索问题。这是重复的。

标签: java android calculator


【解决方案1】:

永远不要使用== 在Java 中检查字符串是否相等。您必须始终使用

string1.equals(string2)

线

if (currentString == "+") 

总是会返回false,所以你必须使用

if (currentString.equals("+")) 

【讨论】:

  • 检查字符串时不使用 .equals() 的所有其他地方也是如此。
【解决方案2】:

问题是您在检查 c 是否为数字之前先执行Integer.toString(c)

另外,请注意不要将字符串与 == 进行比较,必须使用 .equals(),否则会得到意想不到的结果。

【讨论】:

  • c 是一个 int 开头(它的 for 循环计数器),所以这应该不是问题。
【解决方案3】:

我认为问题在于您的 IF 在此片段中返回“false”:

    //Check if the element is a sign
    if(currentElement == "+" || currentElement == "-" || currentElement == "*" || currentElement == "/")
    {
        Log.i("INFORMATION", "Sign found!");
        currentSign = currentElement; //Save into temporary storage to be used next loop
    }
    else // Element is a number
    {
        currentNumber = Double.parseDouble( currentElement ); //Convert number to integer
    }

因此,当您尝试使用 currentElement="+" 执行 Double.parseDouble(currentElement) 时会出现致命错误。

我会使用字符串比较而不是“==”,如下所示:

if("+".equals(currentElement) || "-".equals(currentElement) ... etc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2016-08-27
    • 2014-09-19
    相关资源
    最近更新 更多