gpt4 book ai didi

java - 在android计算器中一次计算多个操作

转载 作者:行者123 更新时间:2023-12-02 07:22:30 25 4
gpt4 key购买 nike

我想在计算器中进行多项运算,例如:2+3*3/7。怎么做。谁能帮我。计算器的单一操作即可工作。

如何在一个 editText 中计算多个操作。此代码添加在 equal 按钮单击方法上。

下面是我的代码:

  btnEqualto.setOnClickListener(new OnClickListener() 

{
@Override

public void onClick(View v)

{

String str2 = edtxt.getText().toString();

if (buttonPressed.equals("+"))

{
Log.e(TAG,"dot operation="+strDot);


if (strDot.equals("."))

{
Log.e(TAG, "If Codition :");


String str1[] = str2.split("[+]");

Log.e(TAG,"str1 ="+str1);

String strVal11 = str1[0];

String strVal22 = str1[1];

Log.e(TAG, "strValues :" + strVal11 + " & " + strVal22);

Double xx = Double.parseDouble(strVal11);

Double yy = Double.parseDouble(strVal22);

Log.e(TAG, "Value of x :" + xx + " AND y : " + yy);

Log.e(TAG, "dot operation");


Double result = xx + yy;

Log.e(TAG, "Result :" + result);

edtxt.setText(result + "");


Log.e(TAG, "VAlue of Result :" + edtxt);

}

else

{

Log.e(TAG, "If Codition :"+strVal2);

strDot = "";

String str1[] = str2.split("[+]");

strVal1 = str1[0];

strVal2 = str1[1];


Log.e(TAG, "Esle Condition 4 Pllus strValues :" + strVal1 + " & " + strVal2);


x = Integer.parseInt(strVal1);

y = Integer.parseInt(strVal2);

Log.e(TAG, "Value of x :" + x + " AND y : " + y);

int result = x + y;

Log.e(TAG, "Result :" + result);

edtxt.setText(result + "");
Log.e(TAG, "VAlue of Result :" + edtxt);

}

}

}

}

);

最佳答案

尝试exp4j:a simple mathematical expression parser for java..

exp4j takes a mathematical term in human readable infix notation and evaluate it's result by first converting the input through Dijkstra's Shunting Yard Algorithm to postfix (RNP) notation and evaluating the postfix term by a simple Stack Algorithm.

尝试一下,看看这是否适合您..

关于java - 在android计算器中一次计算多个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14018531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com