Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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_Eclipse_String_Math_Operators - Fatal编程技术网

Java 从字符串中获取运算符符号

Java 从字符串中获取运算符符号,java,eclipse,string,math,operators,Java,Eclipse,String,Math,Operators,如果我把方程7+13=20作为一个字符串,我将如何从方程中得到运算符符号+ String operator = fileContent.substring(fileContent.indexOf('+')); 我尝试了一些类似的方法,并对其中的一些内容进行了更改,但我一直无法让接线员退出。我只能让它返回像+13=20或13=20这样的东西,但不能让操作符单独返回 我不能为此使用正则表达式、try/catch、数组或SystemTokenizer 此外,数字之间不会总是有空格,也不会总是只有一两

如果我把方程7+13=20作为一个字符串,我将如何从方程中得到运算符符号+

String operator = fileContent.substring(fileContent.indexOf('+'));
我尝试了一些类似的方法,并对其中的一些内容进行了更改,但我一直无法让接线员退出。我只能让它返回像+13=20或13=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和13,然后打印20作为答案

删除所有其他内容:

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

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

删除所有其他内容:

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

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

此答案将给出输出+。考虑到您的变量名是operator,我假设这就是您想要的

使用子字符串:

或者写得更通俗易懂:

int index = fileContent.indexOf('+');
String operator = fileContent.substring(index, index + 1);
int index = fileContent.indexOf('+');
char operator = fileContent.charAt(index);
使用字符:

或者写得更通俗易懂:

int index = fileContent.indexOf('+');
String operator = fileContent.substring(index, index + 1);
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());
现在我们有了字符串运算符=+;和int first=7;和int秒=13

。这只是使用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");
}

在这种情况下,运算符=+;,输出为20。如果运算符=-;,输出为-6。

此答案将给出输出+。考虑到您的变量名是operator,我假设这就是您想要的

使用子字符串:

或者写得更通俗易懂:

int index = fileContent.indexOf('+');
String operator = fileContent.substring(index, index + 1);
int index = fileContent.indexOf('+');
char operator = fileContent.charAt(index);
使用字符:

或者写得更通俗易懂:

int index = fileContent.indexOf('+');
String operator = fileContent.substring(index, index + 1);
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());
现在我们有了字符串运算符=+;和int first=7;和int秒=13

。这只是使用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");
}

在这种情况下,运算符=+;,输出为20。如果运算符=-;,输出为-6。

这是什么愚蠢的要求?使用两个子字符串。一个用于+之前,一个用于+之后。连接这些字符串。为什么不能使用正则表达式、try/catch、数组或SystemTokenizer?你的导师在最后几节课上教你的东西可能就是你应该使用的东西。此外,您应该发布代码,说明您尝试了什么。请显示预期的输出是什么。我仍然不清楚您想要什么。我需要它取7和13,然后打印20作为答案,这是什么意思?字符串是=7 13,你想让它输出20吗?我需要它读7和13,然后读算符是什么,然后像一道普通的数学题一样打印出答案。比如7+13和打印20,或者2*3和打印6这是什么愚蠢的要求?使用两个子字符串。一个用于+之前,一个用于+之后。连接这些字符串。为什么不能使用正则表达式、try/catch、数组或SystemTokenizer?你的导师在最后几节课上教你的东西可能就是你应该使用的东西。此外,您应该发布代码,说明您尝试了什么。请显示预期的输出是什么。我仍然不清楚您想要什么。我需要它取7和13,然后打印20作为答案,这是什么意思?字符串是=7 13,你想让它输出20吗?我需要它读7和13,然后读算符是什么,然后像一道普通的数学题一样打印出答案。像7+13和打印20,或者2*3和打印6,当然,明智的答案是不允许的。闻起来像家庭作业。当然,明智的答案是不允许的。闻起来像是家庭作业。虽然我知道你在做什么,但它实际上并不适用于我已有的,但它让我知道我需要为这个问题做些什么。@CherryBomb95再次检查新的更新。并让我知道如何更改代码以提供所需的输出
工作正常,但存在错误,因为我从文件中获取数据。除此之外,它工作得很好。对于不同的运算符,您需要检查每个运算符上的contains,或者在使用索引生成子字符串之前,先生成一行indexOf并检查运算符index>-1。Stringcontains只返回indexOfstr>-1,因此最好检查indexOf的返回值,因为contains是多余的。@Radiodef Yup。我只是一开始不知道他在问什么,后来也不想再问了。他自己可以做到;我的算法回答了他的问题。谢谢当我明白你在做什么时,它实际上并不适用于我已经有的东西,但它让我知道我需要为这个问题做些什么。@CherryBomb95再次检查新的更新。并随时向我通报如何更改代码以提供所需的输出。这是可行的,但也有错误,因为我从文件中获取数据。除此之外,它工作得很好。对于不同的运算符,您需要检查每个运算符上的contains,或者在使用索引生成子字符串之前,先生成一行indexOf并检查运算符index>-1。Stringcontains只返回indexOfstr>-1,因此最好检查indexOf的返回值,因为contains是多余的。@Radiodef Yup。我只是一开始不知道他在问什么,后来也不想再问了。他自己可以做到;我的算法回答了他的问题。谢谢