时间不够 我得回宿舍 所有的验证部分都没做 代码可以运行 但前提是你得正确输入 否则不认
package com.Test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
//java程序题!!!求大神指教!谢了!
//有一个水果箱(Box),箱子里装有水果(Fruit),每一种水果都有不同的重量和颜色,
//水果有:苹果,梨,橘子。每个苹果(Apple)都有不同的重量和颜色,
//每个橘子(Orange)有不同的重量和颜色,每个梨(Pear)都有不同的重量和颜色,
//可以像水果箱(Box)里添加水果(addFruit),也可以取出水果(getFruit),
//还可以显示水果的重量和颜色,写出实现这些方法的代码,要求实现上述功能!
public class HelpTest {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
Box b=new Box();
while(true)
{
System.out.println(" 请选择功能 ");
System.out.println("1 添加水果");
System.out.println("2 删除水果");
System.out.println("3 显示水果信息");
System.out.println("4 退出系统");
int a =Integer.parseInt(br.readLine());
System.out.println(a);
switch (a) {
case 1:
System.out.println("请输入水果名称");
String name=br.readLine();
System.out.println("请输入水果颜色");
String color=br.readLine();
System.out.println("请输入水果重量");
float weight=Float.parseFloat(br.readLine());
Furit f=new Furit(name, color, weight);
b.AddFurit(f);
break;
case 2:
System.out.println("请输入水果名称");
String name1=br.readLine();
b.DelFurit(name1);
break;
case 3:
System.out.println("请输入水果名称");
String name2=br.readLine();
b.ShowInfo(name2);
break;
case 4:
System.exit(0);
break;
default:
break;
}
}
}
}
class Furit{
private String name;
private String color;
private float weight;
public Furit(String name,String color,float weight){
this.name=name;
this.color=color;
this.weight=weight;
}
public Furit(String name)
{
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
class Box{
ArrayList
//添加水果的方法
public void AddFurit(Furit f){
al.add(f);
}
//删除水果的方法
public void DelFurit(String name)
{
for(int i=0;i
{
if(al.get(i).getName().equals(name))
{
al.remove(i);
}
}
}
//显示水果的方法
public void ShowInfo(String name ){
for(int i=0;i
Furit f=(Furit)al.get(i);
if(name.equals(f.getName()))
{
System.out.println(al.get(i).getName()+"的颜色是"+al.get(i).getColor()+" 重量是"+al.get(i).getWeight());
}
}
}
}
我给你写了这么多你采纳他的答案? 呵呵
import java.util.ArrayList;
import java.util.List;
class Fruit{
private String name;
private String color;
private String weight;
public Fruit(){
}
public Fruit(String name, String color, String weight) {
this.name = name;
this.color = color;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
class Box{
private Fruit f;
public Listflist;
public ListgetFlist() {
return flist;
}
public void setFlist(Listflist) {
this.flist = flist;
}
public Fruit getF() {
return f;
}
public void setF(Fruit f) {
this.f = f;
}
}
public class TestArray01 {
public static void main(String[] args){
Box box = new Box();
Fruit f1 = new Fruit("苹果","红","2.1g");
Fruit f2 = new Fruit("苹果","粉红","2.1g");
Fruit f3 = new Fruit("苹果","红","2.1g");
Listflist1 = new ArrayList (); //flist 为null
flist1.add(f1);
flist1.add(f2);
flist1.add(f3);
box.setFlist(flist1);
Listflist = box.getFlist();
if(flist!=null && flist.size()>0){
for(int i=0;iSystem.out.println(flist.get(i).getName()+","+flist.get(i).getColor()+","+flist.get(i).getWeight());
}
}
System.out.println("---------------------------------------------");
if(flist!=null && flist.size()>0){
for(int i=0;iFruit f= new Fruit("苹果","粉红","2.1g");
if(compareFruit(f,flist.get(i))){
System.out.println(flist.get(i).getName()+","+flist.get(i).getColor()+","+flist.get(i).getWeight());
}
}
}
}
public static boolean compareFruit(Fruit f1,Fruit f2){
if(f1!=null&&f2!=null){
if(f1.getName()==f2.getName()&&f1.getColor()==f2.getColor()&&f1.getWeight()==f2.getWeight()){
return true;
}
}
return false;
}
}
编一个水果(Fruit)类,包含字段:重量、颜色,苹果、梨、橘子这三个类继承自Fruit类,再编一个Box类,编写addFruit() getFruit()方法。
这些都属于基本知识,这些都不会,后面的内容就别想了。
这个题目很简单哪。
编一个水果(Fruit)类,包含字段:重量、颜色,苹果、梨、橘子这三个类继承自Fruit类,再编一个Box类,编写addFruit() getFruit()方法。
这些都属于基本知识,这些都不会,后面的内容就别想了。