1

この形式 [ 1+2/3+4] の文字列を読み取る宿題があり、最初の 3 つの数字と最初の 2 つの演算子である 5 つの変数を格納する必要があります。私の質問は、文字列をループして値を別の変数に保存する方法です。これは私がこれまでに持っているものです。

公開クラス式評価 {

static String expression;
static double o1;
static double o2;
private static double o3;
static char operator1;
private static char operator2;

public static  String getOperand(String s) {
    s = s.trim();
    String num = "";



    while (s.length() > 0 && s.charAt(0) >= '0' && s.charAt(0) <= '9') {



     num = num + s.charAt(0);
     s = s.substring(1);

    }

    expression = s;
    return (num);
}

public static char getOperator(String s) {
    s = s.trim();
    char r = 0;
    int i = 0;
    while (s.length() > 0 && s.charAt(0) >= '0' && s.charAt(0) <= '9') {

        r = s.charAt(i);
        s = s.substring(i+1);

        i++;
    }



    return (r);
}


public static double add(double a, double b) {

    return (a + b);

}

public static double sub(double a, double b) {

    return (a - b);

}

public static double mult(double a, double b) {

    return (a * b);

}

public static double div(double a, double b) {

    return (a / b);

}

public static double solveExpresion(String e) {

    double answer = 0;

    for(int i = 0;i< e.length();i++){

    String operand1;
    String operand2;

    operand1 = getOperand(e);
    o1 = Double.parseDouble(operand1);

    operand2 = getOperand(expression);
    o2 = Double.parseDouble(operand2);

    operator1 = getOperator(expression);





    }

    return (answer);


}
}

これは私のメインクラスです

    import java.util.Scanner;

    public class TestCalc {

public static void main(String args[]) {

    Scanner kb = new Scanner(System.in);

    String equation;
    System.out.println("Please enter your equation: ");
    equation = kb.nextLine();

    double newNum = ExpresionEvaluation.solveExpresion(equation);

    //System.out.println(newNum);

    System.out.println(ExpresionEvaluation.o1);
    System.out.println(ExpresionEvaluation.operator1);
    System.out.println(ExpresionEvaluation.o2);
    //System.out.println(ExpresionEvaluation.expression);
}

}

これを実行するたびに、空の文字列があると表示されます。

4

1 に答える 1

0

Matcher/を使用して、期待どおりに5つの変数を取得するための私の簡単な解決策は次のPatternとおりです。

    public static void main(String[] args) throws ParseException {
        String operation = getOperationFromInput();
        List<Integer> firstThreeNumbers = retrieveFirstThreeNumbers(operation);
        List<Character> firstTwoOperators = retrieveFirstTwoOperators(operation);
        printNumbersAndOperators(firstThreeNumbers, firstTwoOperators);
    }

    private static String getOperationFromInput() {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        return scanner.next();
    }

    private static List<Integer> retrieveFirstThreeNumbers(String operation) {
        List<Integer> firstThreeNumbers = new ArrayList<>();
        Pattern patternNumber = Pattern.compile("\\d");
        Matcher matcher = patternNumber.matcher(operation);
        for (int i = 0; i < 3 && matcher.find(); i++) {
            firstThreeNumbers.add(Integer.valueOf(matcher.group()));
        }
        return firstThreeNumbers;
    }

    private static List<Character> retrieveFirstTwoOperators(String operation) {
        List<Character> firstTwoOperators = new ArrayList<>();
        Pattern patternOperator = Pattern.compile("[+-/*]");
        Matcher matcher = patternOperator.matcher(operation);
        for (int i = 0; i < 2 && matcher.find(); i++) {
            firstTwoOperators.add(matcher.group().charAt(0));
        }
        return firstTwoOperators;
    }

    private static void printNumbersAndOperators(List<Integer> firstThreeNumbers, List<Character> firstTwoOperators) {
        System.out.println(firstThreeNumbers + " " + firstTwoOperators);
    }

このコードでは、最初の 3 つの数値を a に格納しList<Integer>、最初の 2 つの演算子を aに格納しList<Character>ます。

解決策を達成するには複数の方法があるため、宿題として、別の方法を見つけてみてください:)

于 2012-11-02T00:11:08.080 に答える