Java 对于控制台计算器程序,如何将字符串数组(即数字)拆分为字符串数组

Java 对于控制台计算器程序,如何将字符串数组(即数字)拆分为字符串数组,java,Java,我正在用Java编写计算器的命令行应用程序。我需要能够(根据程序要求)将字符串数组(6+3)拆分为字符串数组?那怎么办?我知道你可以使用split方法,但我真的不确定如何使用split方法来进行数学运算 public class Calculator { //public String input ="foo"; public static void main(String[] args) { String input = ""; // initalize the string

我正在用Java编写计算器的命令行应用程序。我需要能够(根据程序要求)将字符串数组(6+3)拆分为字符串数组?那怎么办?我知道你可以使用split方法,但我真的不确定如何使用split方法来进行数学运算

public class Calculator {
//public String input ="foo";

public static void main(String[] args) {
    String input = ""; // initalize the string
    boolean isOn = true; // used for the while loop...when false, the program will exit.
    String exitCommand = "Exit"; // exit command



    System.out.print("Enter a math problem"); // dummy test
    Scanner keyboard = new Scanner(System.in);
    //input = keyboard.nextLine();
    String[] token = input.split(("(?<=[-+*/])|(?=[-+*/])"));
    while (isOn) {
        for (int i = 0; i < token.length; i++) {
            //System.out.println(token[i]);
            Double d = Double.parseDouble(keyboard.nextLine()); //This causes an error
            //String[] token = d.split(("(?<=[-+*/])|(?=[-+*/])"));
            //input = keyboard.nextLine();

            if (input.equalsIgnoreCase(exitCommand)) {
                // if the user enters exit(ignored case) the boolean goes to false, closing the application
                isOn = false;
            }

           System.out.print(token[0] + d); // shows the math problem(which would by the end of the coding should show the
            //answer to the entered math problem.

        }


    }
}
public void validOperator() {
    ArrayList<String> operator = new ArrayList<String>();
    operator.add("+");
    operator.add("-");
    operator.add("*");
    operator.add("/");

}
public void validOperands(){
    ArrayList<String> operand = new ArrayList<String>();
    operand.add("0");
    operand.add("1");
    operand.add("2");
    operand.add("3");
    operand.add("4");
    operand.add("5");
    operand.add("6");
    operand.add("7");
    operand.add("8");
    operand.add("9");
}
附带说明:对于这个应用程序,如何让程序接受各种不同长度的字符串来执行数学运算

public class Calculator {
//public String input ="foo";

public static void main(String[] args) {
    String input = ""; // initalize the string
    boolean isOn = true; // used for the while loop...when false, the program will exit.
    String exitCommand = "Exit"; // exit command



    System.out.print("Enter a math problem"); // dummy test
    Scanner keyboard = new Scanner(System.in);
    //input = keyboard.nextLine();
    String[] token = input.split(("(?<=[-+*/])|(?=[-+*/])"));
    while (isOn) {
        for (int i = 0; i < token.length; i++) {
            //System.out.println(token[i]);
            Double d = Double.parseDouble(keyboard.nextLine()); //This causes an error
            //String[] token = d.split(("(?<=[-+*/])|(?=[-+*/])"));
            //input = keyboard.nextLine();

            if (input.equalsIgnoreCase(exitCommand)) {
                // if the user enters exit(ignored case) the boolean goes to false, closing the application
                isOn = false;
            }

           System.out.print(token[0] + d); // shows the math problem(which would by the end of the coding should show the
            //answer to the entered math problem.

        }


    }
}
public void validOperator() {
    ArrayList<String> operator = new ArrayList<String>();
    operator.add("+");
    operator.add("-");
    operator.add("*");
    operator.add("/");

}
public void validOperands(){
    ArrayList<String> operand = new ArrayList<String>();
    operand.add("0");
    operand.add("1");
    operand.add("2");
    operand.add("3");
    operand.add("4");
    operand.add("5");
    operand.add("6");
    operand.add("7");
    operand.add("8");
    operand.add("9");
}
公共类计算器{
//公共字符串输入=“foo”;
公共静态void main(字符串[]args){
字符串输入=”;//初始化字符串
布尔值isOn=true;//用于while循环…如果为false,程序将退出。
字符串exitCommand=“Exit”;//Exit命令
System.out.print(“输入数学问题”);//虚拟测试
扫描仪键盘=新扫描仪(System.in);
//输入=键盘.nextLine();

String[]token=input.split((“(?有点模糊,但假设您试图将运算符与数字分开

新字符串(“6+3”).split(\+”)应该给出两个数字


Split采用正则表达式,因此您可以将正在处理的所有运算符放在正则表达式中,并获取所有数字

我正在从用户处获取输入,那么如果用户输入3*4/9+3…等,该如何处理(我可能对我的问题措词不当…长时间工作lol我很抱歉)呢?这就是我的字符串userProblem=“2+2“String[]token=userProblem.split(“//s+-*/”);正则表达式必须包含您可能获得的任何可能的运算符。然而,这并不意味着它在图形上是正确的-如果您连续使用+,您仍然会获得一个数字数组,但语法没有意义-这需要一个简单的语言解析器(这比分割更大)你只接受数字0到9吗?所以如果用户输入是代码> 25–4 + 162 < /代码>考虑无效吗?