gpt4 book ai didi

java - 格式化字符串时出现 FormatFlagsConversionMismatchException

转载 作者:行者123 更新时间:2023-12-04 11:02:10 26 4
gpt4 key购买 nike

我在我的程序中进行除法并将结果打印到屏幕上。运行程序时没有问题,但是当我尝试启动单元测试时收到以下错误:

java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = 0



在以下行:
String underscoreToResult = String.format("%" + (i - 2) + "s", "_");

下面我提供与此方法相关的所有代码:
private StringBuilder bodyOfMethodCalculateDivisionWhenDividentLessThanDivisor(StringBuilder result, Integer reminderInInteger, int divisor, int divident, Integer divisorMuplipliedByQuotient,
StringBuilder lastReminder, int i) {

String underscoreToResult = String.format("%" + (i - 2) + "s", "_");
String resultedReminder = reminderInInteger.toString();
result.append(underscoreToResult).append(resultedReminder).append("\n");
String multiply = String.format("%" + (i + 2) + "d", divisorMultipliedByQuontient(divisorMuplipliedByQuotient, divisor, reminderInInteger));
result.append(multiply).append("\n");

if (divident < divisor) {
printLastNumber(lastReminder, reminderInInteger, multiply);
}
Integer tab = resultedReminder.length() - calculateAmountOfDigits(divisorMultipliedByQuontient(divisorMuplipliedByQuotient, divisor, reminderInInteger));
String drawDashes = String.format("%" + (i + 2) + "s", createDivisor(divisorMultipliedByQuontient(divisorMuplipliedByQuotient, divisor, reminderInInteger), tab));
result.append(drawDashes).append("\n");
return result;
}

最佳答案

方法String.format("%Ns", "_");需要 N > 0。

由于您使用的是 String.format("%" + (i - 2) + "s", "_");您需要确保单元测试始终使用 i >= 3 运行以便引发任何异常。

关于java - 格式化字符串时出现 FormatFlagsConversionMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com