While 循环提前终止

问题描述 投票:0回答:1

我有一个任务,生产煎饼,用户也可以同时吃它。条件是30秒内只能制作12个煎饼。我遇到的问题是,当煎饼被生产出来时,用户正在按预期吃它,但是当它生产出最后一个煎饼(即第 12 个)时,它会在用户没有吃掉它的情况下终止,从而浪费了 1 个煎饼。我的最终输出是:

Total number of cake produced is 12
Total number of cake eaten is 11
Total number of pancake remained is 1
public class PanCakeTask {

    int consumedPancake;
    int availablePancake;
    int totalPancakeProduced;
    List<String> usersList = new ArrayList<>();

     void producePancake(HashMap<String,Integer>users){

          consumedPancake =0;
          availablePancake = 0;
          totalPancakeProduced = 0;

        if ( totalPancakeProduced <12) {

            for (String user: users.keySet()){
                consumedPancake +=users.get(user);
            }

            //only continue with the production of pancakes if the number of pancakes
            //produced has not exceeded the quota that was given (i.e 12)

            System.out.println("Pancake produced");
            //increment the number of pancakes available
            availablePancake +=1;
            totalPancakeProduced = consumedPancake + availablePancake;

            eatPancake(users);

        }
    }
     void eatPancake(HashMap<String,Integer>users){
         if (usersList.size()>=1){
             System.out.println("The list is "+usersList);
             //first pick the user at random
             Collections.shuffle(usersList)

             int numberEaten = users.get(usersList.get(0));
             
             //get the user that is on index zero after the list has been shuffled
             //then check how many pancake that the picked user has eaten
             //because each user is expected to eat a maximum of 5 pancake

             if (numberEaten >=5){
                 //This means that this user has exhausted his/her quota so he/she cannot
                 //be allowed to eat again
                 //In this case, the eatPancake method is called again to randomly pick another user
                 //I am thinking of removing the user from the list here so that he/she cannot be picked again
                 usersList.remove(0);
                 eatPancake(users);
             }else {
                 //Meaning that the user still has some quota left for him/her to eat
                 //In this other condition, it will check if there is an available pancake produced

                 if (availablePancake >=1){
                     //user will now eat the pancake
                     //then increment the number of pancake eaten by that user
                     // and decrement the number of pancakes available

                     int newCount = numberEaten + 1;
                     availablePancake -=1;

                     //finally update the user that just eaten the pancake
                     users.put(usersList.get(0),newCount);
                 }
             }
         }
    }
    public void performTask(){

        //Hashmap is used to store the users because of key-value pairs
        HashMap <String,Integer> users = new HashMap<>();
        users.put("user1",0);
        users.put("user2",0);
        users.put("user3",0);

        usersList.addAll(users.keySet());

        System.out.println(users);
        System.out.println(usersList);


        //This task can only be executed in 20 seconds

        Instant stop = Instant.now().plusSeconds(20);
        System.out.println("The starting time is "+Instant.now());

        while (stop.isAfter(Instant.now())){
            //only continue the process if it has not exceeded 20 seconds

            if (totalPancakeProduced <12){
                producePancake(users);
            }else {
                break;
            }
        }
        int available = totalPancakeProduced - consumedPancake;
        int numbersLeftToMeetTheDemand = totalPancakeProduced - 15;
        System.out.println("The ending time is "+Instant.now());
        System.out.println("Total number of cake produced is "+totalPancakeProduced);
        System.out.println("Total number of cake eaten is "+consumedPancake);
        System.out.println("Total number of pancake remained is "+available);
                
}
java while-loop termination
1个回答
0
投票

您仅在

consumedPancake
方法中更新
producePancake

当循环到达最后一次迭代时,这将是一个问题。因为,

producePancake
会根据
consumedPancake
图来计算
users
。之后,调用
eatPancake
来更新地图。这将导致消耗的煎饼数量错误,因为在上次迭代中
consumedPancake
之后,
eatPancake
不会再次更新。

要解决此问题,不要在

consumedPancake
方法中修改
producePancake
,而是在吃完煎饼后在
eatPancake
方法中增加它。

像这样:

    void eatPancake(HashMap<String,Integer>users){
         if (usersList.size()>=1){
             System.out.println("The list is "+usersList);
             //first pick the user at random
             Collections.shuffle(usersList)

             int numberEaten = users.get(usersList.get(0));
             
             //get the user that is on index zero after the list has been shuffled
             //then check how many pancake that the picked user has eaten
             //because each user is expected to eat a maximum of 5 pancake

             if (numberEaten >=5){
                 //This means that this user has exhausted his/her quota so he/she cannot
                 //be allowed to eat again
                 //In this case, the eatPancake method is called again to randomly pick another user
                 //I am thinking of removing the user from the list here so that he/she cannot be picked again
                 usersList.remove(0);
                 eatPancake(users);
             }else {
                 //Meaning that the user still has some quota left for him/her to eat
                 //In this other condition, it will check if there is an available pancake produced

                 if (availablePancake >=1){
                     //user will now eat the pancake
                     //then increment the number of pancake eaten by that user
                     // and decrement the number of pancakes available

                     int newCount = numberEaten + 1;
                     availablePancake -=1;
                     //finally update the user that just eaten the pancake
                     users.put(usersList.get(0),newCount);
                     //here
                     consumedPancake++;
                 }
             }
         }
    }

amd 从 ProducePancake 中删除:

void producePancake(HashMap<String,Integer>users){

          //consumedPancake =0;
          availablePancake = 0;
          totalPancakeProduced = 0;

        if ( totalPancakeProduced <12) {
            /*
            for (String user: users.keySet()){
                consumedPancake +=users.get(user);
            }
            */

            //only continue with the production of pancakes if the number of pancakes
            //produced has not exceeded the quota that was given (i.e 12)

            System.out.println("Pancake produced");
            //increment the number of pancakes available
            availablePancake +=1;
            totalPancakeProduced = consumedPancake + availablePancake;

            eatPancake(users);

        }
    }

此外,您的代码中有一些冗余部分,但这似乎不是问题的原因,因此忽略它们。

© www.soinside.com 2019 - 2024. All rights reserved.