【问题标题】:How do I correctly implement this code segment without getting a logic error? [closed]如何正确实现此代码段而不会出现逻辑错误? [关闭]
【发布时间】:2017-10-09 05:52:17
【问题描述】:

我正在制作一个程序,允许用户根据鞋子的尺码、用途等从预先制作的鞋子菜单中选择鞋子。我首先创建了一个名为 Shoe 的抽象类来表示基本鞋子.然后我创建了一堆具有各种名称的子类,例如 Nke、Adidas 等。这些子类可以实例化它们类型的特定鞋子,例如 Adidas 可以制作一款名为 Adidas Purefly 的鞋子。然后我创建了一个名为 Shoebox 的类,每次实例化它的对象时,它都会创建一个包含所有不同鞋子的菜单。现在我想编写一个程序,允许我在传入名称后选择鞋子,但我有一个问题,我将在列出所有类之后讨论。

这是 Shoe 父类-

public abstract class Shoe {

  private String name;
  private int size;
  private String color;
  private String purpose;
  private double price;

  public Shoe (String called, int numSize, String sColor, String Apurpose, double cost ) {
    name=called;
    size=numSize;
    color=sColor;
    purpose=Apurpose;
    price=cost;
  }

  public String getName() {
    return name;
  }

  public int getSize() {
    return size;
  }

  public double getPrice() {
    return price;
  }

  public String getPurpose() {
    return purpose;
  }

  public String getColor() {
    return color;
  }
}

这是 Nike 类,一个扩展 Shoe 的子类的示例-

public class Nike extends Shoe {

  private String name;
  private int size;
  private String color;
  private String purpose;
  private double price;
  private boolean maxGel;

  public Nike(String called, int measurements, String sColor, String purpose, double cost, boolean hasGel) {
    super(called, measurements, sColor, purpose, cost);
    maxGel=hasGel;
  }

  public boolean hasGel() {
    if(maxGel)
        return true;
    else
        return false;
  }
}

这是 Shoebox 类,以及一个访问器方法,它使用 ArrayList 列出所有鞋子-

import java.util.ArrayList;

public class Shoebox {

    ArrayList <Shoe> box = new ArrayList<Shoe>();

    public Shoebox() {

    Shoe roshe = new Nike("roshe", new int[]{4,5,6,7,8,9,10,11,12}, "black", "fashion", 129.0, false);
    Shoe trainers= new Nike("trainers",new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14},"blue",  "running", 115.5, true);
    Shoe airMax= new Nike("air max", new int[]{8,9,10,11,12,13}, "red", "running",149.9, true);
    Shoe huarache= new Nike("huarache", new int[]{9,10,11,12}, "green", "fashion",139.9, false);
    Shoe stefan= new Nike("stefan", new int[]{3,4,9,10,11,12,15}, "yellow", "tennis",79.9,true);        
    Shoe kd= new Nike("kd", new int[]{5,6,9,10,11,12,14}, "red", "basketball", 145.9, true);
    Shoe lebron= new Nike("lebron", new int[]{6,7,8,9,10}, "gold", "basketball",156.9, true);
    Shoe kyrie= new Nike("kyrie", new int[]{6,7,8,9,10}, "gold", "basketball",156.9, true);
    box.add(roshe);
    box.add(trainers);
    box.add(airMax);
    box.add(huarache);
    box.add(stefan);
    box.add(kd);
    box.add(lebron);
    box.add(kyrie);         //AT this point the length of the box array is 8.



    Shoe harden= new Adidas("harden",new int[]{7,9,10,11,12,13,14}, "red", "basketball", 109.9,  true);
    Shoe lillard= new Adidas("lillard",new int[]{7,9,10,11,12,13,14}, "green", "basketball", 119.9,  true);
    Shoe rose= new Adidas("rose", new int[]{8,9,10},"red", "injury recovery", 145.9, true);
    Shoe nmd= new Adidas("nmd", new int[]{8,10,11,14}, "purple", "fashion", 173.9, true);
    Shoe pharellNMD= new Adidas ("pharell nmd", new int[] {8,9,10,11}, "aqua","fashion", 134.9, true);
    Shoe stanSmith= new Adidas ("stan smith", new int[] {9,10,11,12}, "pink", "skating", 87.9, false);
    Shoe superstar= new Adidas ("superstar",new int[] {9,10,11,12}, "white", "fashion", 79.9, false);
    Shoe barricade= new Adidas ("barricade", new int[] {8,10,11,12,13}, "neon", "tennis", 98.9, false);
    Shoe cloud= new Adidas ("cloud", new int[] {11,12}, "white", "golf", 78.9, false);
    Shoe f7= new Adidas ("f7", new int[] {7,8,9,10,11,12,13,14}, "neon", "soccer", 149.9, false);
    box.add(harden);
    box.add(lillard);
    box.add(rose);
    box.add(nmd);
    box.add(pharellNMD);
    box.add(stanSmith);
    box.add(barricade);
    box.add(cloud);
    box.add(f7);


    Shoe suedeloaf= new Armani ("suede loafers", new int[] {9,10,11,12,13}, "black", "fashion", 300.0);
    Shoe colorway= new Armani ("colorway", new int[] {7,8,9,10,11,12,13}, "blue", "fashion", 500.0);
    Shoe purple= new Armani ("purple loafers", new int[] {9,10,12,13}, "black", "fashion", 300.0);
    Shoe transform= new Armani ("transform", new int[] {9,10,11,12,13}, "teal", "formal", 300.0);
    Shoe tang= new Armani ("tang", new int[] {9,10,13}, "pink", "fashion", 300.0);

    box.add(suedeloaf);
    box.add(colorway);
    box.add(purple);
    box.add(transform);
    box.add(tang);


    //At this point the length of the array is 21


    Shoe I = new Jordan ("I", new int[]{8,9,10,11,12,13}, "red", "finesse", 129.9);
    Shoe Iretro = new Jordan ("retro I", new int[] {9,10,11,12}, "red", "finesse", 199.9);
    Shoe II= new Jordan ("II", new int[] {10,11,12,13,14}, "blue", "basketball", 139.9);
    Shoe III= new Jordan ("III", new int[] {9,10,11,11}, "green", "basketball", 149.9);
    Shoe IV = new Jordan ("IV", new int[] {10,11,12,13}, "purple", "basketball", 159.9);
    Shoe VII = new Jordan ("VII", new int[] {10,11,12,13}, "yellow", "basketball", 189.9);
    Shoe VIII = new Jordan ("VIII", new int[] {10,11,12,13,14}, "blue", "fashion", 189.9);
    Shoe XI= new Jordan ("XI", new int[] {11,12,13}, "red", "basketball", 199.9);
    Shoe XII= new Jordan ("XII", new int[] {9,10,11,12}, "navy", "basketball", 239.9);

    box.add(I);
    box.add(Iretro);
    box.add(II);
    box.add(III);
    box.add(IV);
    box.add(VII);
    box.add(VIII);
    box.add(XI);
    box.add(XII);

    //The length of the box array at this point 30

    Shoe can= new Dockers ("cambridge", new int[] {9,10,11,12,13,14}, "navy", "formal", 64.9);
    Shoe bean= new Dockers ("bean", new int[] {10,11,12,13}, "green", "formal", 77.0);
    Shoe tang1= new Dockers ("tang", new int[] {12,13,14}, "orange", "fashion", 69.9);
    Shoe gold= new Dockers ("gold", new int[] {9,10,11,12,13,14}, "gold", "formal", 62.9);
    Shoe grizzly= new Dockers ("grizzly", new int[] {9,10,11}, "grey", "teaching", 89.9);

    box.add(can);
    box.add(bean);
    box.add(tang1);
    box.add(gold);
    box.add(grizzly);

    //The length of the box array at this point is 35

    Shoe AI= new Reebok("AI", new int[] {8,9,10,11}, "grey", "basketball", 140.0);
    Shoe shaq= new Reebok ("shaq", new int[] {14,15,16,17,18,19,20,21}, "grey", "basketball", 220.0);
    Shoe pumpGraphite= new Reebok("pump graphite", new int[] {7,8,9,10,11}, "orange", "running", 129.9);
    Shoe BB= new Reebok("BB", new int[] {8,9,10,11}, "red", "running", 138.0);
    Shoe leathWhite= new Reebok("leather whites", new int[] {8,9,10,11}, "white", "fashion", 170.0);
    Shoe dualPumpRunner= new Reebok("dual pump runner", new int[] {5,6,7,8,9,10,11,12}, "grey", "running", 130.0);
    Shoe kamikaze2= new Reebok("kamikaze 2", new int[] {8,9,10,11}, "black", "basketball", 140.0);
    Shoe shagnosis= new Reebok("shagnosis", new int[] {14,15,16,17,18,19,20,21}, "zebra", "basketball", 240.0);
    Shoe question= new Reebok("question", new int[] {8,9,10,11}, "grey", "basketball", 140.0);


    box.add(AI);
    box.add(shaq);
    box.add(pumpGraphite);
    box.add(BB);
    box.add(leathWhite);
    box.add(dualPumpRunner);
    box.add(kamikaze2);
    box.add(shagnosis);
    box.add(question);

    //The length of the box array at this point is 44
    }

    public ArrayList<Shoe> getShoes()
    {
        return this.box;
    }


}

最后,这是我迄今为止为 Runner 类 ShoeSelector 编写的代码(请记住,我还没有完成)-

import java.util.*;
public class ShoeSelector {

    public static void main(String[] args) {

        Scanner name= new Scanner(System.in);
        Scanner size= new Scanner(System.in);
        Scanner color= new Scanner(System.in);
        Scanner priceMax= new Scanner(System.in);
        Scanner priceMin= new Scanner(System.in);
        Scanner purpose= new Scanner(System.in);
        Scanner side = new Scanner(System.in);

        System.out.println("Do you know what shoe you want?     If you know the name, enter the name     If you don't, enter 0");
        String nom= name.nextLine();

        Shoebox yee= new Shoebox();  //Creates a new Shoebox array that contains all the shoes

        if (!(nom.equals(0))){  //Checks to see if you have entered a shoe name
        for (Shoe e: yee.getShoes()){
            if (e.getName().equalsIgnoreCase(nom)){
                System.out.println("Good News! We have the Shoe brand you are looking for. Here are the details of " +nom);
                System.out.println( "name:   " + e.getName() +"    color:   " + e.getColor()+"    purpose:   " +e.getPurpose()+"   cost:   " + e.getPrice());
                System.out.println("Do you want to search another model?    Y/N");
                String what= side.nextLine();

                if (what.equalsIgnoreCase("n")){

                    System.exit(0);
                }

            }
//          else {
//              System.out.println("We are very sorry, but we are not in possesseion of this model currently. But please return to check back.");
//              System.out.println("Do you want to search another model?    Y/N");
//              String what= side.nextLine();
//              
//              if (what.equalsIgnoreCase("y"))
//              {
//                  nom= name.nextLine();
//              }
//                  
//
//          }
        }
    }


    }
}

到目前为止,这个类应该读取用户提供的任何鞋子的名称,如果确实存在同名的鞋子,那么它应该返回关于鞋子的所有信息。如果没有这样的鞋子,那么它应该询问用户是否想要搜索另一只鞋子。

好的,问题是,如果您运行未注释部分,它确实会为您提供鞋子的详细信息(如果确实存在),但如果用户输入“,我无法弄清楚如何搜索另一只鞋子” y' 如果他/她想要再次搜索。

另外,如果我取消注释掉注释部分,当我运行程序时,它说即使有同名的鞋子也找不到鞋子。

有人可以向我解释为什么它不起作用以及如何解决它吗?

非常感谢你

附:请记住,我是这方面的初学者,所以我对语言不是很精通,所以如果你觉得我的问题很愚蠢,请不要不喜欢它。而是发表评论并告诉我我错在哪里。

【问题讨论】:

  • 错误信息是什么? “不起作用”是什么意思?
  • 您的代码中缺少“字符串”类型的引号。错误是什么?
  • 应该是new Nike("roshe", 11, "black", "fashion", 129.0, false)
  • 这一行Shoe roshe = new Nike(roshe, 11, black, fashion, 129.0, false) 需要构造函数中的字符串

标签: java class object inheritance polymorphism


【解决方案1】:
Shoe roshe = new Nike("roshe", 11, "black", "fashion", 129.0, false)

字符串字面量应该用双引号括起来。

至于你改变大小类型,传递int数组而不是int

int s[] = {11,12,13};
Shoe roshe = new Nike("roshe", s, "black", "fashion", 129.0, false)

or

Shoe roshe = new Nike("roshe",new int[]{11, 12, 13} , "black", "fashion", 129.0, false)

【讨论】:

  • @f1sh 编辑感谢
  • 非常感谢,伙计...现在可以使用了。
  • 很高兴为您提供帮助。如果它解决了您的问题,请接受我的回答。
  • 我更改了鞋子的程序
  • 代替this-public abstract class Shoe { private String name;私有 int 大小;私有字符串颜色;私有字符串用途;私人双倍价格; public Shoe (String called, int numSize, String sColor, String A purpose, double cost ) { name= called;大小=数字大小;颜色=s颜色;目的=目的;价格=成本; }
猜你喜欢
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
相关资源
最近更新 更多