我有一个函数,试图将用户输入的2-5个数字的String数组解析为一个Int数组,并计算这些数字的总和以确定游戏的获胜者。此时,它引发了NumberFormatException。有人可以解释我在做什么错吗?
关注最多How to use Java get function on Thymeleaf HTML?
这是Java类和函数...
public class Score {
String player1name;
String player2name;
String[] player1score;
String[] player2score;
}
//Function to determine winner
public String getWinningScore() {
//Lambda Expressions to convert String array to Int array and calculate sums
int sumOfscore1 = Arrays.stream(player1score).mapToInt(Integer::parseInt).sum();
int sumOfscore2 = Arrays.stream(player2score).mapToInt(Integer::parseInt).sum();
if(sumOfscore1 > sumOfscore2) {
winner = player1name;
} else if(sumOfscore2 > sumOfscore1) {
winner = player2name;
}
return winner;
}
...以及我在百里香html中的用户输入。
<input type="number" id = "a1s" name="a1" class="newMatch" value="0" min="0" max="11" th:field="*{player1score}">
<input type="number" id = "b1s" name="b1" class="newMatch" value="0" min="0" max="11" th:field="*{player2score}">