I'm trying to convert multiple strings into integers and then print use each one of them outside of the for loop.
我正在尝试将多个字符串转换为整数,然后在for循环之外打印使用它们中的每一个。
This is my for loop:
这是我的for循环:
for (int i = 0; i < parts.length; i++) {
int p1 = Integer.parseInt(parts[i]);
}
3 个解决方案
#1
3
If you want to be able to access all of the integers you are parsing after the loop:
如果您希望能够访问循环后正在解析的所有整数:
int[] pValues = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
pValues[i] = Integer.parseInt(parts[i]);
}
// at this point you still have access to the pValues array
#2
1
In order to use anything outside a for
loop (or any other scope, for that matter) you must declare that variable in the scope where you wish to use it:
为了在for循环(或任何其他范围)之外使用任何东西,您必须在您希望使用它的范围内声明该变量:
int p1 = -1;
for (int i = 0; i < parts.length; i++) {
p1 = Integer.parseInt(parts[i]);
}
Note that in situations like the above, where a loop sets a single value, a very common thing is to break the loop right after setting the value. Obviously, the value should be set conditionally, otherwise the purpose of the loop would be defeated:
请注意,在上述情况下,循环设置单个值,一个非常常见的事情是在设置值后立即中断循环。显然,应该有条件地设置值,否则循环的目的将被打败:
int p1 = -1;
for (int i = 0; i < parts.length; i++) {
if (someCondition()) {
p1 = Integer.parseInt(parts[i]);
break;
}
}
#3
0
Any variables created inside of a loop are LOCAL TO THE LOOP. This means that once you exit the loop, the variable can no longer be accessed! This includes any variables created in the loop signature.
在循环内创建的任何变量都是LOCAL TO LOOP。这意味着一旦退出循环,就无法再访问该变量!这包括在循环签名中创建的任何变量。
Read more: http://www.java-made-easy.com/variable-scope.html#ixzz3TZ6obLXK
阅读更多:http://www.java-made-easy.com/variable-scope.html#ixzz3TZ6obLXK
int secondp1 = 0; // Define your variable outside your loop
for (int i = 0; i < parts.length; i++) {
secondp1 = Integer.parseInt(parts[i]);
}
//so you can use it here.
System.out.println(secondp1 );
You can see this to check out variable scope and lifetime.
您可以看到这个以检查变量范围和生命周期。
#1
3
If you want to be able to access all of the integers you are parsing after the loop:
如果您希望能够访问循环后正在解析的所有整数:
int[] pValues = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
pValues[i] = Integer.parseInt(parts[i]);
}
// at this point you still have access to the pValues array
#2
1
In order to use anything outside a for
loop (or any other scope, for that matter) you must declare that variable in the scope where you wish to use it:
为了在for循环(或任何其他范围)之外使用任何东西,您必须在您希望使用它的范围内声明该变量:
int p1 = -1;
for (int i = 0; i < parts.length; i++) {
p1 = Integer.parseInt(parts[i]);
}
Note that in situations like the above, where a loop sets a single value, a very common thing is to break the loop right after setting the value. Obviously, the value should be set conditionally, otherwise the purpose of the loop would be defeated:
请注意,在上述情况下,循环设置单个值,一个非常常见的事情是在设置值后立即中断循环。显然,应该有条件地设置值,否则循环的目的将被打败:
int p1 = -1;
for (int i = 0; i < parts.length; i++) {
if (someCondition()) {
p1 = Integer.parseInt(parts[i]);
break;
}
}
#3
0
Any variables created inside of a loop are LOCAL TO THE LOOP. This means that once you exit the loop, the variable can no longer be accessed! This includes any variables created in the loop signature.
在循环内创建的任何变量都是LOCAL TO LOOP。这意味着一旦退出循环,就无法再访问该变量!这包括在循环签名中创建的任何变量。
Read more: http://www.java-made-easy.com/variable-scope.html#ixzz3TZ6obLXK
阅读更多:http://www.java-made-easy.com/variable-scope.html#ixzz3TZ6obLXK
int secondp1 = 0; // Define your variable outside your loop
for (int i = 0; i < parts.length; i++) {
secondp1 = Integer.parseInt(parts[i]);
}
//so you can use it here.
System.out.println(secondp1 );
You can see this to check out variable scope and lifetime.
您可以看到这个以检查变量范围和生命周期。