This question already has answers here:
Post and Pre increment operators
                                
                                    (7个答案)
                                
                        
                5年前关闭。
            
        

我有以下代码:

    /* For loop to count 50 days */
    for (int n = 1; n <= 50; n++) {

        int solve = 0;
        solve = solve++;
        item_1.removeDailyDemand();

        if ( n % 5 == 0){


        }
        if (solve == 5){

            item_1.isReOrderPoint();
        }

        System.out.print(n + "\t");
        System.out.println (item_1.getQuantityInStock() + "\t");


玩了很久,对Java来说还很新。我只希望它在运行for循环时将“ solve”增加为“ n”。

整天都在尝试,没有结果!

谢谢

最佳答案

更换:

/* For loop to count 50 days */
for (int n = 1; n <= 50; n++) {
    int solve = 0;
    solve = solve++;


通过:

/* For loop to count 50 days */
int solve = 0;
for (int n = 1; n <= 50; n++) {
    solve++;


在每次迭代中,将solve变量重置为0。
而且solve = solve++是无用的,因为首先复制Solve变量,然后再递增。与solve++相同

10-05 18:45