/**
* Created by y0n on 2017/4/17.
* 1.输出控制台传递的默认参数
*/
public class JavaDay001_1 {
public static void main(String[] args) {
System.out.println(args);
}
}
/**
* Created by y0n on 2017/4/17.
* 2.编程求表达式的结果
*/
public class JavaDay001_2 {
public static void main(String[] args) {
System.out.println("128 & 129 = " + (128 & 129));
System.out.println("56 | 32 = " + (56 | 32));
System.out.println("27 ^ 123 = " + (27 ^ 123));
}
}
/**
* Created by y0n on 2017/4/17.
* 3.从键盘上输入3个数,求出这三个数字中的最大值,并将最大值输出
*/
import java.util.Scanner;
public class JavaDay001_3 {
public static void main(String[] args) {
System.out.println("请输入3个数:");
Scanner scanner = new Scanner(System.in);
int nNumA = scanner.nextInt();
int nNumB = scanner.nextInt();
int nNumC = scanner.nextInt();
int nMax = 0;
if (nNumA >= nNumB && nNumA >= nNumC)
{
nMax = nNumA;
}
else if (nNumB >= nNumC && nNumB >= nNumA)
{
nMax = nNumB;
}
else if (nNumC >= nNumA && nNumC >= nNumB)
{
nMax = nNumC;
}
else
{
System.out.println("无法比较!");
}
System.out.println("Max = " + nMax);
}
}
/**
* Created by y0n on 2017/4/17.
* 4.编写万年历小程序
* 输出结果:
* 请输入年:2016
* 请输入月:6
* 2016年非润年
* 2016年6月份的天数是30
*/
import com.sun.webkit.BackForwardList;
import java.util.Scanner;
public class JavaDay001_4 {
public static void main(String[] args) {
System.out.println("请输入年:");
Scanner scanner = new Scanner(System.in);
int nYear = scanner.nextInt();
System.out.println("请输入月:");
int nMounth = scanner.nextInt();
int nflag = 0;
if (nYear % 4 == 0)
{
if (nYear % 100 == 0)
{
if (nYear % 400 == 0)
{
nflag = 1;
System.out.println(nYear + "年是润年");
}
else
{
System.out.println(nYear + "年不是润年");
}
}
else
{
nflag = 1;
System.out.println(nYear + "年不是润年");
}
}
else
{
System.out.println(nYear + "年不是润年");
}
//判断2016年月份的天数
if (nMounth == 1
|| nMounth == 3
|| nMounth == 5
|| nMounth == 7
|| nMounth == 8
|| nMounth == 10
|| nMounth == 12 )
{
System.out.println(nYear + "年" + nMounth + "月份的天数是:31" );
}
else if (nMounth == 4
|| nMounth == 6
|| nMounth == 9
|| nMounth == 11)
{
System.out.println(nYear + "年" + nMounth + "月份的天数是:30" );
}
else
{
if (nflag == 0)
{
System.out.println(nYear + "年" + nMounth + "月份的天数是:28" );
}
else
{
System.out.println(nYear + "年" + nMounth + "月份的天数是:29" );
}
}
}
}
/**
* Created by y0n on 2017/4/17.
* 5.从键盘输入学生学号:20161103(2016年+11期+03号)
* 使用“/”和“%”云算符分解学生学号获得年份期数和序号
* 并且输出:
* 学生学号:20161103
* 学生入学年份:2016年
* 学生期数:11期
* 学生序号:03号
*/
import java.util.Scanner;
public class JavaDay001_5 {
public static void main(String [] args)
{
System.out.println("请输入学生学号:");
Scanner scanner = new Scanner(System.in);
int nStudent = scanner.nextInt();
int nStudentYear = nStudent / 10000;
int nStudentTime = (nStudent / 100) % nStudentYear;
int nStudentId = nStudent % (nStudent / 100);
System.out.println("学生学号:" + nStudent);
System.out.println("学生入学年份:" + nStudentYear + "年");
System.out.println("学生期数:" + nStudentTime + "期");
System.out.println("学生序号:" + nStudentId + "号");
}
}
/**
* Created by y0n on 2017/4/17.
* 6.为指定成绩加分,直到分数大于等于60为止,
* 输出加分前和加分后的成绩,并统计加分的次数
* 输出结果:
* 加分前成绩:55
* 加分后成绩:60
* 共加了5次!
*/
import java.util.Scanner;
public class JavaDay001_6 {
public static void main(String [] args)
{
System.out.println("请指定分数:");
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
int nCount = 0;
if (score < 60)
{
int temp = score;
while (temp < 60)
{
temp++;
nCount++;
}
System.out.println("加分前成绩:" + score);
System.out.println("加分后成绩:" + temp);
System.out.println("共加了" + nCount + "次!");
}
else
{
System.out.println("不用加分!");
}
}
}
/**
* Created by y0n on 2017/4/17.
*7.画图
*/
public class JavaDay001_7 {
public static void main(String [] args){
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 7; j++)
{
if (i == 0 && j == 3)
{
System.out.print("*");
}
else if(i == 1 && j < 5 && j > 1)
{
System.out.print("*");
}
else if(i == 2 && j < 6 && j > 0)
{
System.out.print("*");
}
else if (i == 3)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 7; j++)
{
if(i == 0 && j < 6 && j > 0)
{
System.out.print("*");
}
else if (i == 1 && j < 5 && j > 1)
{
System.out.print("*");
}
else if (i == 2 && j ==3)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
/**
* Created by y0n on 2017/4/17.
* 8.键盘输入五位数字的会员卡号:38503
* 使用“/”和“%”运算符分解获得的会员卡每一位上的数字
* 将每一位数字求和
*/
import java.util.BitSet;
import java.util.Scanner;
public class JavaDay001_8 {
public static void main(String []args)
{
System.out.println("请输入五位数会员卡号:");
Scanner scanner = new Scanner(System.in);
int nVIPCard = scanner.nextInt();
int nW = nVIPCard / 10000;
int nQ = (nVIPCard / 1000) % 10;
int nB = (nVIPCard / 100) % 10;
int nS = (nVIPCard / 10) % 10;
int nG = nVIPCard % 10;
System.out.println("会员卡号:" + nVIPCard);
System.out.println("万位数:" + nW
+ " 千位数:" + nQ
+ " 百位数:" + nB
+ " 十位数:" + nS
+ " 个位数:" + nG);
System.out.println("会员卡号" + nVIPCard
+ "每位的总和为:"
+ (nW + nQ + nB + nS + nG));
}
}
/**
* Created by y0n on 2017/4/17.
* 9.编写万年历程序
*/
import java.util.Scanner;
public class JavaDay001_9 {
static String CaculateWeekDay(int y,int m,int d)
{
if(m==1)
{
m = 13;
y--;
}
if(m==2)
{
m=14;
y--;
}
int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
String weekstr = null;
switch(week)
{
case 0: weekstr="星期一"; break;
case 1: weekstr="星期二"; break;
case 2: weekstr="星期三"; break;
case 3: weekstr="星期四"; break;
case 4: weekstr="星期五"; break;
case 5: weekstr="星期六"; break;
case 6: weekstr="星期日"; break;
}
return weekstr;
}
public static void main(String [] args)
{
int [][] day = new int[5][7];
System.out.print("请选择年份:");
Scanner scanner = new Scanner(System.in);
int nYear = scanner.nextInt();
System.out.print("请选择月份:");
int nMounth = scanner.nextInt();
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
int bflag = 0;
String strWeek = CaculateWeekDay(nYear, nMounth, 1);
if (strWeek.equals("星期一"))
{
day[0][1] = 1;
}
if (strWeek.equals("星期二"))
{
day[0][2] = 1;
}
if (strWeek.equals("星期三"))
{
day[0][3] = 1;
}
if (strWeek.equals("星期四"))
{
day[0][4] = 1;
}
if (strWeek.equals("星期五"))
{
day[0][5] = 1;
}
if (strWeek.equals("星期六"))
{
day[0][6] = 1;
bflag = 1;
}
if (strWeek.equals("星期日"))
{
day[0][0] = 1;
}
if (nMounth == 1 ||
nMounth == 3 ||
nMounth == 5 ||
nMounth == 7 ||
nMounth == 8 ||
nMounth == 10 ||
nMounth == 12)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 7; j++)
{
if (day[i][j] == 1)
{
for (int m = 0; m < 31; m++)
{
System.out.print(day[i][j] + m + "\t\t");
if ((j + m + 1) % 7 == 0)
{
System.out.println();
}
}
}
else
{
System.out.print("\t\t");
}
}
}
}
else if (nMounth == 4 ||
nMounth == 6 ||
nMounth == 9 ||
nMounth == 11)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 7; j++)
{
if (day[i][j] == 1)
{
for (int m = 0; m < 30; m++)
{
System.out.print(day[i][j] + m + "\t\t");
if ((j + m + 1) % 7 == 0)
{
System.out.println();
}
}
}
else
{
System.out.print("\t\t");
}
}
}
}
else
{
if ((nYear % 4 == 0 || nYear % 400 == 0) && nYear % 100 != 0)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 7; j++)
{
if (day[i][j] == 1)
{
for (int m = 0; m < 29; m++)
{
System.out.print(day[i][j] + m + "\t\t");
if ((j + m + 1) % 7 == 0)
{
System.out.println();
}
}
}
else
{
System.out.print("\t\t");
}
}
}
}
else
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 7; j++)
{
if (day[i][j] == 1)
{
for (int m = 0; m < 28; m++)
{
System.out.print(day[i][j] + m + "\t\t");
if ((j + m + 1) % 7 == 0)
{
System.out.println();
}
}
}
else
{
System.out.print("\t\t");
}
}
}
}
}
}
}
/**
* Created by y0n on 2017/4/18.
* 1.编写一个代表地址的Address类,地址信息由国家,身份,城市,街道,邮编组成
* 并可以返回完整的地址信息
*/
public class JavaDay002_1 {
String Country;
String Identifiy;
String City;
String Street;
String Stamp;
public void setCountry(String country) {
Country = country;
}
public void setIdentifiy(String identifiy) {
Identifiy = identifiy;
}
public void setCity(String city) {
City = city;
}
public void setStreet(String street) {
Street = street;
}
public void setStamp(String stamp) {
Stamp = stamp;
}
public String getCountry() {
return Country;
}
public String getIdentifiy() {
return Identifiy;
}
public String getCity() {
return City;
}
public String getStreet() {
return Street;
}
public String getStamp() {
return Stamp;
}
public static void main(String []args)
{
JavaDay002_1 address = new JavaDay002_1();
address.setCountry("Chain");
address.setIdentifiy("Student");
address.setCity("Beijing");
address.setStreet("XinFuRoad");
address.setStamp("402310");
System.out.println("地址:" +
address.getCountry() + "_" +
address.getIdentifiy() + "_" +
address.getCity() + "_" +
address.getStreet() + "_" +
address.getStamp());
}
}
/**
* Created by y0n on 2017/4/18.
* 2.设计一个Dog类,有名字,颜色,年龄等属性,定义构造方法来初始化类的这些属性,
* 定义方法输出Dog信息,编写应用程序使用Dog类
*/
public class JavaDay002_2 {
String strName;
String strColor;
String strAge;
public JavaDay002_2(String strName, String strColor, String strAge) {
this.strName = strName;
this.strColor = strColor;
this.strAge = strAge;
}
void ShowDog()
{
System.out.println("此狗叫" + strName + "," + strColor + "色," + strAge + "岁");
}
public static void main(String []args)
{
JavaDay002_2 Dog = new JavaDay002_2("旺财", "白", "2");
Dog.ShowDog();
}
}
/**
* Created by y0n on 2017/4/18.
* 3.定义一个数组,它可以存储一个矩形,三角形,圆形
* 一个双精度数或一个整数
*/
public class JavaDay002_3 {
int Rectangle(int length, int wight)
{
return length * wight;
}
int Trangle(int aSide, int bSide, int cSide)
{
int s = (aSide + bSide + cSide) / 2;
double area = (double) (s * (s - aSide) * (s - bSide) * (s - cSide));
return (int) Math.pow(area, 0.5);
}
int Round(int redis)
{
return (int) (redis * redis * 3.14);
}
int IntNumber(int nNum)
{
return nNum;
}
public static void main(String []args)
{
JavaDay002_3 array = new JavaDay002_3();
System.out.println("矩形:" + array.Rectangle(3, 4)
+ " 三角形:" + array.Trangle(3, 4, 5)
+ " 圆:" + array.Round(2)
+ " 整数:" + array.IntNumber(5));
}
}
/**
* Created by y0n on 2017/4/18.
* 4.定义一个抽象类Shape,用类标示二维图形,
* Shape具有抽象方法area和perimeter,分别用来计算形状的面积和周长,
* 定义一些二维形状类(矩形,三角形,圆)
* 这些类都是Shape类的子类
*/
public abstract class JavaDay002_4 {
abstract int area();
abstract int perimeter();
}
class Rect extends JavaDay002_4
{
int length;
int wight;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWight() {
return wight;
}
public void setWight(int wight) {
this.wight = wight;
}
@Override
int area() {
return length * wight;
}
@Override
int perimeter() {
return (length + wight) * 2;
}
}
class Trangle extends JavaDay002_4
{
int aSide;
int bSide;
int cSide;
public int getaSide() {
return aSide;
}
public void setaSide(int aSide) {
this.aSide = aSide;
}
public int getbSide() {
return bSide;
}
public void setbSide(int bSide) {
this.bSide = bSide;
}
public int getcSide() {
return cSide;
}
public void setcSide(int cSide) {
this.cSide = cSide;
}
@Override
int area() {
int s = (aSide + bSide + cSide) / 2;
double area = (double) (s * (s - aSide) * (s - bSide) * (s - cSide));
return (int) Math.pow(area, 0.5);
}
@Override
int perimeter() {
return aSide + bSide + cSide;
}
}
class Round extends JavaDay002_4
{
int Rides;
public int getRides() {
return Rides;
}
public void setRides(int rides) {
Rides = rides;
}
@Override
int area() {
return (int) (Rides * Rides * 3.14);
}
@Override
int perimeter() {
return (int) (2 * Rides * 3.14);
}
}
class ExObj
{
public static void main(String []args)
{
Rect rc = new Rect();
rc.setLength(3);
rc.setWight(4);
System.out.println("矩形面积 = " + rc.area()
+ ",矩形周长 = " + rc.perimeter());
Trangle tg = new Trangle();
tg.setaSide(3);
tg.setbSide(4);
tg.setcSide(5);
System.out.println("三角形面积 = " + tg.area()
+ ",三角形周长 = " + tg.perimeter());
Round rd = new Round();
rd.setRides(2);
System.out.println("圆面积 = " + rd.area()
+ ",圆周长 = " + rd.perimeter());
}
}
/**
* Created by y0n on 2017/4/18.
* 5.编译3个接口,他们之间具有继承关系,
* 每个接口中包含一个常量,通过一个类继承这些接口
* 通过显示接口中的常量字符串类展示这些接口的继承性
*/
interface InterfaceA
{
public final String InterfaceA = "InterfaceA";
abstract void ShowStringA();
}
interface InterfaceB extends InterfaceA
{
public final String InterfaceB = "InterfaceB";
abstract void ShowStringB();
}
interface InterfaceC extends InterfaceB
{
public final String InterfaceC = "InterfaceC";
abstract void ShowStringC();
}
public class JavaDay002_5 implements InterfaceA, InterfaceB, InterfaceC{
@Override
public void ShowStringA() {
System.out.println(InterfaceA);
}
@Override
public void ShowStringB() {
System.out.println(InterfaceB);
}
@Override
public void ShowStringC() {
System.out.println(InterfaceC);
}
public static void main(String []args)
{
JavaDay002_5 Interface = new JavaDay002_5();
Interface.ShowStringA();
Interface.ShowStringB();
Interface.ShowStringC();
}
}
/**
* Created by y0n on 2017/4/18.
* 6.建立一个人类(Penson)和学生类(Student),功能如下:
* Person中包含4个保护型成员姓名(name),地址(addr)
* 性别(sex),年龄(age),用一个4个参数的构造函数
* 初始化这4个属性,以及一个输出方法显示i这4个属性
* Student类继承Person类,并增加输出成员数学(math)
* 英语(english)成绩,用一个6参数构造函数初始化属性,
* 和重写输出方法用于显示6个属性。
*/
class Penson
{
protected String name;
protected String addr;
protected String sex;
protected int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
void ShowItems()
{
System.out.println("姓名:" + getName()
+ " 地址:" + getAddr()
+ " 性别:" + getSex()
+ " 年龄:" + getAge());
}
}
class Student extends Penson
{
int mathScore;
int englishScore;
public Student(String name, String addr, String sex, int age, int mathScore, int englishScore) {
this.mathScore = mathScore;
this.englishScore = englishScore;
this.name = name;
this.addr = addr;
this.sex = sex;
this.age = age;
}
void ShowItems()
{
System.out.println("姓名:" + getName()
+ " 地址:" + getAddr()
+ " 性别:" + getSex()
+ " 年龄:" + getAge()
+ " 数学成绩:" + mathScore
+ " 英语成绩:" + englishScore);
}
}
public class JavaDay002_6 {
public static void main(String []args)
{
Student stu = new Student("y0n", "Beijing", "boy", 5, 80, 90);
stu.ShowItems();
}
}
/**
* Created by y0n on 2017/4/18.
* 7.定义一个接口,并使用匿名类部类方式创建接口实例
*/
interface InterFace
{
abstract void fun();
}
class ImpClass implements InterFace
{
@Override
public void fun() {
System.out.println("匿名内部类1");
}
}
public class JavaDay002_7{
public static void main(String []args)
{
//new出来的内部类还要赋值给一个接口?
InterFace i = new ImpClass()
{
@Override
public void fun() {
super.fun();
System.out.println("匿名内部类2");
}
};
i.fun();
}
}
/**
* Created by y0n on 2017/4/19.
* 1.声明一个图书类,其数据成员为书名,编号,作者
* 出版社名,书价,并拥有静态数据册数,记录图书馆总册数,实现简单的添加
* 查询删除
* 使用ArrayList类保存数据,查询规则自定
*/
class Book
{
String strName;
static int nID;
String strAuthoName;
String strPushName;
int priceBook;
static int nNumBook;
int totalBookNum;
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
public static int getnID() {
return nID;
}
public static void setnID(int nID) {
Book.nID = nID;
}
public String getStrAuthoName() {
return strAuthoName;
}
public void setStrAuthoName(String strAuthoName) {
this.strAuthoName = strAuthoName;
}
public String getStrPushName() {
return strPushName;
}
public void setStrPushName(String strPushName) {
this.strPushName = strPushName;
}
public int getPriceBook() {
return priceBook;
}
public void setPriceBook(int priceBook) {
this.priceBook = priceBook;
}
public static int getnNumBook() {
return nNumBook;
}
public static void setnNumBook(int nNumBook) {
Book.nNumBook = nNumBook;
}
public int getTotalBookNum() {
return totalBookNum;
}
public void setTotalBookNum(int totalBookNum) {
this.totalBookNum = totalBookNum;
}
//添加
List<Book> AddBook(String bookName, int nId, String authoName, String PushName, int priceBook, int numBook)
{
Book bk = new Book();
List<Book> list = new ArrayList<Book>();
bk.setStrName(bookName);
Book.setnID(nId);
bk.setStrAuthoName(authoName);
bk.setStrPushName(PushName);
bk.setPriceBook(priceBook);
//Book.setnNumBook(numBook);
bk.setTotalBookNum(Book.getnNumBook() + numBook);
list.add(bk);
return list;
}
//查询
Book SelectBook(List<Book> al, int nID)
{
//List<Book> list = new ArrayList<Book>();
for (int x = 0; x < al.size(); x++)
{
if (al.get(nID) != null)
{
return al.get(nID);
}
else
{
System.out.println("没找到!");
}
}
return null;
}
//删除
void DeleteBook(List<Book> al, int nID)
{
Book bk = al.remove(nID);
if (bk != null)
{
System.out.println("删除成功!");
}
}
}
public class JavaDay003_1 {
public static void main(String []args)
{
Book bk = new Book();
List<Book> list = new ArrayList<Book>();
list = bk.AddBook("MathBook", 1, "y0n", "xxxPusher", 10, 10);
bk = bk.SelectBook(list, 0);
System.out.println("书名:" + bk.getStrName()
+ " 编号:" + Book.getnID()
+ " 作者名:" + bk.getStrAuthoName()
+ " 出版社名:" + bk.getStrPushName()
+ " 书价:" + bk.getPriceBook()
+ " 册数:" + Book.getnNumBook()
+ " 总数:" + bk.getTotalBookNum());
bk.DeleteBook(list, 0);
}
}
/**
* Created by y0n on 2017/4/19.
* 2.编写一个程序测试异常,
* 该类提供一个输入整数的方法,然后使用这个方法先后输入
* 两个整数,再用第一个整数除以第二个整数,当第一个整数为0是抛出异常
*/
class ExceptionNum
{
void inPutNum(int nNumA, int nNumB)
{
try {
int nNumC = nNumA / nNumB;
}
catch (Exception e)
{
System.out.println("发生异常:" + e.getMessage());
return;
}
return;
}
}
public class JavaDay003_2 {
public static void main(String []args)
{
ExceptionNum en = new ExceptionNum();
en.inPutNum(1, 0);
}
}
/**
* Created by y0n on 2017/4/19.
* 3.完成下列要求,写一程序进行异常验证
* 1)定义一个继承于Exception类的异常类
* InsufficientFundException
* 2)定义一个银行账户类Account,包括
* 属性:账户名称,name,String
* 存款余额balance,double
* 构造函数:Account(String name, double balance);
* 取款操作:void withdrawal(double amount),若大于余额则抛出异常
*/
class InsufficientFundException extends Exception
{
}
class Account
{
String name;
double balance;
public Account(String name, double balance)
{
this.name = name;
this.balance = balance;
}
void withdrawal(double amount) throws InsufficientFundException
{
if (amount > this.balance) {
throw new InsufficientFundException();
}
else
{
this.balance = this.balance - amount;
System.out.println("取款成功!\n余额:" + this.balance);
}
}
}
public class JavaDay003_3 {
public static void main(String []args)
{
try {
Account ac = new Account("y0n", 50);
ac.withdrawal(40);
ac.withdrawal(40);
}
catch (InsufficientFundException e)
{
System.out.print("余额不足!");
}
}
}
小例子:
import java.util.*;
/**
* Created by y0n on 2017/4/19.
* 4.小游戏
* 玩家名字异常检测(不能是数字,只能只能是字符)
* 随机给玩家3张牌
* 玩家将3张牌中最大的牌输出
*/
class Card{
private String color;
private String number;
public Card(String color, String number) {
this.color = color;
this.number = number;
}
public String getColor() {
return color;
}
public String getNumber() {
return number;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Card))
return false;
Card other = (Card) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
return true;
}
}
class Cards {
private List<Card> list = new ArrayList<Card>();
//创建一副扑克牌
public Cards(){
System.out.println("-----------------创建扑克牌------------------");
String[] color = {"黑桃", "红桃", "梅花", "方片"};
String[] number = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J","Q","K", "A"};
for(int i=0;i<color.length;i++)
for(int j=0;j<number.length;j++){
list.add(new Card(color[i], number[j]));
}
System.out.println("----------------扑克牌创建成功!---------------");
}
//获取一副扑克牌
public List<Card> getList() {
return list;
}
//洗牌(打乱)
public void shufCards(){
System.out.println("----------------开始洗牌------------------------");
Collections.shuffle(list);
System.out.println("----------------洗牌结束------------------------");
}
//展示一副扑克牌
public void showCards(){
System.out.print("当前的扑克牌为:");
System.out.print("[ ");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i).getColor() + list.get(i).getNumber()+ " ");
}
System.out.println(" ]");
}
}
class Player {
private int id;
private String name;
private List<Card> handCards = new ArrayList<Card>();
public Player(int id, String name){
this.id = id;
this.name = name;
}
public List<Card> getHandCards() {
return handCards;
}
public void setHandCards(Card card) {
handCards.add(card);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CardComparator implements Comparator<Card> {
@Override
public int compare(Card c1, Card c2) {
// 构建花色和牌值数组,通过比对,计算得到某张牌的价值(大小)
String[] color = {"方片", "梅花", "红桃", "黑桃"};
String[] number = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J","Q","K", "A"};
//由于比较规则是先比较牌值,如果相等再比较花色(黑红梅方),所以将牌值赋予较高的权值
int valueOfC1 = 0;
int valueOfC2 = 0;
for(int i=0;i<number.length;i++){
if(c1.getNumber().equals(number[i])) valueOfC1 += i*10;
if(c2.getNumber().equals(number[i])) valueOfC2 += i*10;
}
for(int i=0;i<color.length;i++){
if(c1.getColor().equals(color[i])) valueOfC1 += i;
if(c2.getColor().equals(color[i])) valueOfC2 += i;
}
if( valueOfC1 > valueOfC2 ) return -1;
if( valueOfC1 < valueOfC2 ) return 1;
return 0;
}
}
public class JavaDay004_4 {
//创建玩家
//要对玩家ID的异常处理,要求用户只能输入整数ID,否则需要重新输入
public Player setPlayer(){
int id=0;
String name="";
Scanner console = new Scanner(System.in);
boolean ready = true;
do{
try{
System.out.println("输入ID:");
id = console.nextInt();
ready = true;
}catch(Exception e){
System.out.println("请输入整数类型的ID!");
ready = false;
console.nextLine();
}
}while(ready==false);
System.out.println("输入姓名:");
name = console.next();
return new Player(id, name);
}
public static void main(String[] args) {
//测试简易扑克牌程序
JavaDay004_4 game = new JavaDay004_4();
//(1)创建一副牌
Cards cards = new Cards();
//(2)展示新的扑克牌
cards.showCards();
//(3)洗牌
cards.shufCards();
//(4)创建玩家
System.out.println("--------------创建两个(or多个)玩家就可以开始游戏啦!-------------");
List<Player> p = new ArrayList<Player>();
for(int i=0;i<2;i++)
{
System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
p.add(game.setPlayer());
}
for(int i=0;i<p.size();i++)
{
System.out.println("欢迎玩家:"+p.get(i).getName());
}
//(5)扑克牌比大小游戏开始啦~
int count = 0;
System.out.println("------------------开始发牌---------------------");
//设定每人分别拿两张(or多张)
for(int i=0; i<2;i++){
//玩家轮流拿牌
for(int j=0; j< p.size(); j++){
System.out.println(">玩家"+p.get(j).getName()+"拿牌");
p.get(j).setHandCards(cards.getList().get(count));
count++;
}
}
System.out.println("------------------发牌结束!--------------------");
System.out.println("------------------开始游戏 ---------------------");
for(int i=0;i<p.size();i++){
System.out.print("玩家"+p.get(i).getName()+"的手牌为:[ ");
for(int j=0;j<p.get(i).getHandCards().size();j++){
Card cur = p.get(i).getHandCards().get(j);
System.out.print(cur.getColor()+cur.getNumber()+" ");
}
System.out.println(" ]");
}
//排序得到每个玩家最大的手牌(排序规则自定义)
for(int i=0;i<p.size();i++){
Collections.sort(p.get(i).getHandCards(), new CardComparator());
}
List<Card> maxCard = new ArrayList<Card>();
for(int i=0;i<p.size();i++){
Card maxCur = p.get(i).getHandCards().get(0);
System.out.println("玩家"+p.get(i).getName()+"最大的手牌为:"+ maxCur.getColor()+maxCur.getNumber());
maxCard.add(maxCur);
}
//得到最后的胜者
List<Card> temp = new ArrayList<Card>();
temp.addAll(maxCard);
Collections.sort(temp, new CardComparator());
for(int i=0;i<p.size();i++){
if(maxCard.get(i).equals(temp.get(0))) System.out.println("恭喜玩家:"+p.get(i).getName()+"获胜!");
}
}
}
---------------------
作者:u011337769
来源:****
原文:https://blog.****.net/u011337769/article/details/72629282
版权声明:本文为博主原创文章,转载请附上博文链接!