【问题标题】:Getting an operator symbol out of a string从字符串中获取运算符符号
【发布时间】:2013-12-09 14:39:37
【问题描述】:

如果我将等式7 + 13 = 20 作为字符串,我将如何将运算符符号 + 排除在等式之外?

String operator = fileContent.substring(fileContent.indexOf('+'));

我尝试了一些类似的方法,并更改了其中的一些内容,但我无法让操作员退出。我只能让它返回 + 13 = 2013 = 20 之类的东西,但不能单独返回操作员。

我不能为此使用正则表达式、try/catch、数组或 SystemTokenizer。

此外,数字之间并不总是有空格,它们的大小也不总是只有一位或两位数。

最后一件事。我还需要它,以便如果将运算符放入 problem = (firstNumber + operator + secondNumber) 之类的东西中,它实际上可以计算数学。

预期的输出,或者至少我想要做的,是让它接受第一个和第二个数字,并读取运算符,以便它可以对问题进行数学运算。它真的很混乱,我很抱歉。

int first = Integer.parseInt(fileContent.substring(0, fileContent.indexOf(" ")));
int second = secondNumber(result, fileContent);
int last = Integer.parseInt(result.substring(result.indexOf("=") + 1));

这是我必须获取第一个、第二个和答案数字的代码,但我需要以一种可以将其放在第一个和第二个数字之间的方式取出运算符符号,以便它真正执行数学,所以我可以对照答案编号检查它。

它的作业将充当“数学问题评分者”,但没有一个运算符是相同的。

我需要它以7 and 13 并打印20 作为答案。

【问题讨论】:

  • 这是什么愚蠢的要求?使用两个子字符串。一个用于+ 之前,一个用于+ 之后。连接这些字符串。
  • 为什么不能使用正则表达式、try/catch、数组或 SystemTokenizer?无论您的讲师在最后几节课中教给您什么,都可能是您应该使用的。另外,你真的应该发布代码来表明你已经尝试过。
  • 请显示预期的输出是什么。
  • 我仍然对你想要什么感到困惑。 “我需要它取 7 和 13 并打印 20 作为答案”,这是什么意思?字符串是否将是 = "7 13" 并且您希望它输出 "20"?
  • 我需要它来读取 7 和 13,然后读取运算符是什么,并像普通数学问题一样打印出答案。比如 7 + 13 并打印 20,或 2 * 3 并打印 6

标签: java eclipse string math operators


【解决方案1】:

这个答案将给出输出+。考虑到您的变量名称是 operator,我认为这就是您想要的。

使用substring

String operator = fileContent.substring(fileContent.indexOf('+'), fileContent.indexOf('+') + 1);

或者写得更易读:

int index = fileContent.indexOf('+');
String operator = fileContent.substring(index, index + 1);

使用charAt

char operator = fileContent.charAt(fileContent.indexOf('+'));

或者写得更易读:

int index = fileContent.indexOf('+');
char operator = fileContent.charAt(index);

编辑

以下是查找第一个和第二个数字的方法:

String eqn = "7 + 13 = 20";

int operatorIndex = eqn.indexOf(operator); // already defined operator as a String
int equalIndex = eqn.indexOf('=');

int first = Integer.parseInt(eqn.substring(0, operatorIndex).trim());
int second = Integer.parseInt(eqn.substring(operatorIndex + 1, equalIndex).trim());

现在我们有 String operator = "+";int first = 7;int second = 13;

Click this sentence if you don't know what a switch statement is in Java。这只是使用 if 结构的另一种方式。这并不难理解。不过,如果你也需要我,我可以重写使用 if 结构。

我会用这个:

String operator = "+";

int first = 7; int second = 13;

switch (operator)
{
    case "+": 
        System.out.print("" + (first + second));
        break;

    case "-": 
        System.out.print("" + (first - second));
        break;

    case "*": 
        System.out.print("" + (first * second));
        break;

    case "/": 
        System.out.print("" + (first / second));
        break;

    default: System.out.print("not sum, difference, product or quotient");
}

operator = "+"; 的情况下,输出为20。如果operator = "-";,则输出为-6

【讨论】:

  • 虽然我了解您在做什么,但它实际上不适用于我已经拥有的东西,但它让我知道我需要为这个问题做些什么。
  • @CherryBomb95 再次检查新更新。并让我了解如何更改代码以提供所需的输出。
  • 它可以工作,但是因为我从文件中获取数据而出现错误。除此之外,它完美无缺。
  • 对于不同的运算符,您需要在每个运算符上检查contains,或者在使用索引创建子字符串之前创建一行indexOf 并检查是否operatorIndex > -1String#contains 只返回 indexOf(str) > -1,因此首选检查 indexOf 的返回值,因为 contains 是多余的。
  • @Radiodef 是的。我只是一开始不知道他在问什么,后来也不想这样做。他可以自己做;我的算法回答了他的问题。谢谢
【解决方案2】:

删除其他所有内容:

String operator = fileContent.replaceAll("[^-+*/]", "");;

请注意,减号必须在字符类中排在第一位或最后一位或被转义,否则它是一个范围运算符。

【讨论】:

  • 当然,明智的答案是不允许的。闻起来像家庭作业。
猜你喜欢
  • 2015-03-24
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
相关资源
最近更新 更多