我有一个任务,生产煎饼,用户也可以同时吃它。条件是30秒内只能制作12个煎饼。我遇到的问题是,当煎饼被生产出来时,用户正在按预期吃它,但是当它生产出最后一个煎饼(即第 12 个)时,它会终止,而用户不会吃掉它,从而浪费了 1 个煎饼。所以我的最终输出是: 制作的蛋糕总数为 12 个 吃掉的蛋糕总数是 11 剩下的煎饼总数是 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);
System.out.println("The users data are "+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;
System.out.println("Available pancake here is "+availablePancake);
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);
System.out.println("The list after shuffle is "+usersList);
System.out.println("The selected user is "+usersList.get(0));
int numberEaten = users.get(usersList.get(0));
System.out.println("Number this user has eaten is "+numberEaten);
//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);
System.out.println("The updated user is "+usersList.get(0));
System.out.println("Number of pancake eaten by this user is "+usersList.get(0)+" "+users.get(usersList.get(0)));
}
}
}
}
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);
if (totalPancakeProduced >=15){
//meaning that the produced pancakes was enough for the 3 users
System.out.println("The shopkeeper was able to meet the needs of the users");
}else {
//Wasn't able to complete the user's needs
System.out.println("The shop keeper was not able to meet the needs of the users");
}
if (available > 0){
//Wasted pancake(s)
//This is the number(s) of pancake that was produced but the user's didn't eat it
System.out.println(available+" pancake(s) were wasted");
}
if (numbersLeftToMeetTheDemand <0){
//meaning that there were some lapses in the user's demand
System.out.println("The number of pancakes needed to complete the user's demand is "+numbersLeftToMeetTheDemand);
}
}
}
Java 中 while 循环的提前终止可以使用“break”语句来实现。当遇到“break”时,无论循环条件如何,都会立即退出循环。当循环体内满足特定条件时,这特别有用,可以实现更高效和受控的程序流程。