韩老师java课程

可变参数练习

package step;

import java.util.*;
public class byteOperator {
    public static void main(String[] args) {
    HspMethod h = new HspMethod();
    String s = h.showScore("董建",80,95,100,90,100);
    System.out.println(s);
    }
}
class HspMethod{
    public String  showScore(String name,int ...score){
        int sum = 0;
        for(int i = 0; i < score.length; i++){
            sum+=score[i];
        }
        return name + score.length+"门课的" + "总成绩为:" +sum;
    }
}

this练习

package step;

import java.util.*;
public class byteOperator {
    public static void main(String[] args) {
    Person p1 = new Person(23,"董建");
    Person p2 = new Person(23,"董光建");
        System.out.println(p1.compareTo(p2));
    }
}
class Person{
    int age;
    String name;
    public Person(){

    }
    public Person(int age,String name){
        this.age = age;
        this.name = name;
    }
    public boolean compareTo(Person p1){
        return this.name.equals(p1.name)&&this.age == p1.age;
       //this就是当前对象
    }

}

圆面积

package step;

public class Circle_Test {
    public static void main(String[] args) {
        Circle c = new Circle(2.5);
        System.out.println(c.area());

    }
}
class Circle{
    double r;
    public Circle(double r){
        this.r = r;
    }
    public double area(){
        return 2.0 * Math.PI * Math.pow(r,2);
    }
}

匿名对象使用

package step;

public class Test {
    int count = 10;//属性
    public void count1(){
        count = 11; //这个count就是属性
        System.out.println(count);
    }
    public void count2(){
        count++;
        System.out.println(count);
    }
    public static void main(String[] args) {
        new Test().count1();//调用对象方法count1(),输出11
        //new Test()是匿名对象,
        // 匿名对象使用后就不能使用,只能用一次
        Test t1 = new Test();//此时匿名对象已销毁
        //新开辟一个堆空间
        t1.count2();//输出11
        t1.count2();//12
    }
}

在这里插入图片描述

package step;

public class Test{
    public static void main(String[] args) {
        Cricle c1 = new Cricle();
        PassObject p = new PassObject();
        p.printAreas(c1,5);
    }
}
class Cricle{
    double r;

    public Cricle(){

    }
    public Cricle(double r){
        this.r = r;
    }
    public double findArea(){
        return  Math.PI * Math.pow(r,2);
    }
    public void setR(double r){
        this.r = r;
    }

}
class PassObject{
    public void printAreas(Cricle c,int times){
        for(int i = 1 ;i <= times; i++){
            c.setR(i); //在这设置半径的值
            //搞一个set方法设置半径,
            //也可以直接用c.r设置
            System.out.println(c.findArea());
        }
    }
}

与计算机玩猜拳游戏
重点是面向对象的设计思想

package step;
import java.util.Random;
import java.util.Scanner;

/*                                                                                                    
请编写一个猜拳的游戏                                                                                            
有个人 Tom,设计他的成员变量. 成员方法, 可以电脑猜拳. 电脑每次都会随机生成 0, 1, 2                                                    
0 表示 石头 1 表示剪刀 2 表示 布                                                                                 
并要可以显示 Tom的输赢次数(清单), 假定 玩三次.                                                                          
 */
// 测试类,主类
public class MoraGame {

    // 测试                                                                                             
    public static void main(String[] args) {
        // 创建一个玩家对象                                                                                   
        Tom t = new Tom();
        // 用来记录最后输赢的次数                                                                                
        int isWinCount = 0;

        // 创建一个二维数组,用来接收局数,Tom出拳情况以及电脑出拳情况                                                            
        int[][] arr1 = new int[3][3];
        int j = 0;

        // 创建一个一维数组,用来接收输赢情况                                                                          
        String[] arr2 = new String[3];

        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {   //比赛3次                                                              
            // 获取玩家出的拳                                                                                
            System.out.println("请输入你要出的拳(0-拳头,1-剪刀,2-布):");
            int num = scanner.nextInt();
            t.setTomGuessNum(num);
            int tomGuess = t.getTomGuessNum();
            arr1[i][j + 1] = tomGuess;

            // 获取电脑出的拳                                                                                
            int comGuess = t.computerNum();
            arr1[i][j + 2] = comGuess;

            // 将玩家猜的拳与电脑做比较                                                                           
            String isWin = t.vsComputer();
            arr2[i] = isWin;
            arr1[i][j] = t.count;

            // 对每一局的情况进行输出                                                                            
            System.out.println("=========================================");
            System.out.println("局数\t玩家的出拳\t电脑的出拳\t输赢情况");
            System.out.println(t.count + "\t" + tomGuess + "\t\t" + comGuess + "\t\t" + t.vsComputer());
            System.out.println("=========================================");
            System.out.println("\n\n");
            isWinCount = t.winCount(isWin);
        }

        // 对游戏的最终结果进行输出                                                                               
        System.out.println("局数\t玩家的出拳\t电脑的出拳\t\t输赢情况");
        for (int a = 0; a < arr1.length; a++) {
            for (int b = 0; b < arr1[a].length; b++) {
                System.out.print(arr1[a][b] + "\t\t\t");
            }

            System.out.print(arr2[a]);
            System.out.println();
        }
        System.out.println("你赢了" + isWinCount + "次");
    }

}

// Tom类
class Tom {     // 核心代码  
    // 玩家出拳的类型
    int tomGuessNum; //0,1,2
    // 电脑出拳的类型
    int comGuessNum; //0,1,2
    // 玩家赢的次数
    int winCountNum;
    // 比赛的次数
    int count = 1;   //一共比赛3次                                                                                 
    
    public void showInfo() {
        //....
    }

    /**
     * 电脑随机生成猜拳的数字的方法                                                                                 
     * @return
     */
    public int computerNum() {
        Random r = new Random();
        comGuessNum = r.nextInt(3);      // 方法 返回 0-2的随机数                                                             
        // System.out.println(comGuessNum);                                                           
        return comGuessNum;
    }

    /**
     * 设置玩家猜拳的数字的方法                                                                                   
     * @param tomGuessNum
     */
    public void setTomGuessNum(int tomGuessNum) {
        if (tomGuessNum > 2 || tomGuessNum < 0) {
            //抛出一个异常, 李同学会写,没有处理
            throw new IllegalArgumentException("数字输入错误");
        }
        this.tomGuessNum = tomGuessNum;
    }

    public int getTomGuessNum() {
        return tomGuessNum;
    }

    /**
     * 比较猜拳的结果                                                                                        
     * @return 玩家赢返回true,否则返回false                                                                    
     */
    public String vsComputer() {
        //比较巧
        if (tomGuessNum == 0 && comGuessNum == 1) {
            return "你赢了";
        } else if (tomGuessNum == 1 && comGuessNum == 2) {
            return "你赢了";
        } else if (tomGuessNum == 2 && comGuessNum == 0) {
            return "你赢了";
        } else if (tomGuessNum == comGuessNum){
            return "平手";
        } else {
            return "你输了";
        }
    }

    /**
     * 记录玩家赢的次数                                                                                       
     * @return
     */
    public int winCount(String s) {
        count++;    //控制玩的次数                                                                                   
        if (s.equals("你赢了")) {     //统计赢的次数                                                                   
            winCountNum++;
        }
        return winCountNum;
    }

}                                                                                                     
                                                                                                      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值