Home > Back-end >  Pray god give CARDS insertion sort of pseudo code and JAVA code after improvement
Pray god give CARDS insertion sort of pseudo code and JAVA code after improvement

Time:09-16

Pray god give CARDS insertion sort of pseudo code and JAVA code after improvement

CodePudding user response:

 
Private static final String [] pokerArray={" A ", "2", "3", "4", "5", "6", "7", "eight" and "9", "10", "J", "Q", "K"};

Private static final char [] decorArray={'? ', '? ', '? ', '? '};

/* *
* 1. Initialization orderly array,
* 2. Prompt the user for object attribute, the legitimacy of the calibration data
* 3. For the object should be inserted into the subscript:
* i. if the current object points less than a certain object in the list (list. Get (I)) of the points, suggests that the current object should be inserted in a (list. Get (I)) before
Ii. If the current object * points equal to a certain object in the list (list. Get (I)) points, compare the size of design and color, if the design and color is less than a certain object (list. Get (I)) points, illustrate the current object shall be inserted before an object
* iii. If did not find the subscript traversal is over, that the object is greater than the current list of all objects that can be directly inserted in the back
*/

Public static void main (String [] args) {
ArrayList List=initArrayList ();;
System. Out. Println (" has 10 CARDS randomly generated for you, the order of playing CARDS is: ");
list.forEach(System.out::println);
Scanner sc=new Scanner(System.in);
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
While (true) {
String point="";
Do {
Try {
System. The out. Println (" please input points: ");
Point=sc. NextLine ();
String finalPoint=point;
If (Arrays. Stream (pokerArray). NoneMatch (p - & gt; P.e quals (finalPoint))) {
System. The out. Println (" input points are not legitimate, please enter again: ");
//empty, continue to enter circulation
Point="";
}
} the catch (Exception e) {
System. The out. Println (" input points not to recognize, please input again: ");
}
} while (". "equals (point));
Char decor=' ';
Do {
Try {
System. The out. Println (" please enter the color: ");
Decor=sc. NextLine (.) charAt (0);
boolean check=false;
For (char c: decorArray) {
If (c==decor) {
check=true;
break;
}
}
if (! Check) {
System. The out. Println (" input of design and color is not legal, please enter again: ");
Decor=' ';
}
} the catch (Exception e) {
System. The out. Println (" input of design and color is not to recognize, please input again: ");
}
} while (decor==");
Poker p=new Poker (point, decor);
Int index=getIndex (list, p);
if (index !=1) {
List. The add (the index, p);
System. The out. Println (" insertion is successful, the current order of playing CARDS is: ");
list.forEach(System.out::println);
} else {
System. The out. Println (" current has been on the CARDS: ");
}
}

}

Private static ArrayList InitArrayList () {
ArrayList List=new ArrayList<> (a);
Do {
Poker p=new Poker (pokerArray [(int) (Math. The random () * 13)], decorArray [(int) (Math. The random () * 4)]);
Int index=getIndex (list, p);
if (index !=1) {
//card repeat, do not add
List. The add (the index, p);
}
} while (list. The size () & lt; 10);
return list;
}

Private static int getIndex (ArrayList The list, Poker p) {
If (a list. The size ()==0) {
return 0;
}
for (int i=0; I & lt; List. The size (); I++) {
//points less than the ith a sorted list object, showing the position of being inserted just before the element
If (getPointNum (p) & lt; GetPointNum (list. Get (I))) {
return i;
}
If (getPointNum (p)==getPointNum (list. Get (I))) {
//points less than equal to the design and color is less than a sorted list of the objects, I also want to insert position just before the element
If (getDecorNum (p) & lt; GetDecorNum (list. Get (I))) {
return i;
}
//if design and color is equal, that means repeat
If (getDecorNum (p)==getDecorNum (list. Get (I))) {
return -1;
}
}
}
//traverse shows the latest row is larger than any one element
Return the list. The size ();
}


Private static int getPointNum Poker (p) {
Int point=0;
for(int i=0; iIf (pokerArray [I] equals (p.g etPoint ())) {
Point=I + 1;
}
}
Return point.
}

Private static int getDecorNum Poker (p) {
Int decor=0;
for(int i=0; iIf (decorArray [I]==p.g etDecor ()) {
Decor=I + 1;
}
}
Return decor;
}

CodePudding user response:

Poker object code
 
Public class Poker {
String point;//points
Character decor;
//design and color
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 +' \ ' '+
'} ';
}
}

CodePudding user response:

Results:
Have 10 CARDS randomly generated for you, the order of playing CARDS is:
Poker {, decor point='A'='? '}
Poker {point='2', decor='? '}
Poker {point='2', decor='? '}
Poker {point='4', decor='? '}
Poker {point='6', decor='? '}
Poker {point='7', decor='? '}
Poker {point='7', decor='? '}
Poker {point='8', decor='? '}
Poker {point='8', decor='? '}
Poker {point='Q', decor='? '}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Please input points:
4
Please enter the color:
A
Input of design and color is not legal, please enter again:
Please enter the color:
?
Currently on the card:
Please input points:
4
Please enter the color:
?
Insert the success, the current order of playing CARDS is:
Poker {, decor point='A'='? '}
Poker {point='2', decor='? '}
Poker {point='2', decor='? '}
nullnullnullnullnullnullnullnullnull
  • Related