大家好,又见面了,我是你们的朋友全栈君。
设计一个用户类User,类中的变量有用户名、密码和记录用户数量的变量,定义3个构造方法:无参的、为用户名赋值的、为用户名和密码赋值的,还有获取和设置密码的方法和返回类信息的方法。
package Java2;
class Poker
{
public class Card
{
private String suite;
private int face;
public Card(String suite, int face)
{
this.suite = suite;
this.face = face;
}
public String toString()
{
String faceStr = “”;
switch (face)
{
case 1:
faceStr = “A”;
break;
case 11:
faceStr = “J”;
break;
case 12:
faceStr = “Q”;
break;
case 13:
faceStr = “K”;
break;
default:
faceStr = String.valueOf(face);
}
return suite + faceStr;
}
}
private static String[] suites = { “黑桃”, “红桃”, “梅花”, “方块” };
private static int[] faces = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
private Card[] cards;
public Poker()
{
cards = new Card[52];
for (int i = 0; i < suites.length; i++)
{
for (int j = 0; j < faces.length; j++)
{
cards[i * 13 + j] = new Card(suites[i], faces[j]);
}
}
}
public void shuffle()
{
int len = cards.length;
for (int i = 0; i < len; i++)
{
int index = (int) (Math.random() * len);
Card temp = cards[index];
cards[index] = cards[i];
cards[i] = temp;
}
}
public Card getCard(int index)
{
return cards[index];
}
}
package Java2;
public class uerstest {
public static void main(String args[]){
User user1=new User(“huawei”,”46333″);
user1.print();
User user2=new User(“luhao”,”371623″);
user2.print();
User user3=new User(“ersha”,”371663″);
user2.print();
new User().count();
}
}
设计一副牌Poker的外部类和一张牌Card的内部类。
(1)Poker类中定义私有成员花色数组、点数数组以及一副牌的数组属性,提供构造方法(创建并初始化一副牌的数组)、随机洗牌方法shuffle(Math.random()获取[0,1)的随机数;获取[n,m)的随机数公式为Math.random()*(m-n)+n)和发牌方法deal。
(2)Card类中定义花色和点数属性,提供打印信息方法。
(3)定义测试类并在main()方法中创建一副牌Poker对象,并调用shufle()进行洗牌,调用deal()进行发牌。
package Java2;
class Poker
{
public class Card
{
private String suite;
private int face;
public Card(String suite, int face)
{
this.suite = suite;
this.face = face;
}
public String toString()
{
String faceStr = “”;
switch (face)
{
case 1:
faceStr = “A”;
break;
case 11:
faceStr = “J”;
break;
case 12:
faceStr = “Q”;
break;
case 13:
faceStr = “K”;
break;
default:
faceStr = String.valueOf(face);
}
return suite + faceStr;
}
}
private static String[] suites = { “黑桃”, “红桃”, “梅花”, “方块” };
private static int[] faces = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
private Card[] cards;
public Poker()
{
cards = new Card[52];
for (int i = 0; i < suites.length; i++)
{
for (int j = 0; j < faces.length; j++)
{
cards[i * 13 + j] = new Card(suites[i], faces[j]);
}
}
}
public void shuffle()
{
int len = cards.length;
for (int i = 0; i < len; i++)
{
int index = (int) (Math.random() * len);
Card temp = cards[index];
cards[index] = cards[i];
cards[i] = temp;
}
}
public Card getCard(int index)
{
return cards[index];
}
}
package Java2;
public class pokertest {
public static void main(String[] args)
{
Poker poker = new Poker();
poker.shuffle();
Poker.Card c1 = poker.getCard(0);
Poker.Card c2 = poker.getCard(2);
System.out.println(c1);
System.out.println(c2);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155114.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...