for循环在java中表现不同[重复]

时间:2022-08-27 20:47:03

This question already has an answer here:

这个问题在这里已有答案:

class A {
public static void main (String[] args) {
   // code ...
   System.out.println(B.fun1());
   // code ...
}

class B {
   // code ...
   public int fun1(){
   return C.fun2();
 }

class C {
    // code ...
    public int fun2() {
        int u = 0;
        for(int k= 0; arr[k] != null ; k++,System.out.println("k="+k)) {
            int a = arr[k].getVal();
            String s = Integer.toString(a);
            if (s.equals(("1"))) {
                u = u + 10;
            } else {
                u = u + a;
            }
        }
        return u;
    }
 }

I have a code of same structure as shown above and when main executes the output is:

我有一个如上所示相同结构的代码,当main执行输出时:

k=1
k=2
k=3

the return value u from fun1 as printed in main is:

main中打印的fun1的返回值u是:

30  //depends on getVal()

but after this output it also shows this

但在此输出之后它也显示了这一点

k=1
k=2
k=1
k=2
k=1
k=2

and I receive an ArrayIndexOutOfBoundsException. How is this possible? My question is why I am getting this k=1 k=2 k=1 ... thing CODE:https://drive.google.com/open?id=1aZYv7qqYd1___fXA92lbtLO5WccI9wz6 How to reproduce: Things are random so try to play couple of times.

我收到一个ArrayIndexOutOfBoundsException。这怎么可能?我的问题是为什么我得到这个k = 1 k = 2 k = 1 ...事情CODE:https://drive.google.com/open?id = 1aZYv7qqYd1 ___ fXA92lbtLO5WccI9wz6如何重现:事情是随机的所以试着玩情侣时代

2 个解决方案

#1


1  

for(int k= 0; arr[k] != null ; k++,System.out.println("k="+k)) {

You have no constraint on how high k can go, rather you are only evaluating until it is found to be null. Out of index values will throw an IndexOutOfBoundsException rather than evaluate to a null value, so if you have no null values in your array, you will always run into an exception barring some other manner of breaking the loop. To protect from this you can check to make sure k is within the array length prior to doing your null check.

你对k的高度没有约束,而只是在你被发现为null之前进行评估。超出索引值将抛出IndexOutOfBoundsException而不是求值为null值,因此如果数组中没有空值,则总是会遇到一个异常,除非有其他方式打破循环。为了防止这种情况,您可以检查以确保在进行空检查之前k在数组长度内。

for(int k= 0; k < arr.length && arr[k] != null ; k++,System.out.println("k="+k)) {

#2


0  

The original code is here

原始代码在这里

Looking to your entire code I guessed how to remove the extra numbers in console output, also I made a couple change to make your code a bit more efficient. Let me know if this is the final solution to your problem.

看看你的整个代码,我猜测如何删除控制台输出中的额外数字,我做了一些更改,使你的代码更有效。如果这是您问题的最终解决方案,请告诉我。

First of all I removed the System.out.Println(k) inside the loop which eliminated the final extra numbers in the console output:

首先,我删除了循环中的System.out.Println(k),它消除了控制台输出中的最终额外数字:

public int evaluateHand() {
    int u = 0;
    try {
        for(int k= 0; hand[k] != null; k++) {
            int a = hand[k].getRank();
            String ok = Integer.toString(a);
            if (ok.equals(("13"))) {
                u = u + 10;
            }else if (ok.equals(("12"))) {
                u = u + 10;
            } else if (ok.equals(("11"))) {
                u = u + 10;
            } else if (ok.equals(("1"))) {
                u = u + 11;
            } else {
                u = u + a;
            }
        }

#1


1  

for(int k= 0; arr[k] != null ; k++,System.out.println("k="+k)) {

You have no constraint on how high k can go, rather you are only evaluating until it is found to be null. Out of index values will throw an IndexOutOfBoundsException rather than evaluate to a null value, so if you have no null values in your array, you will always run into an exception barring some other manner of breaking the loop. To protect from this you can check to make sure k is within the array length prior to doing your null check.

你对k的高度没有约束,而只是在你被发现为null之前进行评估。超出索引值将抛出IndexOutOfBoundsException而不是求值为null值,因此如果数组中没有空值,则总是会遇到一个异常,除非有其他方式打破循环。为了防止这种情况,您可以检查以确保在进行空检查之前k在数组长度内。

for(int k= 0; k < arr.length && arr[k] != null ; k++,System.out.println("k="+k)) {

#2


0  

The original code is here

原始代码在这里

Looking to your entire code I guessed how to remove the extra numbers in console output, also I made a couple change to make your code a bit more efficient. Let me know if this is the final solution to your problem.

看看你的整个代码,我猜测如何删除控制台输出中的额外数字,我做了一些更改,使你的代码更有效。如果这是您问题的最终解决方案,请告诉我。

First of all I removed the System.out.Println(k) inside the loop which eliminated the final extra numbers in the console output:

首先,我删除了循环中的System.out.Println(k),它消除了控制台输出中的最终额外数字:

public int evaluateHand() {
    int u = 0;
    try {
        for(int k= 0; hand[k] != null; k++) {
            int a = hand[k].getRank();
            String ok = Integer.toString(a);
            if (ok.equals(("13"))) {
                u = u + 10;
            }else if (ok.equals(("12"))) {
                u = u + 10;
            } else if (ok.equals(("11"))) {
                u = u + 10;
            } else if (ok.equals(("1"))) {
                u = u + 11;
            } else {
                u = u + a;
            }
        }