栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java冒泡排序和选择呢排序

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java冒泡排序和选择呢排序

package com.hp.fan;

public class F4 {
    public static void main(String[] args) {
        int[]arr={4,5,6,2,1,8,};

        for (int i=0;iarr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }

for (int i=0;i
package com.hp.fan;

public class F5 {
    
    public static void main(String[] args) {
        int arr[]={4,5,2,8,9,};
        
        //i : 0-length-1
        for (int i=0;iarr[j]){
                    int temp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=temp;
                }
            }
           // System.out.println();
       }
        //遍历
        for (int i=0;i
7月8日  2:1.玩家类pk
    增加暴击
    增加物品掉落(击杀敌人后随机掉落物品) 实体类
package comm.hp.yun.demo2;

import java.util.Random;

public class Player {

        public String name;
        public String type;
        public int life;
        public int defense;
        public int attack;

        
        Random a = new Random();
        Random b = new Random();
        Random c = new Random();
        public void say(){
            System.out.println("我是"+name+",是一个"+type);
            System.out.println("生命值"+life+"防御值"+defense+"攻击力"+attack);
        }

        public void pk(Player p){
            int flag = 0;
            //回合制,直至一方死亡
            while (true) {
                this.say();
                p.say();

                if (flag == 0) {
                    int harm = this.attack - p.defense;
                    int t = b.nextInt(2);
                    if (t!=0){
                        System.out.println(p.name + "掉血" + harm);
                        p.life -= harm;
                    }else {
                        System.out.println("暴击");
                        System.out.println(p.name + "掉血" + harm*2);
                        p.life -= harm*2;
                    }
                    flag = 1;
                } else {
                    int harm = p.attack - this.defense;
                    int l = c.nextInt(2);
                    if (l!=0){
                        System.out.println(this.name + "掉血" + harm);
                        this.life -= harm;
                    }else {
                        System.out.println("暴击");
                        System.out.println(this.name + "掉血" + harm*2);
                        this.life -= harm*2;
                    }
                    flag = 0;
                }
                //有血量<=0,战斗结束
                if(this.life<=0){
                    System.out.println(p.name+"打败了"+this.name);
                    break;
                }
                if(p.life<=0){
                    System.out.println(this.name+"打败了"+p.name);
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


            String [] stu = {"奖励100经济","破军","名刀司命","碎星锤","美女一个"," "};
            for (int z = 0; z < stu.length; z++) {
                z = a.nextInt(6);
                System.out.println("恭喜你获得了胜利"+stu[z]);
                break;
            }
        }
        //构造器

        public Player() {
        }
        public Player(String name, String type, int life, int defense, int attack) {
            this.name = name;
            this.type = type;
            this.life = life;
            this.defense = defense;
            this.attack = attack;
        }

        //set,get方法
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public int getLife() {
            return life;
        }

        public void setLife(int life) {
            this.life = life;
        }

        public int getDefense() {
            return defense;
        }

        public void setDefense(int defense) {
            this.defense = defense;
        }

        public int getAttack() {
            return attack;
        }

        public void setAttack(int attack) {
            this.attack = attack;
        }
    }

测试类
package comm.hp.yun.demo2;

public class PlayerTest {
        public static void main(String[] args) {
            //选英雄阶段
            Player p1 = new Player("韩信", "刺客", 1000, 120, 300);
            Player p2 = new Player("瑶", "辅助", 2200, 330, 280);

            //开始决斗
            p1.pk(p2);
        }
    }

3.柜台商品管理系统 实体类:Goods
package comm.hp.yun.demo3;

public class Goods {
    //属性
    private int id;//商品号
    private String goodsName;//商品名称
    private int price;//商品价格
    private String desc;//商品描述
//封装get方法
    public int getId() {
        return id;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public int getPrice() {
        return price;
    }

    public String getDesc() {
        return desc;
    }
//封装set方法
    public void setId(int id) {
        this.id = id;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
//无参构造器
    public Goods(){

    }
//重写toString方法
    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", goodsName='" + goodsName + ''' +
                ", price=" + price +
                ", desc='" + desc + ''' +
                '}';
    }
//有参构造器	    有参构造器初始化初始化商品对象
    public Goods(int id, String goodsName, int price, String desc) {
        this.id = id;
        this.goodsName = goodsName;
        this.price = price;
        this.desc = desc;
    }
}
柜台类:Counter
package comm.hp.yun.demo3;

public class Cunter {
    //属性:
    //柜台商品列表,固定10个商品位置
    private Goods[] fan=new Goods[10];
    //柜台商品数量
    private int num;

//构造器:	    无参构造器初始化2个商品
    public Cunter(){
        this.fan[0]=new Goods(1,"巧克力",25,"美味可口,恋爱必备!");
        this.fan[1]=new Goods(2,"卫龙辣条",2,"隔壁小孩馋哭了!");
        this.fan[2]=new Goods(3,"红茶",30,"冰爽可口");
        this.fan[3]=new Goods(4,"香蕉",10,"香甜可口");
        num=4;//相当于四个商品

    }
    //展示柜台所有的商品(不能输出null)
    public void show(){
        for (Goods yun:fan){
            if (yun !=null){
                System.out.println(yun);
            }

        }
    }
}
测试类:CounterTest
package comm.hp.yun.demo3;

public class CounterTest {
    public static void main(String[] args) {
        //创建柜台对象
        Cunter fan=new Cunter();
        //调用show()方法展示柜台商品
        fan.show();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1007119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号