为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
Java入门第三季_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第三季

陈码农
难度入门
时长 5小时 0分
  • 练习的运行结果样例展示

    查看全部
  • 练习:Collections.sort()方法对泛型为String的List进行排序版本2

    查看全部
  • Collection  Map 存储对象


    查看全部
  • package game;


    import java.util.ArrayList;

    import java.util.List;


    /**

     * 玩牌的人

     * @author lenovo

     *

     */

    public class People {


    private Integer id;   

    private String name;

    //扑克牌集合

    private List<Puke> pukes;

    public People(Integer id,String name){

    this.id = id;

    this.name = name;

    this.pukes = new ArrayList<Puke>();

    }


    public Integer getId() {

    return id;

    }


    public String getName() {

    return name;

    }


    public List<Puke> getPukes() {

    return pukes;

    }


    public void setId(Integer id) {

    this.id = id;

    }


    public void setName(String name) {

    this.name = name;

    }


    public void setPukes(List<Puke> pukes) {

    this.pukes = pukes;

    }


    @Override

    public int hashCode() {

    final int prime = 31;

    int result = 1;

    result = prime * result + id;

    return result;

    }


    @Override

    public boolean equals(Object obj) {

    if (this == obj)

    return true;

    if (obj == null)

    return false;

    if (getClass() != obj.getClass())

    return false;

    People other = (People) obj;

    if (id != other.id)

    return false;

    return true;

    }


    }


    package game;


    public class Puke {

    //花色

    private String color;

    //花色的权值

    private Integer colorValue;

    //点数

    private String nums;

    //点数的权值

    private Integer numsValue;

    public Puke(String color ,Integer colorValue , String nums , Integer numsValue){

    this.color = color;

    this.colorValue = colorValue;

    this.nums = nums;

    this.numsValue = numsValue;

    }


    public String getColor() {

    return color;

    }


    public Integer getColorValue() {

    return colorValue;

    }


    public String getNums() {

    return nums;

    }


    public Integer getNumsValue() {

    return numsValue;

    }


    public void setColor(String color) {

    this.color = color;

    }


    public void setColorValue(Integer colorValue) {

    this.colorValue = colorValue;

    }


    public void setNums(String nums) {

    this.nums = nums;

    }


    public void setNumsValue(Integer numsValue) {

    this.numsValue = numsValue;

    }


    @Override

    public int hashCode() {

    final int prime = 31;

    int result = 1;

    result = prime * result + ((color == null) ? 0 : color.hashCode());

    result = prime * result + ((colorValue == null) ? 0 : colorValue.hashCode());

    result = prime * result + ((nums == null) ? 0 : nums.hashCode());

    result = prime * result + ((numsValue == null) ? 0 : numsValue.hashCode());

    return result;

    }


    @Override

    public boolean equals(Object obj) {

    if (this == obj)

    return true;

    if (obj == null)

    return false;

    if (getClass() != obj.getClass())

    return false;

    Puke other = (Puke) obj;

    if (color == null) {

    if (other.color != null)

    return false;

    } else if (!color.equals(other.color))

    return false;

    if (colorValue == null) {

    if (other.colorValue != null)

    return false;

    } else if (!colorValue.equals(other.colorValue))

    return false;

    if (nums == null) {

    if (other.nums != null)

    return false;

    } else if (!nums.equals(other.nums))

    return false;

    if (numsValue == null) {

    if (other.numsValue != null)

    return false;

    } else if (!numsValue.equals(other.numsValue))

    return false;

    return true;

    }


    }



    package game;


    import java.util.ArrayList;

    import java.util.Collections;

    import java.util.Iterator;

    import java.util.List;

    import java.util.Scanner;


    public class TestGame {

    //扑克牌

    private List<Puke> pukeList =  new ArrayList<Puke>();

    //玩家

    private List<People> peopleList = new ArrayList<People>();

    /**

    * 创建扑克牌

    */

    public void createPuke(){

    //创建一幅扑克牌

    String[] colors = {"黑桃","红桃","梅花","方片 "};

    String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

    pukeList =  new ArrayList<Puke>();

    //循环网扑克牌中添加值

    for(int i = 0 ;i<colors.length;i++){

    for(int j = 0 ;j < nums.length;j++ ){

    Puke p = new Puke(colors[i], i, nums[j], j);

    pukeList.add(p);

    }

    }

    }

    /**

    * 展示扑克牌信息

    */

    public void showPuke(){

    System.out.println("扑克牌信息为:");

    for (int i = 0; i < pukeList.size(); i++) {

    if(i==0){

    System.out.print(pukeList.get(i).getColor()+":" +pukeList.get(i).getNums());

    }else{

    System.out.print(","+pukeList.get(i).getColor()+":" +pukeList.get(i).getNums());

    }

    }

    System.out.println("");

    }

    /**

    * 洗牌

    * @param args

    */

    public void shufflePuke(){

    System.out.println("开始洗牌..................");

    Collections.shuffle(pukeList);

    System.out.println("洗牌结束!");

    }

    /**

    * 创建玩家

    */

    public void cratePeople(){

    int peopleNum = 2 ;

    Scanner console = null;

    for (int i = 0; i < peopleNum ; i++) {

    System.out.println("输入第"+ (i+1) +"位玩家的id和姓名:");

    while(true){

    try {

    System.out.println("请输入玩家id:");

    console = new Scanner(System.in);

    int id = console.nextInt();

    People p1 = new People(id, null);

    if(peopleList.contains(p1)){

    System.out.println("该id已存在");

    continue;

    }

    System.out.println("请输入玩家的姓名:");

    String name = console.next();

    p1.setName(name);

    peopleList.add(p1);

    break;

    } catch (Exception e) {

    //e.printStackTrace();

    System.out.println("玩家id格式输入不对");

    continue;

    }

    }

    }

    System.out.println(" 欢迎玩家:");

    for (People p : peopleList) {

    System.out.println("玩家id:"+p.getId()+",姓名:"+p.getName());

    }

    }

    /**

    * 开始发牌

    */

    public void getPuke(){

    System.out.println("开始发牌!!");

    //记录发到第几张牌

    int k=0;

    for(int i = 0 ;i <2;i++){

    for (People people : peopleList) {

    System.out.println("玩家"+ people.getId()+":"+people.getName()+"拿牌");

    people.getPukes().add(pukeList.get(k));

    k++;

    }

    }

    System.out.println("发牌结束!");

    }

    /**

    * 展示玩家手中的牌

    */

    public void showPeoplePuke(){

    Iterator<People> it = peopleList.iterator();

    while(it.hasNext()){

    People p = it.next();

    System.out.print("玩家"+p.getId()+":"+p.getName()+"手中的牌为:");

    for (Puke puke : p.getPukes()) {

    System.out.print(puke.getColor()+puke.getNums()+"  ");

    }

    System.out.println("");

    }

    }

    /**

    * 开始游戏

    * @param args

    */

    public void playGame(){

    System.out.println("开始游戏!");

    System.out.println("请各玩家展示手中最大的一张牌");

    for (People  people : peopleList) {

    //给玩家手中的牌进行排序

    Collections.sort(people.getPukes(), new PukeComparator());

    System.out.println("玩家:"+people.getName()+"手中最大的牌为:"+ people.getPukes().get(1).getColor()+people.getPukes().get(1).getNums());

    }

    //展示所有玩家最大的牌

    List<Puke> peoPleMaxList = new ArrayList<Puke>();

    for (People people : peopleList) {

    peoPleMaxList.add(people.getPukes().get(1));

    }

    Collections.sort(peoPleMaxList, new PukeComparator());

    Puke maxP = peoPleMaxList.get(1);

    System.out.println("最大的牌为:"+ maxP.getColor()+maxP.getNums());

    //判断谁是赢家

    for (int i = 0; i < peopleList.size(); i++) {

    if(peopleList.get(i).getPukes().contains(maxP)){

    System.out.println(peopleList.get(i).getName()+"获胜!");

    }

    }

    }

    public static void main (String[] args){

    TestGame tg = new TestGame();

    tg.createPuke();

    tg.showPuke();

    tg.shufflePuke();

    tg.showPuke();

    tg.cratePeople();

    tg.getPuke();

    tg.showPeoplePuke();

    tg.playGame();

    }

    }


    package game;


    import java.util.Comparator;


    public class PukeComparator implements Comparator<Puke> {


    @Override

    public int compare(Puke o1, Puke o2) {

    if(o1.getNumsValue()== o2.getNumsValue()){

    return o1.getColorValue().compareTo(o2.getColorValue());

    }else{

    return o1.getNumsValue().compareTo(o2.getNumsValue());

    }

    }


    }


    查看全部
  • hashSet中的contains()方法实现原理:先调用对象的hashCode()方法,返回对象的hash码值,再调用equals()进行判断该对象是否包含在hashSet中。

    查看全部
  • List接口的实现类:ArrayList 

    查看全部
  • 主要分为Collection Map根接口

    常用的 ArrayList HashSet , HashMap

    查看全部
  • 集合的作用

    查看全部
  • 处理异常

    try-catch以及try-catch-finally

    try{

    //一些会抛出异常的方法

    }catch(Exception e){

    //处理改异常的代码块

    }

    eg.

    try{

    //一些会抛出异常的方法

      System.out.print("请输入你的年龄:");

      Scanner input = new Scanner(System.in);

      int age = input.nextInt();

      System.out.println("十年后你"+(age+10)+"岁");

    }catch(InputMismatchException e){

    //处理该异常的代码块

    System.out.println("你应该输入整数");

    }

    System.out.println("程序结束啦!");

    //多重catch块的语法,也就是说在try块后用多个catch块来捕获多种类型的异常并对其进行相应的处理

    语法:

    try{

    //一些会抛出异常的方法

    }catch(Exception e){

    //处理该异常的代码块

    }catch(Exception2 e){

    //处理Exception2的代码块

    }...(n个catch块)...{

    }

    eg.

    Scanner input = new Scanner(System.in);

    try{

    //一些会抛出异常的方法

      System.out.print("请输入第一个数:");

      int one = input.nextInt();

      System.out.print("请输入第二个数:");

      int two = input.nextInt();

      System.out.println("两数相除结果为:"+ one/two);

    }catch(InputMismatchException e){

    //处理该异常的代码块

    System.out.println("你应该输入整数");

    }catch(ArithmeticEception e)

    {

    System.out.println("除数不能为零");

    }

    System.out.println("程序结束啦!");


    try{

    //一些会抛出异常的方法

    }catch(Exception e){

    //处理该异常的代码块

    }catch(Exception2 e){

    //处理Exception2的代码块

    }...(n个catch块)...{

    }finally{

    //最终将要执行的一些代码

    }

    https://img1.sycdn.imooc.com//5afa34b100018fb604830347.jpg

    查看全部
  • 异常简介

    异常处理的作用

    Java异常体系结构简介

    处理异常

    try-catch以及try-catch-finally

    抛出异常

    自定义异常

    异常链

    什么是异常?

    概念:有异于常态,和正常情况不一样,有错误出现。在编程上来讲我们把那些阻止当前方法或作用域继续执行的问题,我们称之为异常

    异常处理的作用和意义:

    将异常提示给编程人员或用户,使本来已经中断的程序以适当的方式继续运行或者退出并且能够保存用户的当前操作或者进行数据回滚,最后再把占用的资源释放掉。

    https://img1.sycdn.imooc.com//5afa286b0001b7b807360558.jpg

    查看全部
    0 采集 收起 来源:Java异常简介

    2018-05-15

  • 多重catch语句子类在前父类在后。
    查看全部
    0 采集 收起 来源:练习题

    2018-05-15

  • 思考题目:

    查看全部
  • public class HelloWorld {

        public static void main(String[] args) {

            

            // 定义double类型变量

    double a = 91.5;

            

             // 手动装箱

    Double b = new Double(a); 

            

            // 自动装箱

    Double c = a;      


            System.out.println("装箱后的结果为:" + b + "和" + c);

            

            // 定义一个Double包装类对象,值为8

    Double d = new Double(87.0);

            

            // 手动拆箱

    double e =  d.doubleValue()           ;

            

            // 自动拆箱

    double f =  d              ;

            

             System.out.println("拆箱后的结果为:" + e + "和" + f);

    }

    }


    查看全部
  • public class HelloWorld {

        public static void main(String[] args) {

    // 创建一个空的StringBuilder对象

            StringBuilder str = new StringBuilder();

    // 追加字符串

    str.append("jaewkjldfxmopzdm");

            // 从后往前每隔三位插入逗号

    for(int i = str.length()-3; i >0 ; i = i - 3){

        str.insert(i, ",");

    }

            

            

            // 将StringBuilder对象转换为String对象并输出

    System.out.print(str.toString());

    }

    }


    查看全部
  • 循环遍历Set,只能使用foreach,Iterator方法,不能使用fori方法,因为Set无序,不重复。

    查看全部

举报

0/150
提交
取消
课程须知
此部分为 Java 课程的进阶内容,适合具有一定 Java 基础的伙伴们学习,如果您是新手,建议您移步 《Java入门第一季》 和 《Java入门第二季》,在理解并掌握面向对象相关知识后再回来进修。
老师告诉你能学到什么?
本课程将学习 Java 中的异常处理、集合框架、字符串、常用类等,逐步学习掌握 Java 高级技术。
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!