【问题标题】:how to read in a string and store into different variables with out using an array如何在不使用数组的情况下读取字符串并存储到不同的变量中
【发布时间】:2012-10-22 15:09:01
【问题描述】:

我有一个家庭作业,我应该以这种格式读取一个字符串 [1+2/3+4] 然后我必须存储 5 个变量,它们是前 3 个数字和前 2 个运算符。我的问题是如何遍历字符串并将值存储到不同的变量中。这就是我目前所拥有的。

公共类表达式评估{

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);
}

}

每次我运行它都说我有一个空字符串。

【问题讨论】:

  • 不可能。 Java 字符串包含一个char[],所以只要你使用一个字符串,你就在使用一个数组。

标签: java string store calculator


【解决方案1】:

这是我按预期检索 5 个变量的快速解决方案,使用 Matcher/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);
    }

在这段代码中,我将前三个数字存储在 List&lt;Integer&gt; 中,前两个运算符存储在 List&lt;Character&gt; 中。

有多种方法可以实现一个解决方案,所以作为一个家庭作业,尝试找到其他方法:)

【讨论】:

  • 问题是我不能使用它必须存储到变量中的任何类型的数组或列表进行计算,然后从输入字符串中再存储 3 个变量
猜你喜欢
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 2022-06-14
相关资源
最近更新 更多