System.out.print("Wrong. correct answer is " 5 + 7);
此输出:错误。正确答案是57我希望它输出:错误。正确答案是12
您的问题是,在这种情况下,您将两个字符串组合在一起:字符串5和字符串7。结果为57。如果要加两个数字,请使用以下代码:
int result = 5 + 7;
System.out.print("Wrong. correct answer is " + result);
我们也可以使用printf,它也允许格式化。
System.out.printf("Wrong. correct answer is %d", 5 + 7);