循环时跳过一些值

时间:2016-02-11 10:40:19

标签: java android

我正在尝试制作一个以用户设置计数页面的代码,问题是ListView正在跳过一些行!! 我单独尝试将代码作为java代码,它运行良好! 这是代码:

public void CollectThePages(int Num1,int Num2){
    int startpage = Num1;
    int endpage = Num2;
    int Number = startpage;
    int Day = 1;
    int DailyPages = 9;



    while(Number >= startpage){
        HashMap<String,String> data = new HashMap<>();
        if(Number == startpage){
            System.out.println("Day Number " + Day);
            data.put("day", "Day " + Day);
            Day ++;
            System.out.print("From " + Number);
            data.put("from","From " + Number);
            Number += DailyPages;
            int NumberofPages = DailyPages + 1;
            System.out.print(" To "+Number);
            data.put("to", "To " + Number);
            System.out.println(" | The Pages are " + NumberofPages);
            data.put("NOP",NumberofPages + " Pages");
        }

        if(Number >= startpage){
            Number +=1;
            System.out.println("Day Number " + Day);
            data.put("day", "Day " + Day);
            Day ++;
            System.out.print("From "+Number);
            data.put("from", "From " + Number);
            Number += DailyPages;
            int NumberofPages = DailyPages + 1;
            System.out.print(" To "+Number);
            data.put("to", "To " + Number);
            System.out.println(" | The Pages are " + NumberofPages);
            data.put("NOP", NumberofPages + " Pages");
        }

        if(Number > endpage | (Number + DailyPages) > endpage){
            Number +=1;
            System.out.println("Day Number " + Day);
            data.put("day","Day " + Day);
            Day ++;
            System.out.print("From " + Number);
            data.put("from","From " + Number);
            int value = endpage - Number;
            Number += value;
            System.out.print(" To " + Number);
            data.put("to", "To " + Number);
            System.out.println(" | The Pages are " + (value + 1));
            data.put("NOP",value + " Pages");

        }

        if(Number == endpage | (Number + 1) == endpage){
            Number = 0;
        }
        valueList.add(data);
    }

    ValueList = (ListView) findViewById(R.id.valueList);
    //Out of The Loop
    valueListAdapter = new ValueListAdapter(getApplicationContext(),valueList);
    ValueList.setAdapter(valueListAdapter);
}

这是结果(第一页是1,最后是55) - 跳过第1天和第5天 Skipped the 1st day and 5th day !!

2 个答案:

答案 0 :(得分:1)

@Shree Krishna正确地指出了你的错误,应该相应地修改你的代码。您也可以使用以下内容: -

 public void CollectThePages(int Num1,int Num2){
        int startpage = Num1;
        int endpage = Num2;
        int Number = startpage;
        int Day = 1;
        int DailyPages = 9;

        while(Number <= endpage){
            HashMap<String,String> data = new HashMap<>();
            System.out.print("Day Number " + Day);
            data.put("day", "Day " + Day);
            ++Day;
            System.out.print("From " + Number);
            data.put("from", "From " + Number);
            int prevNumber = Number;
            Number += DailyPages;
            Number = Number > endpage ? endpage : Number;
            int NumberofPages = Number - prevNumber + 1;
            System.out.print(" To " + Number);
            data.put("to", "To " + Number);
            System.out.print(" | The Pages are " + NumberofPages);
            data.put("NOP", NumberofPages + " Pages");
            ++Number;
            valueList.add(data);
        }

        ValueList = (ListView) findViewById(R.id.valueList);
        //Out of The Loop
        valueListAdapter = new ValueListAdapter(getApplicationContext(),valueList);
        ValueList.setAdapter(valueListAdapter);
    }

答案 1 :(得分:0)

作为代码中的逻辑,如果

,请简单举例
let filterArray = personArray.filter
            { person in
           let age = personObj.age;
           let name = personObj.name;
           let income = personObj.income
           BOOL result = (age > 20 && name == "Stack" && income >40000);
          return result;
        }

您的病情将是

Num1 = 1

然后发生的事情是if(Number == startpage) ---> if(1 == 1) //Condition satisfied 增加1并添加到变量Day

同一循环中的另一个条件

data

然后再次if(Number >= startpage) ---> if(10 >= 1) //Again Condition satisfied 增加1

<强>结论

您的每个数据都会以+1添加。

要防止此

检查你的每一个if条件并使它们都是唯一的,不要在相同条件下重复它,或者如果它在将来没有违反你的逻辑的话,在每个条件下也改变Day的值。