基本函数调用
请位各位兄弟,这个表达式是什么意思啊.
int size=6;
if(args.length!=0){
size=Integer.parseInt(args[0]);}
问题点数:20、回复次数:11Top
1 楼wingtrace(虽然生活很艰苦,但是我们也不能做禽兽)回复于 2005-07-31 10:32:29 得分 0
如果命令行参数至少有一个,那么就将第一个命令行参数(String)转换为int型并赋值给size。Top
2 楼HitXU(一天不学习,赶不上刘少奇)回复于 2005-07-31 10:35:44 得分 0
args[0]是第一个参数,这个传进来是个String,
if(args.length!=0)是判断有没有参数传进来,args是String[]类型的,是字符串数组,存放传进来的参数,每个参数是一个String.
Integer类有static方法public static int parseInt(String s),该方法可以把一个字符串转换成整型数。Top
3 楼kingfish(工作很忙,很少来csdn...)回复于 2005-07-31 10:41:26 得分 0
java A 100
====
args[0]Top
4 楼sleot(悠悠晚风)回复于 2005-07-31 11:04:44 得分 0
谢谢大家哈.有点理解了.
还想问问(String)转换为int型是怎么转换的.
Top
5 楼interhanchi(on the Java Road)回复于 2005-07-31 11:29:07 得分 0
parseInt
public static int parseInt(String s)
throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.
Top
6 楼wingtrace(虽然生活很艰苦,但是我们也不能做禽兽)回复于 2005-07-31 11:29:49 得分 0
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to
* indicate a negative value. The resulting integer value is returned.
* <p>
* An exception of type <code>NumberFormatException</code> is
* thrown if any of the following situations occurs:
* <ul>
* <li>The first argument is <code>null</code> or is a string of
* length zero.
* <li>The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.
* <li>Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
* <code>'-'</code> (<code>'\u002D'</code>) provided that the
* string is longer than length 1.
* <li>The value represented by the string is not a value of type
* <code>int</code>.
* </ul><p>
* Examples:
* <blockquote><pre>
* parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("2147483647", 10) returns 2147483647
* parseInt("-2147483648", 10) returns -2147483648
* parseInt("2147483648", 10) throws a NumberFormatException
* parseInt("99", 8) throws a NumberFormatException
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787
* </pre></blockquote>
*
* @param s the <code>String</code> containing the integer
* representation to be parsed
* @param radix the radix to be used while parsing <code>s</code>.
* @return the integer represented by the string argument in the
* specified radix.
* @exception NumberFormatException if the <code>String</code>
* does not contain a parsable <code>int</code>.
*/
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;
if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / radix;
if (i < max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}
/**
* Parses the string argument as a signed decimal integer. The
* characters in the string must all be decimal digits, except that
* the first character may be an ASCII minus sign <code>'-'</code>
* (<code>'\u002D'</code>) to indicate a negative value. The resulting
* integer value is returned, exactly as if the argument and the radix
* 10 were given as arguments to the
* {@link #parseInt(java.lang.String, int)} method.
*
* @param s a <code>String</code> containing the <code>int</code>
* representation to be parsed
* @return the integer value represented by the argument in decimal.
* @exception NumberFormatException if the string does not contain a
* parsable integer.
*/
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
Top
7 楼believefym(feng)回复于 2005-07-31 11:31:09 得分 0
还想问问(String)转换为int型是怎么转换的.
------------------------
调用Integer类的parseInt函数Top
8 楼sleot(悠悠晚风)回复于 2005-07-31 11:37:21 得分 0
这么长啊,晕都快晕了.
size=Integer.parseInt(args[0])中的 args[0]是String 的一个reference还是具体的值.
如果是String 中具体的一个值的话.
如"bad"应该转成多少.
请大家指点一下,麻烦大家了.Top
9 楼believefym(feng)回复于 2005-07-31 11:57:01 得分 0
"bad"当然是不能转成int型的,
Integer.parseInt(String str),参数的String代表了一个整型,只不过是String罢了Top
10 楼sleot(悠悠晚风)回复于 2005-07-31 12:01:30 得分 0
可以说得更详细一些吗?
最好结合例子.
谢谢!Top
11 楼dudeng2005(dudeng)回复于 2005-07-31 16:36:20 得分 0
size = Integer.parseInt(args[0])中的args[0]是String的一个reference还是具体的值。
如果是String中的具体的一个值的话。
如"bad"应该转成多少。
告诉你输入的只要不是纯数字组成的字符串 是不可能转成整行的。
开始的那个int size = 6;这句话跟下面的没有影响。Top




