Я пытаюсь решить проблему с заданием на программирование и продолжаю получать ошибки.Я пробовал несколько способов исправить код самостоятельно, и ни один из них не сработал.На самом деле, это одна из самых сложных задач из 20.Вот как это работает:
Анализировать и оценивать простые математические задачи, возвращая ответ в виде целого числа.
1. Add two numbers together.
What is 1 plus 1? 2
Now, perform the other four operations:
1. Add two negative numbers together.
What is -1 plus -10? -11
2. Subtract 2 numbers together.
What is 4 minus -12? -16
3. Multiply two numbers together.
What is -3 multiplied by 25? -75
4. Divide two numbers together.
What is 33 divided by -3? -11
public int solveWordProblem(String string)
{
String[] SplitValues = string.split(" ");
switch(SplitValues[4])
{
case "plus":
final int LeftOperator = 1;
final int RightOperator = 1;
sum = Integer.parseInt(SplitValues[LeftOperator]) +
Integer.parseInt(SplitValues[RightOperator].
substring(2, SplitWords[RightOperator].length() - 1));
return sum;
case "plus(1)":
final int LeftOperator_1 = -1;
final int RightOperator_1 = -10;
sum1 = Integer.parseInt(SplitValues[LeftOperator_1]) +
Integer.parseInt(SplitValues[RightOperator_1].
substring(-11, SplitWords[RightOperator_1].length() - 1));
return sum1;
case "minus":
final int LeftOperator_2 = 4;
final int RightOperator_2 = -12;
diff = Integer.parseInt(SplitValues[LeftOperator_2]) -
Integer.parseInt(SplitValues[RightOperator_2].
substring(-16, SplitValues[RightOperator_1].length() - 1));
return diff;
case "multiplied":
final int LeftOperator_3 = -3;
final int RightOperator_3 = 25;
product = Integer.parseInt(SplitValues[LeftOperator_3]) *
Integer.parseInt(SplitValues[RightOperator_3].
substring(-75, SplitValues[RightOperator].length() - 1));
return product;
case "divided":
final int LeftOperator_3 = 33;
final int RightOperator_3 = -3;
quotient = Integer.parseInt(SplitValues[LeftOperator_4])/
Integer.parseInt(SplitValues[RightOperator_4].
substring(-11, SplitValues[RightOperator_4].length() - 1));
return quotient;
}
}
}
Основной / текущий код:
public int solveWordProblem(String string)
{
String[] SplitValues = string.split(" ");
String value = "";
if (SplitValues[3].equals("plus"))
{
for (int i = 2; i < SplitValues[4].length();)
{
switch(SplitValues[4].charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
final int LeftValue = 1;
final int RightValue = 1;
int sum = Integer.parseInt(SplitValues[LeftValue]) +
Integer.parseInt(SplitValues[RightValue]);
value += SplitValues[4].charAt(sum);
break;
default:
}
return i;
}
}
else if (SplitValues[3].equals("plus(1)"))
{
for (int i = -11; i < SplitValues[4].length();)
{
switch(SplitValues[4].charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
final int LeftValue_1 = -1;
final int RightValue_1 = -10;
int sum1 = Integer.parseInt(SplitValues[LeftValue_1]) +
Integer.parseInt(SplitValues[RightValue_1]);
value += SplitValues[4].charAt(sum1);
break;
default:
}
return i;
}
}
else if (SplitValues[3].equals("minus"))
{
for (int i = 16; i < SplitValues[4].length();)
{
switch(SplitValues[4].charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
final int LeftValue_2 = 4;
final int RightValue_2 = -12;
int diff = Integer.parseInt(SplitValues[LeftValue_2]) -
Integer.parseInt(SplitValues[RightValue_2]);
value += SplitValues[4].charAt(diff);
break;
default:
}
return i;
}
}
else if (SplitValues[2].equals("multiplied"))
{
for (int i = -75; i < SplitValues[4].length();)
{
switch(SplitValues[5].charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
final int LeftValue_3 = -3;
final int RightValue_3 = 25;
int product = Integer.parseInt(SplitValues[LeftValue_3]) *
Integer.parseInt(SplitValues[RightValue_3]);
value += SplitValues[5].charAt(product);
break;
default:
}
return i;
}
}
else if (SplitValues[3].equals("divided"))
{
for (int i = -11; i < SplitValues[4].length();)
{
switch(SplitValues[4].charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
final int LeftValue_4 = 33;
final int RightValue_4 = -3;
int quotient = Integer.parseInt(SplitValues[LeftValue_4])/
Integer.parseInt(SplitValues[RightValue_4]);
value += SplitValues[4].charAt(quotient);
break;
default:
}
return i;
}
}
return 0;
}
Test Code:
@Test
public void testSingleAddition()
{
assertEquals(2, evaluationService.solveWordProblem("What is 1 plus 1?"));
}
@Test
public void testSingleAdditionWithNegativeNumbers()
{
assertEquals(-11, evaluationService.solveWordProblem("What is -1 plus -10?"));
}
@Test
public void testSingleSubtraction()
{
assertEquals(16, evaluationService.solveWordProblem("What is 4 minus -12?"));
}
@Test
public void testSingleMultiplication()
{
assertEquals(-75, evaluationService.solveWordProblem("What is -3 multiplied by 25?"));
}
@Test
public void testSingleDivision()
{
assertEquals(-11, evaluationService.solveWordProblem("What is 33 divided by -3?"));
}
Ожидаемый результат:
What is 1 plus 1?
2
What is -1 plus -10?
-11
What is 4 minus -12?
16
What is -3 multiplied by 25?
-75
What is 33 divided by -3?
-11