循环,迭代意外的结果

时间:2014-09-13 16:48:30

标签: java loops for-loop iteration

我必须做一个循环,但我尝试了很多时间来获得预期的结果,但我不知道我做错了什么。

public class JavaApplication34 {
   int totalResults = 248; //
   int itemsperPage = 10;
   int i = 0;
   int j = 0;
   int count = 0;

   public void makeLoop() {
      for (i = 0; i < 10; i++) {
         System.out.println("nextPage " + i);
         this.count = i + 1;
         for (j = 0; j < 10; j++) {
            System.out.println("Filenummer " + (j + 1));
            this.count = j + 1;
            System.out.println("count " + count);
         }
      }
   }

   public static void main(String[] args) {
      JavaApplication34 myTest = new JavaApplication34();
      myTest.makeLoop();
   }        
}

我应该按照结果

进入控制台
  • 第1页,filenummer 1,filenummer 2,filenummer 3 ...,filenummer 10
  • 第2页,filenummer 11,filenummer 12,filenummer 13 ...,filenummer20
  • 第3页,依此类推。

4 个答案:

答案 0 :(得分:4)

根据预期的输出,您也可以更改代码

for (i = 1; i <= (totalResults/itemsperPage) + 1; i++) {
    System.out.println("nextPage " + i);
    for (; j < i* itemsperPage; j++) {
        if(j > totalResults) {
           break;
        }
        System.out.println("Filenummer " + (j + 1));
    }
}

答案 1 :(得分:1)

我现在有以下代码,aeshang写道:

public class JavaApplication34 {

int totalResults = 34;
int itemsperPage = 10;
int i = 0;
int j = 0;

public void makeLoop() {

    for (i = 1; i <= (totalResults / itemsperPage); i++) {
        System.out.println("nextPage " + i);
        for (; j < i * itemsperPage; j++) {
            if (j > totalResults) {
                break;
            }
            System.out.println("Filenummer " + (j + 1));
        }
    }

}

它给了我以下输出

  • nexpage 1,filenumber 1,filnumber 2,....,filnumber 10
  • nexpage 2,filenumber 11,filenumber 12 ....,filnumber 20
  • ...........
  • nexpage 3,filenumber 21,filenumber 22,... filnumber 30(但它 应该是,34)

答案 2 :(得分:0)

        int totalResults = 34;
        int itemsperPage = 10;
        int i = 0;
        int j = 0;
        int loopSize = (totalResults / itemsperPage);

        public void makeLoop() {
            loopSize = (totalResults % itemsperPage > 0) ? ++loopSize : loopSize;

            for (i = 1; i <= loopSize; i++) {
                System.out.print("nextPage " + i + "...");
                for (; j < i * itemsperPage; j++) {
                    if (j > totalResults) {
                        break;
                    }
                    System.out.print(" Filenummer " + (j + 1));
                }
                System.out.println();

            }

        }

输出= nextPage 1 ... Filenummer 1 Filenummer 2 Filenummer 3 Filenummer 4 Filenummer 5 Filenummer 6 Filenummer 7 Filenummer 8 Filenummer 9 Filenummer 10 nextPage 2 ... Filenummer 11 Filenummer 12 Filenummer 13 Filenummer 14 Filenummer 15 Filenummer 16 Filenummer 17 Filenummer 18 Filenummer 19 Filenummer 20 nextPage 3 ... Filenummer 21 Filenummer 22 Filenummer 23 Filenummer 24 Filenummer 25 Filenummer 26 Filenummer 27 Filenummer 28 Filenummer 29 Filenummer 30 nextPage 4 ... Filenummer 31 Filenummer 32 Filenummer 33 Filenummer 34 Filenummer 35

答案 3 :(得分:0)

int totalResults = 34;
int itemsperPage = 10;
int i = 0;
int j = 1;
int loopSize = (totalResults / itemsperPage);

public void makeLoop() {
    loopSize = (totalResults % itemsperPage > 0) ? ++loopSize : loopSize;

    for (i = 1; i <= loopSize; i++) {
        System.out.print("nextPage " + i + "...");
        for (; j < i * itemsperPage; j++) {
            if (j > totalResults) {
                break;
            }
            System.out.print(" Filenummer " + (j));
        }
        System.out.println();

    }

}

输出= nextPage 1 ... Filenummer 1 Filenummer 2 Filenummer 3 Filenummer 4 Filenummer 5 Filenummer 6 Filenummer 7 Filenummer 8 Filenummer 9 nextPage 2 ... Filenummer 10 Filenummer 11 Filenummer 12 Filenummer 13 Filenummer 14 Filenummer 15 Filenummer 16 Filenummer 17 Filenummer 18 Filenummer 19 nextPage 3 ... Filenummer 20 Filenummer 21 Filenummer 22 Filenummer 23 Filenummer 24 Filenummer 25 Filenummer 26 Filenummer 27 Filenummer 28 Filenummer 29 nextPage 4 ... Filenummer 30 Filenummer 31 Filenummer 32 Filenummer 33 Filenummer 34