【test】Java一些比较简单的题目总结,以及答案

在这里插入图片描述
在这里插入图片描述

第一题:
public class demo {
    public static void main(String[] args) {
        A01 a = new A01();
        double arr[] = {1.4, 3.4, 1.9};
        Double res = a.max(arr);
        if (res != 0) {
            System.out.println(res);
        } else {
            System.out.println("error");
        }
    }
}

class A01 {
    public Double max(double[] arr) {
        if (arr != null && arr.length > 0) {
            double max = arr[0];//假定第一元素就是最大的值
            for (int i = 0; i < arr.length; i++) {
                if (max < arr[i]) {
                    max = arr[i];
                }
            }
            return max;
        } else {
            return null;
        }
    }
}
第二题:
public class demo {
    public static void main(String[] args) {
        String arr[] = {};
        A02 b = new A02();
        Scanner str = new Scanner(System.in);
        System.out.println("请输入名称:");
        String findStr = str.next();
        int index = b.find(findStr, arr);
        System.out.println("查找的index为 " + index);
    }
}

class A02 {
    public int find(String str, String[] arr) {
        if (arr != null && arr.length > 0) {
            for (int i = 0; i < arr.length; i++) {
                if (str.equals(arr[i])) {
                    return i;
                }
            }
            return -1;
        } else {
            return -2;
        }
    }
}
第三题:
public class demo {
    public static void main(String[] args) {
        Book book = new Book("倚天屠龙记", 151);
        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 (price > 150) {
            price = 150;
        } else if (price > 100) {
            price = 100;
        }
    }

    //显示书籍情况
    public void info() {
        System.out.println("书名= " + name + " 价格= " + price);
    }
}
第四题:
public class demo {
    public static void main(String[] args) {
        A04 d = new A04();
        int oldArr[] = {12, 43, 21, 32, 1};
        int[] newArr = d.copyArr(oldArr);

        //输出
        System.out.println("返回的新数组:");
        for (int i = 0; i < newArr.length; i++) {
            System.out.print(newArr[i] + " ");
        }
    }
}

class A04 {
    public int[] copyArr(int oldArr[]) {
        //new一个新数组,长度为旧数组的长度
        int newArr[] = new int[oldArr.length];
        //遍历旧数组中的元素,复制在新数组上
        for (int i = 0; i < oldArr.length; i++) {
            newArr[i] = oldArr[i];
        }
        //返回新数组
        return newArr;
    }
}
第五题:
public class demo {
    public static void main(String[] args) {
        Circle circle = new Circle(4);
        //面积
        System.out.println("面积为 = " + circle.acreage());
        //周长
        System.out.println("周长为 = " + circle.perimeter());
    }
}

class Circle {
    //半径
    double r;

    //有参构造器
    public Circle(double r) {
        this.r = r;
    }

    //周长
    public double perimeter() {
        return Math.PI * r;
    }

    //面积
    public double acreage() {
        return 2 * Math.PI * r * r;
    }
}
第六题:
public class demo {
    public static void main(String[] args) {
        Cale cale = new Cale(9, 5);
        System.out.println("加为 " + cale.plus());
        System.out.println("减为 " + cale.minus());
        System.out.println("乘为 " + cale.ride());
        Double trade = cale.trade();
        if (trade != 0) {
            System.out.println("除为 " + trade);
        }
    }
}

class Cale {
    double num1;
    double num2;

    //有参构造器
    public Cale(double num1, double num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    //加
    public double plus() {
        return num1 + num2;
    }

    //减
    public double minus() {
        return num1 - num2;
    }

    //乘
    public double ride() {
        return num1 * num2;
    }

    //除
    public Double trade() {
        if (num2 == 0) {
            System.out.println("分母不能为零");
            return null;
        } else {
            return num1 / num2;
        }
    }
}
第七题:
public class demo {
    public static void main(String[] args) {
        Dog dog = new Dog("小双", 3, "彩色");
        dog.show();
    }
}

class Dog {
    String name;
    int age;
    String color;

    public Dog() {

    }

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public void show() {
        System.out.println("小狗名为 " + name + " 年龄为 " + age + " 颜色为 "+color);
    }
}
第八题:
输出的是  10  9  10
第九题:
public class demo {
    public static void main(String[] args) {
        Music music = new Music("《小幸运》", 70);
        System.out.println("播放情况如下: ");
        music.play();
        System.out.println(music.getMusic());
    }
}

class Music {
    String name;
    int times;

    public Music(String name, int times) {
        this.name = name;
        this.times = times;
    }

    public void play() {
        System.out.println("音乐 " + name + " 正在播放…… 时长为 " + times + " 秒");
    }

    public String getMusic() {
        return "音乐" + name + "播放时间为" + times;
    }
}
第十题:
输出为  101 100 101 101
第十一题:
//定义形式为
public double method(double d1,double d2)
第十二题:
public class demo {
    public static void main(String[] args) {
        Employee employee = new Employee("大哥", "男", 18, "耀嘉集团CEO", 100000000000.0);
        employee.show();
    }
}

class Employee {
    String name;
    String sex;
    int age;
    String post;
    double money;

    public Employee() {

    }

    public Employee(String name, String sex, int age, String post, double money) {
        //构造器复用
        this(name, sex, age);
        this.post = post;
        this.money = money;
    }

    public Employee(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public Employee(String post, double money) {
        this.post = post;
        this.money = money;
    }

    public void show() {
        System.out.println("\n信息如下: ");
        System.out.println("姓名为: " + name + " 性别为: " + sex + " 年龄为: " + age + " 职位是 " + post + " 薪水为: " + money);
    }
}
第十三题:
public class demo {
    public static void main(String[] args) {
        Circle c = new Circle();
        PassObject p = new PassObject();
        p.printAreas(c, 10);
    }
}

class Circle {
    double r;

    public Circle() {

    }

    public Circle(double r) {
        this.r = r;
    }

    //圆的面积
    public double findArea() {
        return Math.PI * r * r;
    }

    //添加方法,修改对象的半径值
    public void setR(double r) {
        this.r = r;
    }
}

class PassObject {
    public void printAreas(Circle c, int times) {
        System.out.println("半径\t面积");
        for (int i = 1; i <= times; i++) {
            c.setR(i);
            System.out.println((double) i + "\t" + c.findArea());
        }
    }
}
第十四题:
//主类
public class demo {
    public static void main(String[] args) {
        //创建一个玩家对象
        Tom t = new Tom();

        //用来记录最后的输赢
        int isWinCount = 0;

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

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

        Scanner myScanner = new Scanner(System.in);

        //遍历比赛次数 比赛三次
        for (int i = 0; i < 3; i++) {
            //获取玩家出的拳
            System.out.println("请输入你要出的拳: 0 拳头 , 1 剪刀 , 2 布");
            int num = myScanner.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");

            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 + "次");
    }
}

//方法类
class Tom {
    // 玩家出拳类型
    int tomGuessNum; // 0 拳头 , 1 剪刀 , 2 布
    // 电脑出拳类型
    int comGuessNum; // 0 拳头 , 1 剪刀 , 2 布
    // 玩家赢的次数
    int winCountNum;
    //比赛的次数
    int count = 1; // 一共三局


    //电脑随机生成数字的方法
    public int computerNum() {
        Random r = new Random();
        comGuessNum = r.nextInt(3);// 3 为下标值,即返回 0-2 的随机数
        return comGuessNum;
    }


    //设置玩家猜拳的数字的限制方法
    public void setTomGuessNum(int tomGuessNum) {

        if (tomGuessNum > 2 || tomGuessNum < 0) {
            //抛出一个异常
            throw new IllegalArgumentException("数字输入有误,请检查!");
        }
        this.tomGuessNum = tomGuessNum;
    }

    //
    public int getTomGuessNum() {
        return tomGuessNum;
    }

    // 猜拳结果
    // 玩家赢返回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 "你输了";
        }
    }

    //记录玩家的输赢情况
    public int winCount(String s) {
        //控制玩的次数
        count++;
        //统计玩的次数
        if (s.equals("你赢了")) {
            winCountNum++;
        }
        return winCountNum;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

꧁小ۣۖิ鸽ۣۖิ子ۣۖิ꧂

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

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

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

打赏作者

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

抵扣说明:

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

余额充值