简体   繁体   English

在 android studio 中,如何仅在特定情况下为我的计算器应用程序允许小数点?

[英]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.

相关问题 我如何只允许用户在计算器应用程序中输入小数点? - How would I only allow user to input one decimal point in calculator app? Android - 如何只允许一定数量的小数位 - Android - How to only allow a certain number of decimal places 我在计算器中实现小数点时遇到问题 - I'm having trouble implementing decimal point in my calculator 如何在我的计算器应用程序的 Edittext 中阻止重复的 fonts? - How do I block repeated fonts in Edittext in my calculator app? 如何在 Android Studio 中修复我的旧应用程序? - How do I fix my old app in Android Studio? 为什么我的小数点后面只有一位而不是两位? - Why do I only get one place behind my decimal point instead of two? java计算器小数点 - java calculator decimal point 在 Android Studio 中执行某个操作后,如何设置我的复选框默认选中? - How do I set my checkbox checked by default after a certain action is performed in Android Studio? 当达到特定分数时,如何在我的骰子游戏中添加警报对话框? Java,Android工作室 - How do I add an Alert Dialog to my Dice Game when a certain score is reached? Java, Android Studio 如何在Spring Boot Security OAuth2应用程序中仅对某些类启用OAuth2? - How do I enable OAuth2 for only certain classes in my Spring Boot Security OAuth2 app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM