50,532
社区成员
发帖
与我相关
我的任务
分享
public class Poker {
String point;//点数
Character decor;//花色
public Poker(String point, Character decor) {
this.point = point;
this.decor = decor;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public Character getDecor() {
return decor;
}
public void setDecor(Character decor) {
this.decor = decor;
}
@Override
public String toString() {
return "Poker{" +
"point='" + point + '\'' +
", decor='" + decor + '\'' +
'}';
}
}
private static final String[] pokerArray = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
private static final char[] decorArray = {'♥','♦','♠','♣'};
/**
* 1.初始化有序数组,
* 2.提示用户输入对象属性,校验数据的合法性
* 3.获取该对象应当插入的下标 :
* i.如果当前对象的点数小于列表中某一对象(list.get(i))的点数,说明当前对象应当插入在某一对象(list.get(i))之前
* ii.如果当前对象的点数的等于列表中某一对象(list.get(i))的点数,比较花色的大小,如果花色小于某一对象(list.get(i))的点数,说明当前对象应当插入在某一对象之前
* iii.如果遍历结束都没有找到下标,说明该对象大于当前列表中所有对象,直接插入在最后面即可
*/
public static void main(String[] args) {
ArrayList<Poker> list = initArrayList();;
System.out.println("已经为你随机生成10张扑克牌,当前扑克牌的顺序为:");
list.forEach(System.out::println);
Scanner sc = new Scanner(System.in);
System.out.println("----------------------");
while (true){
String point = "";
do{
try {
System.out.println("请输入点数:");
point = sc.nextLine();
String finalPoint = point;
if (Arrays.stream(pokerArray).noneMatch(p -> p.equals(finalPoint))){
System.out.println("输入的点数不合法请重新输入:");
//置空,继续进入循环
point = "";
}
}catch (Exception e){
System.out.println("输入的点数未识别到,请重新输入:");
}
}while ("".equals(point));
char decor = ' ';
do{
try {
System.out.println("请输入花色:");
decor = sc.nextLine().charAt(0);
boolean check = false;
for (char c : decorArray) {
if (c == decor) {
check = true;
break;
}
}
if (!check){
System.out.println("输入的花色不合法请重新输入:");
decor = ' ';
}
}catch (Exception e){
System.out.println("输入的花色未识别到,请重新输入:");
}
}while (decor ==' ');
Poker p = new Poker(point,decor);
int index = getIndex(list,p);
if (index != -1){
list.add(index,p);
System.out.println("插入成功,当前扑克牌的顺序为:");
list.forEach(System.out::println);
}else {
System.out.println("当前已经在该张牌:");
}
}
}
private static ArrayList<Poker> initArrayList() {
ArrayList<Poker> list = new ArrayList<>();
do {
Poker p = new Poker(pokerArray[(int) (Math.random()*13)],decorArray[(int) (Math.random()*4)]);
int index = getIndex(list,p);
if (index != -1){
//牌重复,不添加
list.add(index,p);
}
}while (list.size()<10);
return list;
}
private static int getIndex(ArrayList<Poker> list, Poker p) {
if (list.size() == 0){
return 0;
}
for (int i = 0; i < list.size(); i++) {
//点数小于已排序列表中的第i个对象,说明要插入的位置就在这个元素之前
if (getPointNum(p) < getPointNum(list.get(i))){
return i;
}
if (getPointNum(p) == getPointNum(list.get(i))){
//点数小于相等 花色小于已排序列表中的第i个对象,也说明要插入的位置就在这个元素之前
if (getDecorNum(p) < getDecorNum(list.get(i))){
return i;
}
//如果花色相等,说明重复
if (getDecorNum(p) == getDecorNum(list.get(i))){
return -1;
}
}
}
//遍历完说明最新的排大于任何一个元素
return list.size();
}
private static int getPointNum(Poker p ){
int point = 0;
for(int i = 0;i<pokerArray.length;i++){
if (pokerArray[i].equals(p.getPoint())){
point = i+1;
}
}
return point;
}
private static int getDecorNum(Poker p ){
int decor = 0;
for(int i = 0;i<decorArray.length;i++){
if (decorArray[i]==p.getDecor()){
decor = i+1;
}
}
return decor;
}