如何在循环中的某个点打印“i”的值?

时间:2013-04-21 12:27:42

标签: java

public static void main(String[] args) {
    int largestChain = 0;
    for(int i = 1; i < 1000000; ++i) {
        if(chain(i) > largestChain) {
            largestChain = chain(i);  // Here, how do I print the final value
                                      // of 'i' that respects the rule chain(i)?
        }
    }
    System.out.print(largestChain);
}

例如,如果i = 13chain(i) = 10largestChain最多13(来自1)是10.现在让我们假设我们有一个周期,从1到100,同一largestChain的{​​{1}}仍为10。当周期必须完成其任务(并转到100)时,如何打印i = 13

2 个答案:

答案 0 :(得分:2)

可能这就是你想要的:

public static void main(String[]args){

    int largestChain = 0;
    int largestIndex = 0;
    for(int i = 1; i < 1000000; ++i){
        if(chain(i) > largestChain){
            largestChain = chain(i); 
            largestIndex = i;
        }
    }
    System.out.print("Largest chain is :"+largestChain);
    System.out.print("Largest chain index is :"+largestIndex);
}

答案 1 :(得分:1)

    int largestChain = 0;
    int index =0;
    for(int i = 1; i < 1000000; ++i){
        if(chain(i) > largestChain){
            largestChain = chain(i); //here, how do I print the final value of 'i' that respects the rule chain(i) 
            index = i;
        }
    }
    System.out.print(largestChain + " index  : " + index);