我真的很想知道为什么53.0返回时,5.0应该返回这里为charAt(0).返回52.0而不是4.0的charAt(1)

  • 本文关键字:返回 charAt 这里 想知道 真的 java return-value
  • 更新时间 :
  • 英文 :


这是我打印结果值的测试类,因为它是错误的,并且想先解决它,如果postfix.charAt(0)在这里打印出来,我已经尝试过了,我确实得到了5,但这不是问题:

public class TestClass {
public String postfix = "54+" //the string as you can see charAt(0) is 5 and charAt(1) is 4
@Before 
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testEasyEvaluatePostfixExpression() throws InvalidNotationFormatException, StackOverflowException, StackUnderflowException {
double result = Notation.evaluatePostfixExpression(postfix); //assigns result to what is returned by calling the class Notation's evaluatePostfixExpression method passing the String postfix as parameter
System.out.println(result); //simply prints out result which is postfixEpr.charAt(0)
assertEquals(evalEasyPostfix, result, .001); //this should fail which it does anyway
}
}

这是要测试的类,当返回postfixexp . charat(0)时返回错误的数据,这里显示的53.0不存在:0:

public class Notation {
public static double evaluatePostfixExpression(String postfixExpr)
throws InvalidNotationFormatException, StackOverflowException, StackUnderflowException {
return Double.valueOf(postfixExpr.charAt(0)); //when I change return type to String and change result to string also I get "54+" which is what is expected the charAt() value is what is wrong.

}
}``

charAt()返回char原语,该原语被Double.valueOf()视为与其相关的ascii数。尝试先用Character.toString()char转换为字符串,或者只使用substring方法。

https://commons.wikimedia.org/wiki/File ASCII-Table-wide.svg

https://beginnersbook.com/2019/04/java-char-to-string-conversion/

最新更新