[英]How do I allow a decimal point only in certain circumstance for my calculator app in android studio?
I am trying to figure out on how to allow only a decimal point for every decimal number.我想弄清楚如何只允许每个小数点有一个小数点。
I have been able to allow only 1 decimal on the output when the user is inputting his equation, but I can't figure out how to allow the user to input a decimal point whenever he wants to have more than 1 decimal number on the screen.当用户输入他的方程式时,我已经能够在 output 上只允许一位小数,但我无法弄清楚如何允许用户在他想要在屏幕上输入多于 1 位小数时输入小数点. At this moment, my code only allows only 1 decimal point on screen.
目前,我的代码只允许在屏幕上显示 1 个小数点。
How do I change this to allow it for every number without adding decimal points in incorrect places?我如何更改它以允许它用于每个数字而不在不正确的位置添加小数点?
I am assuming this is a string you are trying to manipulate.我假设这是您要操作的字符串。 Ex "10.5+6*7.0".
例如“10.5+6*7.0”。 I will assume you are storing the user input in a StringBuilder.
我假设您将用户输入存储在 StringBuilder 中。
Every time the user types a character you append it to the StringBuilder.每次用户键入一个字符时,您 append 将其传递给 StringBuilder。 Always keep track of the rightmost "number delimiter" in the string.
始终跟踪字符串中最右边的“数字定界符”。 Delimiter Examples: +-*/%()[].
定界符示例:+-*/%()[]。 You know the current number being typed begins after the rightmost delimiter.
您知道当前键入的数字从最右边的定界符之后开始。 Check the chars from delimiter to string length to see if there is already a decimal in the number.
检查从定界符到字符串长度的字符,看看数字中是否已经有小数。
import java.util.*;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
class Foo {
int rightmostDelimiterIdx = 0;
List<Character> delimiters = Arrays.asList('+', '-', '*', '/', '%', '(', ')', '[', ']');
public static void main(String[] args){
Foo foo = new Foo();
try {
foo.run();
} catch (ScriptException e) {
e.printStackTrace();
}
}
public void run() throws ScriptException
{
StringBuilder sb = new StringBuilder(); // store user input
ScriptEngineManager mgr = new ScriptEngineManager(); // evaluate expression with JavaScript
ScriptEngine engine = mgr.getEngineByName("JavaScript");
// User types:5.0*7
charTyped(sb, '5');
charTyped(sb,'.');
charTyped(sb,'0');
charTyped(sb,'*');
charTyped(sb,'7');
System.out.println(engine.eval(sb.toString()));
sb.setLength(0);
// User types:8+2.3.3
charTyped(sb, '8');
charTyped(sb,'+');
charTyped(sb,'2');
charTyped(sb,'.');
charTyped(sb,'3');
charTyped(sb,'.'); // second decimal will be rejected, thus string will be "8+2.33"
charTyped(sb,'3');
System.out.println(engine.eval(sb.toString()));
}
// appends c to sb if valid operation
public void charTyped(StringBuilder sb, char c)
{
if(delimiters.contains(c)) {
rightmostDelimiterIdx = sb.length();
} else if(c == '.' && sb.indexOf(".", rightmostDelimiterIdx) != -1) {
return; // do not add '.' b/c already exists in number
}
sb.append(c);
}
public void charDeleted(StringBuilder sb, char c) {
// I leave implementation up to you
}
}
Output: Output:
35.0 35.0
10.33 10.33
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.