0031 练习(猜拳游戏)

 

 

 

 

 

 

 

 

 

 

 

/    1.编写类AA,定义方法max,实现求某个double数组的最大值,并返回

public class Homework09{

    public static void main(String[] args) {
        
        AA aa = new AA();
        double[] arr = {10,0.5,66,75,62,42.6};
        Double res = aa.max(arr);
        if(res != null){
            System.out.println("arr最大值=" + res);
        }else{
            System.out.println("输入有误,不能为null或{}");
        }
    }
}

class AA{
    public Double max(double[] arr){

        if(arr != null && arr.length > 0){
            double max = arr[0];
            for(int i = 1;i < arr.length;i++){
                if(max < arr[i]){
                    max = arr[i];
                }
            }
            return max;
        }else{
            return null;
        }
    }
}

//    2.编写类AA,定义方法find,实现查找某字符串是否在字符串数组中,并返回索引,找不到返回-1

public class Homework09{

    public static void main(String[] args) {

        String[] strs = {"jack","tom","mary","milan"};
        AA aa = new AA();
        int index = aa.find("huang",strs);
        System.out.println("查找的index=" + index);
    }
}

class AA{
    public int find(String findstr,String[] strs){
        for(int i = 0;i < strs.length;i++){
            if(findstr.equals(strs[i])){
                return i;
            }
        }
        return -1;
    }
}

//    3.编写类Book,定义方法updatePrice,实现更改某本书的价格
//      若价格大于150,则更改为150,若价格大于100,则更改为100,否则不变

public class Homework09{

    public static void main(String[] args) {

        Book book = new Book("西游记",120);
        book.info();
        book.updatePrice();
        book.info();
    }
}
class Book{
    String name;
    double price;
    public Book(String name,double price){
        this.name = name;
        this.price = price;
    }
    public void updatePrice(){
        if(this.price > 150){
            this.price = 150;
        }else if(this.price > 100){
            this.price = 100;
        }
    }
    public void info(){
        System.out.println("书名=" + this.name + "价格=" + this.price);
    }
}

//    4.定义一个圆类Circle,定义属性:半径,提供显示周长、面积的方法

public class Homework09{

    public static void main(String[] args) {

        Circle circle = new Circle(4);
        System.out.println("面积=" + circle.area());
        System.out.println("周长=" + circle.meter());
    }
}

class Circle{
    double radius;
    public Circle(double radius){
        this.radius = radius;
    }
    public double area(){
        return Math.PI * radius * radius;
    }
    public double meter(){
        return Math.PI * 2 * radius;

    }
}

//    5.创建一个Employee类,属性为(姓名、年龄、性别、薪水、职位),提供3个构造器方法
//      初始化(职位、薪水)(性别、年龄、名字)(姓名、年龄、性别、薪水、职位)充分复用构造器

public class Homework09{

    public static void main(String[] args) {

    }
}

class Employee{
    String name;
    int age;
    char gender;
    double sal;
    String job;

    public Employee(double sal,String job){
        this.sal = sal;
        this.job = job;
    }
    public Employee(char gender,int age,String name){
        this.gender = gender;
        this.age = age;
        this.name = name;
    }
    public Employee(double sal,String job,char gender,int age,String name){
        this(gender,age,name);
        this.sal = sal;
        this.job = job;
    }
}

/*    6.将对象作为参数传递给方法
    要求
    1.定义一个Circle类,包含一个double型的radius属性,代表圆的半径,findArea()方法返回圆面积
    2.定义一个类PassObject,在类中定义一个方法printAreas(),该方法定义如下:
      public void printAreas(Circle c,int times)
    3.在printAreas方法中打印出1到times之间的每个整数半径值及对应面积
    4.在main()调用printAreas()方法,调用完毕后输出当前半径值
*/

public class Homework09{

    public static void main(String[] args) {

        Circle c = new Circle();
        PassObject p = new PassObject();
        p.printAreas(c,5);
    }
}

class Circle{
    double radius;
    // public Circle(){//无参构造器

    // }
    // public Circle(radius){
    //     this.radius = radius;
    // }
    public double findArea(){
        return Math.PI * radius * radius;
    }
    public void setRadius(double radius){
        this.radius = radius;
    }

}

class PassObject{
    public void printAreas(Circle c,int times){
        System.out.println("radius\tarea");
        for(int i = 1;i <= times;i++){
            c.setRadius(i);    //添加方法setRadius,修改对象半径值
            System.out.println(i + "\t" + c.findArea());
        }
    }
}

//    设计一个可以和电脑猜拳的游戏,0,1,2分别表示石头,剪刀,布,并显示玩家的输赢次数
import java.util.Random;
import java.util.Scanner;
public class Homework09{
    public static void main(String[] args) {

        Player p = new Player();        //创建对象
        int isWincount = 0;                //记录输赢次数

        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++){
            //获取玩家出拳
            System.out.println("请输入你要出的拳(0-石头,1-剪刀,2-布)");
            int num = scanner.nextInt();
            p.setPlayerGuessNum(num);
            int playerGuess = p.getPlayerGuessNum();
            arr1[i][j+1] = playerGuess;

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

            //将玩家与电脑出拳情况做比较
            String isWin = p.vsComputer();
            arr2[i] = isWin;
            arr1[i][j] = p.count;

            //对每一局情况进行输出
            System.out.println("===========================================");
            System.out.println("局数\t玩家出拳\t电脑出拳\t输赢情况");
            System.out.println(p.count + "\t" + playerGuess + "\t\t" + comGuess + "\t\t" + p.vsComputer());
            System.out.println("===========================================");
            System.out.println("\n\n");
            isWincount = p.WinCount(isWin);
        }
        //对最终结果进行输出
        System.out.println("局数\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.print("\n");
        }
        System.out.println("你赢了" + isWincount + "次");

    }
}

class Player{
    int playerGuessNum;    //玩家出拳类型
    int comGuessNum;    //电脑出拳类型
    int winCountNum;    //玩家赢的次数
    int count = 1;        //比赛次数

    public void showInfo(){

    }
    //电脑随机生成数字方法
    public int computerNum(){
        Random r = new Random();
        comGuessNum = r.nextInt(3);    //返回0-2随机数
        return comGuessNum;
    }
    //玩家生成数字方法
    public void setPlayerGuessNum(int playerGuessNum){
        if(playerGuessNum > 2 || playerGuessNum < 0){
            throw new IllegalArgumentException("数字输入错误");//抛出异常
        }
        this.playerGuessNum = playerGuessNum;
    }

    public int getPlayerGuessNum(){
        return playerGuessNum;
    }
    //比较猜拳结果
    public String vsComputer(){
        if(playerGuessNum == 0 && comGuessNum == 1){
            return "你赢了";
        }else if(playerGuessNum == 1 && comGuessNum == 2){
            return "你赢了";
        }else if(playerGuessNum == 2 && comGuessNum == 0){
            return "你赢了";
        }else if(playerGuessNum == comGuessNum){
            return "打平了";
        }else{
            return "你输了";
        }
    }
    //记录赢的次数
    public int WinCount(String s){
        count++;
        if(s.equals("你赢了")){
            winCountNum++;
        }
        return winCountNum;
    }
}
 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nzmzmc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值