[英]go to beginning of for statement in java
I'm very new to Java and maybe my question is a bit irritating. 我对Java非常陌生,也许我的问题有点恼人。
I have two for
loops in my code and I want to go to the beginning of each one by a for
- else
statement. 我的代码中有两个for
循环,我想通过for
- else
语句转到每个循环的开头。
public static void main(String[] args)
{
int[][] x=new int[1000][1000];
int[] Z=new int[1000];
lable1:
for(int i=1; i<=1000; i++)
{
Z[i]=rand1.nextInt(1000);
System.out.println("Z["+i +"] = " + Z[i] );
if(Z[i]>0 && Z[i]<=Nk)
{
int Z1=Z[i]-1;
lable2:
for(int j = 1; j<=Z1;j++ )
{
x[i][j]= rand2.nextInt(1000);
sum+=x[i][j];
if( sum<1000)
{
x[i][(j+1)]=1000-sum;
System.out.println("x[" + i+"][" + j + "] = " + x[i][j]);
System.out.println("Nx[" + i+"][" + (j+1) + "] = " +x[i][(j+1)]);
}
else{
// ????
//Goto lable2;
}
}
}
else{
//goto label1;
// ????
}
}
}
Just use continue
keyword.. It will continue with the next iteration.. No need to label your loop .. As you are not continuing the outer loop from the inner one.. Or, if you want to continue
with outer loop, you can use continue
with a label ... 只需使用continue
关键字即可。它将继续下一次迭代。 不需要标记您的循环 ..因为您不需要从内部继续进行外部循环。或者,如果您想continue
进行外部循环,则可以使用带有标签的 continue
...
And you should use your for loop from j = 0 to j < z1
.. 并且您应该使用从j = 0 to j < z1
..的for循环。
for(int j = 0; j < Z1;j++ ) {
if( sum<1000) {
x[i][(j+1)]=1000-sum;
System.out.println("x[" + i+"][" + j + "] = " + x[i][j]);
System.out.println("Nx[" + i+"][" + (j+1) + "] = " +x[i][(j+1)]);
}
else{ // Not needed if your else does not contain anything else..
continue;
}
}
In fact you don't need an else
block at all.. If you are not doing any further processing in it.. 实际上,您根本不需要else
块 。.如果您没有在其中进行任何进一步的处理。
Just remove it.. It will automatically go to your loop.. 只需将其删除即可。它将自动进入您的循环。
Suggestion : - You should use coding convention.. variable names start with lowercase letter
or underscore
.. (Z1 -> z1) 建议 :-您应使用编码约定。.变量名以lowercase letter
或underscore
.. underscore
(Z1-> z1)
Here you are: 这个给你:
public static void main(String[] args) {
int[][] x = new int[1000][1000];
int[] Z = new int[1000];
boolean resetOuterCycle = true;
for (int i = 0; i < 1000; i++) {
Z[i] = rand1.nextInt(1000);
System.out.println("Z[" + i + "] = " + Z[i]);
if (Z[i] > 0 && Z[i] <= Nk) {
int Z1 = Z[i] - 1;
boolean resetInnerCycle = true;
for (int j = 0; j < Z1; j++) {
x[i][j] = rand2.nextInt(1000);
sum += x[i][j];
if (sum < 1000) {
x[i][(j + 1)] = 1000 - sum;
System.out.println("x[" + i + "][" + j + "] = " + x[i][j]);
System.out.println("Nx[" + i + "][" + (j + 1) + "] = " + x[i][(j + 1)]);
} else if (resetInnerCycle) {
j = 0;
resetInnerCycle = false;
}
}
} else if (resetOuterCycle) {
i = 0;
resetOuterCycle = false;
}
}
}
- In your above code you can use 2 approach to do it... -在上面的代码中,您可以使用2种方法来实现...
1st Approach : No else
part 第一种方法:没有else
部分
for (int i = 0; i < 1000; i++) {
if (Z[i] > 0 && Z[i] <= Nk){
for (int j = 0; j < Z1; j++) {
if(sum < 1000){
}
}
}
}
2nd Approach : With else
part and continue
第二种方法:与else
部分continue
for (int i = 0; i < 1000; i++) {
if (Z[i] > 0 && Z[i] <= Nk){
for (int j = 0; j < Z1; j++) {
if(sum < 1000){
}else{
continue;
}
}
}else{
continue;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.