我寫的是沒買完5次,壹直選店鋪購買
package?test;import?java.util.ArrayList;
import?java.util.HashMap;
import?java.util.List;
import?java.util.Map;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
int?count?=?0; //?用戶購物
int?countLimit?=?5; //?用戶最大次數
int?shopCount?=?3; //?用戶店鋪最大購衣
List<Integer>?hasShop?=?new?ArrayList<Integer>();//?逛店鋪個數
Map<Integer,?Integer>?shopMap?=?new?HashMap<>();//?店鋪{店鋪id=用戶購衣數}
shopMap.put(1,?0);
shopMap.put(2,?0);
shopMap.put(3,?0);
Scanner?sc?=?new?Scanner(System.in);
while?(count?<?countLimit)?{
//?判斷用戶店鋪購衣數?==?3
String?shopIds?=?"";
for?(Map.Entry<Integer,?Integer>?entry?:?shopMap.entrySet())?{
int?shopId?=?entry.getKey();
int?userCount?=?entry.getValue();
if?(userCount?!=?shopCount)?{
if?("".equals(shopIds))?{
shopIds?+=?shopId;
}else?{
shopIds?+=?",?"+shopId;
}
}
}
System.out.print("請選擇店鋪號("?+?shopIds+"):?");
int?shopId?=?sc.nextInt();
if?(shopMap.containsKey(shopId))?{
int?count1?=?shopMap.get(shopId);
if?(count1?!=?shopCount)?{
System.out.println("您現在進入的是"+shopId+"店:");
System.out.println("服務員問:先生您買衣服嗎?y/n:");
hasShop.add(new?Integer(shopId));
String?first?=?sc.next();
if?("y".equals(first))?{
count?++;
count1?++;
shopMap.put(shopId,?count1);
System.out.println("購買壹件衣服成功");
while?(count?!=?countLimit?&&?count1?!=?shopCount)?{
System.out.println("服務員問:先生您還繼續購買嗎?y/n:");
String?second?=?sc.next();
if?("y".equals(second))?{
count?++;
count1?++;?
shopMap.put(shopId,?count1);
System.out.println("購買壹件衣服成功");
}else{
break;
}
}
}
}
}
}
sc.close();
System.out.println("購物完成,小王壹***逛了"+hasShop.size()+"家店"+hasShop.toString()+",買了"+count+"件衣服");
}
}
結果值: