版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qwer1203355251/article/details/52939153
1.
{方块 5}{梅花 6}{红桃 3}{黑桃 3}
{梅花 Q}{黑桃 K}{方块 8}{方块 Q}
{梅花 7}{方块 4}{梅花 10}{红桃 K}
{方块 J}{梅花 4}{红桃 4}{方块 K}
{梅花 5}{黑桃 7}{方块 9}{黑桃 5}
{红桃 5}{梅花 J}{黑桃 A}{黑桃 8}
{红桃 10}{红桃 7}{黑桃 4}{红桃 A}
{黑桃 9}{方块 6}{梅花 8}{方块 A}
{红桃 6}{红桃 9}{方块 10}{梅花 3}
{黑桃 2}{黑桃 J}{梅花 A}{红桃 8}
{红桃 2}{红桃 J}{梅花 9}{梅花 2}
{黑桃 10}{红桃 Q}{方块 2}{黑桃 6}
{方块 3}{方块 7}{梅花 K}{黑桃 Q}
2.
运行结果:
[{狗娃 17}, {狗剩 22}, {美美 50}]
import java.util.*;
import javax.print.attribute.standard.SheetCollate;
class Person
{
int age;
String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
public String toString()
{
return "{"+name+" "+age+"}";
}
}
class Poker
{
String color;
String num;
public Poker(String color, String num) {
super();
this.num = num;
this.color = color;
}
public String toString()
{
return "{"+color+" "+num+"}";
}
}
public class Poker1
{
public static void main(String[] args)
{
LinkedList pokers = creatPoker();
//System.out.println(pokers);
//System.out.println(pokers.size());
//showPoker(pokers);
shufflePoker(pokers);
showPoker(pokers);
}
public static LinkedList creatPoker()//生成一副扑克牌
{
LinkedList list = new LinkedList();
String[] colors = {"黑桃","红桃","梅花","方块"};
String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for(int i=0;i<nums.length;i++)
{
for(int j=0;j<colors.length;j++)
{
list.add(new Poker(colors[j], nums[i]));
}
}
return list;
}
public static void showPoker(LinkedList pokers)//展示扑克牌
{
for(int i=0;i<pokers.size();i++)
{
System.out.print(pokers.get(i));
if(i%4==3)
System.out.println();
}
}
public static void shufflePoker(LinkedList pokers)
{
Random random = new Random();
for(int i=0;i<50;i++)
{
int index1 = random.nextInt(pokers.size());
int index2 = random.nextInt(pokers.size());
Poker poker1 = (Poker)pokers.get(index1);
Poker poker2 = (Poker)pokers.get(index2);
pokers.set(index1, poker2);
pokers.set(index2, poker1);
}
}
}
{方块 5}{梅花 6}{红桃 3}{黑桃 3}
{梅花 Q}{黑桃 K}{方块 8}{方块 Q}
{梅花 7}{方块 4}{梅花 10}{红桃 K}
{方块 J}{梅花 4}{红桃 4}{方块 K}
{梅花 5}{黑桃 7}{方块 9}{黑桃 5}
{红桃 5}{梅花 J}{黑桃 A}{黑桃 8}
{红桃 10}{红桃 7}{黑桃 4}{红桃 A}
{黑桃 9}{方块 6}{梅花 8}{方块 A}
{红桃 6}{红桃 9}{方块 10}{梅花 3}
{黑桃 2}{黑桃 J}{梅花 A}{红桃 8}
{红桃 2}{红桃 J}{梅花 9}{梅花 2}
{黑桃 10}{红桃 Q}{方块 2}{黑桃 6}
{方块 3}{方块 7}{梅花 K}{黑桃 Q}
2.
import java.util.*;
class Person
{
int age;
String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public String toString()
{
return "{"+name+" "+age+"}";
}
}
public class Paixu {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(new Person(17,"狗娃"));
list.add(new Person(22,"狗剩"));
list.add(new Person(50,"美美"));
for(int i=0;i<list.size();i++)
for(int j=i+1;j<list.size();j++)
{
Person p1 = (Person)list.get(i);
Person p2 = (Person)list.get(j);
if(p1.age>p2.age)
{
list.set(i, p2);
list.set(j, p1);
}
}
System.out.println(list);
}
}
运行结果:
[{狗娃 17}, {狗剩 22}, {美美 50}]