package Maths;public class ParseInteger {public static void main(String[] args) {assert parseInt("123") == Integer.parseInt("123");assert parseInt("-123") == Integer.parseInt("-123");assert parseInt("0123") == Integer.parseInt("0123");assert parseInt("+123") == Integer.parseInt("+123");}/*** Parse a string to integer** @param s the string* @return the integer value represented by the argument in decimal.* @throws NumberFormatException if the {@code string} does not contain a parsable integer.*/public static int parseInt(String s) {if (s == null || s.length() == 0) {throw new NumberFormatException("null");}boolean isNegative = s.charAt(0) == '-';boolean isPositive = s.charAt(0) == '+';int number = 0;for (int i = isNegative ? 1 : isPositive ? 1 : 0, length = s.length(); i < length; ++i) {if (!Character.isDigit(s.charAt(i))) {throw new NumberFormatException("s=" + s);}number = number * 10 + s.charAt(i) - '0';}return isNegative ? -number : number;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。