本文主要是介绍javaaaa,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
User类创建
public class User {private String name;private int money;public User(){}public User(String name,int money){this.name=name;this.money=money;}public void show(){System.out.println("Name:"+name+" Money:"+money);}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}
}
Manage类
import java.util.ArrayList;public class Manager extends User{public Manager(String name, int money) {super(name, money);}public Manager() {super();}
//需要一个集合用来存储发红包的个数以及各个金额public ArrayList<Integer> List(int totalmoney,int count){//发的钱ArrayList<Integer> list = new ArrayList<>();int leftmoney = super.getMoney();//群主总共的钱if(leftmoney < totalmoney) return list;//群主没那么多钱int averagemoney = totalmoney/count;//平均每个人int modemoney = totalmoney%count;//除不尽的余数加在list最后一个for(int i = 0 ; i < count-1 ; i++){list.add(averagemoney);}super.setMoney(leftmoney - totalmoney);//群主剩的钱list.add(modemoney+averagemoney);return list;//返回红包集合}
}
Member类
import java.util.ArrayList;
import java.util.Random;public class Member extends User{public Member() {super();}public Member(String name, int money) {super(name, money);}public void receive(ArrayList<Integer> list){int random = new Random().nextInt(list.size());//随即索引int num = list.remove(random);//将这个数去除,并且取出int money = super.getMoney()+num;//把钱加上super.setMoney(money);//更新前的金额}
}
调用
import java.util.ArrayList;
import java.util.Scanner;public class Using {public static void main(String[] args) {Member A= new Member("贤哥",0);Member B = new Member("超哥",0);Member C = new Member("小哥",0);Manager qun = new Manager("群主",10000);System.out.println("原来的钱");qun.show();A.show();B.show();C.show();System.out.println("================");System.out.println("你想发多少");int F = new Scanner(System.in).nextInt();ArrayList<Integer> list = qun.List(F,3);A.receive(list);B.receive(list);C.receive(list);qun.show();A.show();B.show();C.show();}
}
运行结果:
这篇关于javaaaa的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!