gpt4 book ai didi

java - 从字符串中删除最后一个数字

转载 作者:行者123 更新时间:2023-12-02 01:10:05 35 4
gpt4 key购买 nike

我正在 Android XML 应用程序中创建一个 Java 计算器(仅按钮 - 如 T9 键),并且我想修复“取消”按钮。

当我单击它时,我想取消仅包含数字的字符串中的最后一位数字(或“.”,因为您还可以添加“.”来写入十进制数字)。有一个问题:如果用户写入一个 int 数字(34),我将其转换为 double,它会变成 34.0,如果我取消最后一位数字,它会取消 0。

我也尝试过使用String.substring(start,end),但它不起作用......您对取消句柄有什么建议吗?谢谢!

这是取消最后一位数字的函数。

/* tv -> TextView
- tvResult contains what the user writes and the result when he clicks "=", - tvCalc contains the calc that the user is entering.
For example, if the user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84" */

public void handleCancel(View v) {
//tv -> TextView, tvResult contains what the user writes and the result when he clicks "=", tvCalc contains the calc that the user is entering: for example if user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84"

if (tvResult.length() == 0) {
errorToast = Toast.makeText(MainActivity.this, "Error! There's nothing to cancel!", Toast.LENGTH_LONG);
errorToast.show();
} else {
tvResult.setText(tvResult.toString().substring(0, tvResult.toString().length() - 1))

if (tvCalc.toString().contains("=")) {
tvCalc.setText(tvResult.toString());
operand1 = tvResult.toString();
} else {
tvCalc.setText(tvCalc.toString().substring(0, tvCalc.toString().length() - 1));
if (operator == "") operand1 = tvResult.toString();
else {
operand2 = tvResult.toString();
try {
int conv = Integer.parseInt(tvCalcolo.toString());
operazione = "";
} catch(Exception e) {}
}
}
}
}

添加用户选择的功能:

public void userChoice(View v)
{
Button clicked=(Button)findViewById(v.getId());
String choice=clicked.getText().toString();
try
{
int number=Integer.parseInt(choice);
if (operator=="")
{
operand1+=choice;
if (tvCalc.toString().contains("=")) tvCalc.setText(operand1);
} else operand2+=choice;
} catch (Exception e)
{
if (choice.equals('.'))
{
if (operator=="") operand1+=choice;
else operand2+=choice;
} else
{
if (operand2!="")
{
handleEqual(v);
operand1=tvResult.toString();
}
operator=choice;
tvCalc.append(operator);
tvResult.setText("");
return;
}
}

tvCalc.append(choice);
tvResult.append(choice);
}

感谢 Cardinal System,我编辑了函数“handleCancel”:

public void handleCancel (View v)
{

double d;
try
{
d=Double.parseDouble(tvRisultato.toString());
} catch (Exception e)
{
bCanc.setText("Error"); //It's the button 'Cancel'
}

String newVal;
if (d%1==0)
{
int val=(int)d;
newVal=String.valueOf(val);
newVal=newVal.substring(0,newVal.length()-1);
} else
{
newVal=String.valueOf(d);
newVal=newVal.substring(0,newVal.length()-1);
if (newVal.endsWith("."))
{
newVal=newVal.substring(0,newVal.length()-1);
}
}

tvResult.setText(newVal);

if ((tvCalc.toString().contains("+")||tvCalc.toString().contains("-")||tvCalc.toString().contains("*")||tvCalc.toString().contains("/")||tvCalc.toString().contains("^"))&&tvCalc.toString().contains("="))
{
tvCalc.setText(tvResult.toString());
operand1=tvResult.toString();
} else if ((tvCalc.toString().contains("+")||tvCalc.toString().contains("-")||tvCalc.toString().contains("*")||tvCalc.toString().contains("/")||tvCalc.toString().contains("^"))&&!tvCalc.toString().contains("="))
{
if (tvCalc.toString().endsWith("+")||tvCalc.toString().endsWith("-")||tvCalc.toString().endsWith("*")||tvCalc.toString().endsWith("/")||tvCalc.toString().endsWith("^")) operator="";
tvCalc.setText(tvCalc.toString().substring(0,tvCalc.toString().length()-1));
}
}

最佳答案

看看这是否对您有帮助:

// Let's make it a double so we have something to work with.
double d = Double.parseDouble(tvResult.getText().toString());

String newText;
if (d % 1 == 0) { // See if the number is a whole number
int i = (int) d; // If it is, cast it to an int to get rid of the decimal
newText = String.valueOf(i); // Parse it to a string so we can clip off the end
newText = newText.substring(0, newText.length() - 1); // Clip off the end
} else {
// If it's not a whole number, just parse it to a string.
newText = String.valueOf(d);
newText = newText.substring(0, newText.length() - 1); // Clip off the end
if (newText.endsWith(".")) {
// If the number we clipped off was a tenth, clip off the decimal
newText = newText.substring(0, newText.length() - 1);
}
}

tvResult.setText(newText);

关于java - 从字符串中删除最后一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534884/

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