有一个水果箱(box),箱子里装有水果(fruit)。每一种水果都有不同的重量(weight)和颜色(color),水果有:苹果(apple),梨(pear)。可以向水果箱(box)里添加水果(ad

有一个水果箱(box),箱子里装有水果(fruit)。每一种水果都有不同的重量(weight)和颜色(color),水果有:苹果(apple),梨(pear)。可以向水果箱(box)里添加水果(addFruit),也可以取出水果(getFruit)。请编写java代码实现上述功能。](这里写自定义目录标题)

我建立了一个类来实现该问题(java8)。

import java.util.*;

public class Box{
	static Scanner input=new Scanner(System.in);
	 String color;
     static int a1,a2,b1, b2,c1,c2,d1,d2;
     static int number;
	 static double boxweight;
	 static double a4,b4,c4;//分别指苹果,梨子,橘子的单个重量。
	public Box(String color,double weight) {
		this.color=color;
	}
	
	public static int addfruit() {
		System.out.println("您想要往箱子里分别添加多少的苹果,梨子,橘子?请依次输入你想要添加的个数");
		 a1=input.nextInt();
		 b1=input.nextInt();
		 c1=input.nextInt();
		System.out.println("您此次向水果箱里添加了"+a1+"个苹果,"+b1+"个梨子,"+c1+"个橘子.");
		d1=a1+b1+c1;
		return d1;
	}
	
	public static int getfruit(){
	System.out.println("您想要往箱子里分别取出多少的苹果,梨子,橘子?请依次输入你想要添加的个数。");
	 a2=input.nextInt();
	 b2=input.nextInt();
	 c2=input.nextInt();
	System.out.println("您此次向水果箱里取出了"+a2+"个苹果,"+b2+"个梨子,"+c2+"个橘子。");
	d2=a2+b2+c2;
	return d2;
        }
		
    public static void display(Box a,Box b,Box c)
    {
    	int a3=a1-a2;int b3= b1-b2;int c3=c1-c2;
    	number=d1-d2;
    	boxweight=a3*a4+b3*b4+c3*c4;
    	System.out.println("现在水果箱中的总水果个数为"+Box.number+",各个水果的总重量为"+Box.boxweight);
    	System.out.println("其中苹果现在有"+a3+"个,梨子有"+b3+"个,"+"橘子有"+c3+"个。");
    	System.out.println("其中苹果现在有"+a3*a4+"两"+ ",梨子有"+b3*b4+"两"+ " ,"+"橘子有"+c3*c4+ "两。");
    }
	public static void main(String args[]) {
	
		//设置3种水果的重量。
		System.out.println("请分别设置单个苹果、梨子、橘子的重量。");
		a4=input.nextDouble();
		b4=input.nextDouble();
		c4=input.nextDouble();
		System.out.println("您已设置一个苹果的重量为"+a4+"两,一个梨子的重量为:"+b4+"两,一个橘子的重量为:"+c4+"两。");
		
		Box apple=new Box("红",a4);
		Box pear=new Box("黄",b4);
		Box orange=new Box("绿",c4);
		
		System.out.println("苹果的颜色:"+apple.color+",   梨子的颜色: "+pear.color+",    橘子的颜色:"+orange.color );
		
		System.out.println("水果箱中的水果在开始的时候有苹果有0个,梨子有0个,橘子有0个。");
		Box.addfruit();
		Box.display(apple, pear, orange);
		Box.getfruit();
		Box.display(apple, pear, orange);
		
	}
}

在这里插入图片描述

喜欢的可以点个赞哦。
DF,Tarim。

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
我可以为您提供相关的Java代码: 首先,我们需要创建水果的类(Fruit): public class Fruit { private String name; private double weight; private String color; public Fruit(String name, double weight, String color) { this.name = name; this.weight = weight; this.color = color; } public String getName() { return name; } public double getWeight() { return weight; } public String getColor() { return color; } } 然后,我们需要创建水果的类(Box): import java.util.ArrayList; public class Box { private ArrayList<Fruit> fruits = new ArrayList<>(); public void addFruit(Fruit fruit) { fruits.add(fruit); } public Fruit getFruit(int index) { return fruits.get(index); } public double getTotalWeight() { double totalWeight = 0; for (Fruit fruit : fruits) { totalWeight += fruit.getWeight(); } return totalWeight; } public void displayFruits() { for (Fruit fruit : fruits) { System.out.println("Name: " + fruit.getName() + ", Weight: " + fruit.getWeight() + ", Color: " + fruit.getColor()); } } } 最后,我们可以在主函数中进行测试: public static void main(String[] args) { Fruit apple1 = new Fruit("Apple", 0.2, "Red"); Fruit apple2 = new Fruit("Apple", 0.3, "Green"); Fruit orange1 = new Fruit("Orange", 0.4, "Orange"); Fruit orange2 = new Fruit("Orange", 0.35, "Yellow"); Fruit pear1 = new Fruit("Pear", 0.25, "Yellow"); Fruit pear2 = new Fruit("Pear", 0.3, "Green"); Box box = new Box(); box.addFruit(apple1); box.addFruit(apple2); box.addFruit(orange1); box.addFruit(orange2); box.addFruit(pear1); box.addFruit(pear2); System.out.println("Total weight of fruits in the box: " + box.getTotalWeight()); System.out.println("Fruits in the box:"); box.displayFruits(); System.out.println("Getting the third fruit in the box:"); Fruit fruit = box.getFruit(2); System.out.println("Name: " + fruit.getName() + ", Weight: " + fruit.getWeight() + ", Color: " + fruit.getColor()); } 这段代码就可以实现题目要求的功能:创建一个水果,向添加不同水果,取出其中一个水果,显示水果重量颜色,以及显示水果中所有水果的信息。其中,水果类还包括一个计算水果重量的方法(getTotalWeight)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值