Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在字符串中循环以检查值。(Java计算器)_Java_Arrays_String_Loops - Fatal编程技术网

在字符串中循环以检查值。(Java计算器)

在字符串中循环以检查值。(Java计算器),java,arrays,string,loops,Java,Arrays,String,Loops,我正在尝试编写一个方法,该方法将查看sting并将字符串中的任何数字添加到calcOperand数组中,并将任何运算符添加到calcOperator数组中。这是我的Javat计算器 下面是我一直坚持的代码。您可以看到,我试图创建一个方法,将字符串拆分为一个数组,并在其中循环。然后我想检查字符串,看看它是运算符还是操作数。最好的方法是什么 import java.util.*; public class stringCalculator { private String userinput; p

我正在尝试编写一个方法,该方法将查看sting并将字符串中的任何数字添加到
calcOperand
数组中,并将任何运算符添加到
calcOperator
数组中。这是我的Javat计算器

下面是我一直坚持的代码。您可以看到,我试图创建一个方法,将
字符串
拆分为一个数组,并在其中循环。然后我想检查
字符串
,看看它是运算符还是操作数。最好的方法是什么

import java.util.*;
public class stringCalculator {

private String userinput;
private int[] calcOperator;
private char[] calcOperand;

public stringCalculator(String userinput){
    this.userinput = userinput;
    //System.out.println(userinput);
}   

//This function will check the input and return true if the user enters a correct expression.
public boolean checkInput(){
    boolean show = userinput.matches("[-+/*0-9]+");
    return show;
}

//This function will add any numbers in the string to the calcOperand array.
//and any operators to the calcOperator field.
public void parseInput(){
    String[] theinput = userinput.split("");

    for (int i = 0 ; i < theinput.length ; i++){
        if(theinput.charAt(i) == "+" ){
            calcOperator.add("+");
        }
    }
}
}
import java.util.*;
公共类字符串计算器{
私有字符串用户输入;
私有int[]计算器;
私有字符[]calcOperand;
公共字符串计算器(字符串用户输入){
this.userinput=userinput;
//System.out.println(用户输入);
}   
//如果用户输入正确的表达式,此函数将检查输入并返回true。
公共布尔校验输入(){
boolean show=userinput.matches(“[-+/*0-9]+”);
回归演出;
}
//此函数将字符串中的任何数字添加到calcOperand数组中。
//以及calcOperator字段的所有运算符。
public void parseInput(){
字符串[]theinput=userinput.split(“”);
for(int i=0;i
您正在使用“”检查字符。我们对字符串使用
”。试试这个

if(theinput.charAt(i) == '+' )

另外,
calcOperator
是一个数组。数组没有
add
方法。我认为应该使用。

输入是一个字符串数组,不能对字符串数组调用charAt()方法。这就是代码的问题所在。请尝试下面的代码,其中数组的字符串分别与字符串“+”进行比较

String[]theinput=userinput.split(“”);
for(int i=0;i
我想你应该把这样的表达式“4+2-1/3”分解成

  • int[]计算器={4,2,1,3}
  • char[]calcOperand={'+','-','/'}
对吧?

你几乎做对了!检查用户输入,从头到尾创建子字符串(不要拆分字符串)

提示:使用动态数据对象而不是数组

private ArrayList<Integer> calcOperator = new ArrayList<Integer>();
private ArrayList<Character> calcOperand = new ArrayList<Character>();

public void parseInput(){
    int start = 0;
    int end = 0;
    for (int i = 0; i < input.length(); i ++){
        if (input.charAt(i) == '+'){
            end = i;
            String operator = input.substring(start, end).trim();
            int opAsInt = Integer.parseInt(operator);
            char operand = '+'; //as defined in the if-clause

            calcOperator.add(opAsInt);
            calcOperand.add(operand);

            //set index
            start = i + 1;
        }           
    }
    //last operatore would be missing right now, so add it!
    String operator = input.substring(start, input.length() ).trim();
    int opAsInt = Integer.parseInt(operator);
    calcOperator.add(opAsInt);
}
private ArrayList calcOperator=new ArrayList();
private ArrayList calcOperand=new ArrayList();
public void parseInput(){
int start=0;
int end=0;
对于(int i=0;i
将calcOperand数组中的操作数存储在checkInput()方法的calcOperator数组中,如下所示:

public stringCalculator(String userinput) {
    this.userinput = userinput;
    checkInput();
}

// This function will check the input and return true if the user enters a
// correct expression.
public boolean checkInput() {
    String[] operators = userinput.split("[-+/*]"); 
    calcOperator = new int[operators.length];
    for (int i = 0; i < calcOperator.length; i++) {
        calcOperator[i] = Integer.parseInt(operators[i]);
    }
    String[] operands = userinput.split("[0-9]");
    calcOperand = new char[operands.length];
    for (int i = 0; i < operands.length; i++) {
        if (operands[i].length() > 0) {
            calcOperand[i] = operands[i].charAt(0);
        }
    }
    return true;
}
公共字符串计算器(字符串用户输入){
this.userinput=userinput;
checkInput();
}
//此函数将检查输入,如果用户输入
//正确的表达。
公共布尔校验输入(){
String[]operators=userinput.split(“[-+/*]”);
calcOperator=new int[operators.length];
对于(int i=0;i0){
calcOperand[i]=操作数[i]。字符(0);
}
}
返回true;
}

它是工作代码吗?如果是,则在Hello@Ciaran Ashton中发布此内容以供查看,您还需要执行其他操作“-”和“/”,甚至“%”?
public stringCalculator(String userinput) {
    this.userinput = userinput;
    checkInput();
}

// This function will check the input and return true if the user enters a
// correct expression.
public boolean checkInput() {
    String[] operators = userinput.split("[-+/*]"); 
    calcOperator = new int[operators.length];
    for (int i = 0; i < calcOperator.length; i++) {
        calcOperator[i] = Integer.parseInt(operators[i]);
    }
    String[] operands = userinput.split("[0-9]");
    calcOperand = new char[operands.length];
    for (int i = 0; i < operands.length; i++) {
        if (operands[i].length() > 0) {
            calcOperand[i] = operands[i].charAt(0);
        }
    }
    return true;
}