「java分配货物」应该如何分配载货
今天给各位分享java分配货物的知识,其中也会对应该如何分配载货进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、新手初学java问题?
- 2、用java编写一个新的类,该类主要用来处理商店中出售货物的信息
- 3、求助:java中怎么实现按比例随机分配
- 4、Java 作业如下 课外作业: ①编写一个新的类,该类主要用来处理商店中出售货物的信息,比
- 5、想用java自动生成货品编号的代码,生成规则如下:
- 6、java 程序一共数量的 东西 平均分给几个人 怎么分
新手初学java问题?
先说你开发中的错误:
1.类名:这是习惯问题,约定俗成的是首字母大写:请把goods改成Goods,
请看实例化过程:
goods goods = new goods();//这样的容易混淆
Goods goods = new Goods();//这样的才符合规范
2、构造方法,请尽量在前面加上public修饰符。
public Goods(String name,String time){... ...}
3、stus[i]=new goods(words[0],words[i]);
words 的取值是 words=userInput.split(",");
是用户输入的一传字符串分割来的,你能保证用户属于的一定带","符号;
如果用户输入的不带这个符号,那你words的长度只能是1,
退一步说:就算用户输入的符合规范,
我们假定words.length=2
你循环的是stus的长度,我们假定 stus的长度为3,
前两轮循环不会报错,
第三轮循环,
i=2
这时候wrods[i]就是 words[2];
words的长度只有2,下标0和1才有值,2肯定报错,下标越界。
你的编译器肯定报错
Java.lang.ArrayIndexOutOfBoundsException
下次记得把错误发出来。
用java编写一个新的类,该类主要用来处理商店中出售货物的信息
public Goods{
public String name;//名称
public double price;//价格
public int count;//数量
public Goods(String name,double price,int count){
this.name=name;
this.price=price;
this.count= count;
}
public void show(){
System.out.print("名称"+this.name+"价格"+this.price+"数量"+this.count);
}
}
public Test{
public static void main(String[] args){
Goods g = new Goods("苹果",100.0,1);
g.show();
}
}
希望能帮到你 谢谢
求助:java中怎么实现按比例随机分配
比如有十个糖果,按照2:3:5的比例分配给三个小孩
public class luck {
public static ListString candy = new ArrayListString();
public static ListString child1 = new ArrayListString();
public static ListString child2 = new ArrayListString();
public static ListString child3 = new ArrayListString();
// 2:3:5
public static void main(String[] args) {
candy.add("糖果1");
candy.add("糖果2");
candy.add("糖果3");
candy.add("糖果4");
candy.add("糖果5");
candy.add("糖果6");
candy.add("糖果7");
candy.add("糖果8");
candy.add("糖果9");
candy.add("糖果10");
int count = 10;
for (int i = 0; i 2; i++) {
int math = (int) (Math.random() * count);
child1.add(candy.get(math));
candy.remove(math);
count--;
}
for (int i = 0; i 3; i++) {
int math = (int) (Math.random() * count);
child2.add(candy.get(math));
candy.remove(math);
count--;
}
for (int i = 0; i 5; i++) {
int math = (int) (Math.random() * count);
child3.add(candy.get(math));
candy.remove(math);
count--;
}
System.out.println("child1的糖:");
for (int i = 0; i child1.size(); i++) {
System.out.println(child1.get(i));
}
System.out.println("child2的糖:");
for (int i = 0; i child2.size(); i++) {
System.out.println(child2.get(i));
}
System.out.println("child3的糖:");
for (int i = 0; i child3.size(); i++) {
System.out.println(child3.get(i));
}
}
}
输出结果:
child1的糖:
糖果6
糖果1
child2的糖:
糖果7
糖果2
糖果8
child3的糖:
糖果4
糖果3
糖果10
糖果5
糖果9
Java 作业如下 课外作业: ①编写一个新的类,该类主要用来处理商店中出售货物的信息,比
public class Goods {
private int goodsId;
private String goodsName;
private double goodsPrice;
private int goodsNumber;
public Goods(){}
public Goods(int id,String name,double price,int number)
{
this.goodsId=id;
this.goodsName=name;
this.goodsPrice=price;
this.goodsNumber=number;
}
public int getGoodsId()
{
return this.goodsId;
}
public void setGoodsId(int id)
{
this.goodsId=id;
}
public String getGoodsName()
{
return this.goodsName;
}
public void setGoodsName(String name)
{
this.goodsName=name;
}
public double getGoodsPrice()
{
return this.goodsPrice;
}
public void setGoodsPrice(int price)
{
this.goodsPrice=price;
}
public int getGoodsNumber()
{
return this.goodsNumber;
}
public void setGoodsNumber(int number)
{
this.goodsNumber=number;
}
public String toString()
{
return "货物条码Id:"+this.goodsId+",货物名称:"+this.goodsName+",货物单价:"+this.goodsPrice+",货物数量:"+this.goodsNumber;
}
}
class goodsExt extends Goods
{
private double goodsSum;
public goodsExt(){}
public goodsExt(int id,String name,double price,int number)
{
super(id,name,price,number);
this.goodsSum=price*number;
}
public double getGoodsSum()
{
return getGoodsPrice()*getGoodsNumber();
}
}
想用java自动生成货品编号的代码,生成规则如下:
那就传分类的参数,去生成呗,,,,,从数据中确定下一个编号
~
~
~
java 程序一共数量的 东西 平均分给几个人 怎么分
看你要分到多精细了 如果余数继续分的话 遇到无限循环 递归能把你程序给你分崩了
java分配货物的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于应该如何分配载货、java分配货物的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。