简体   繁体   English

使用Java,面向对象和构造函数创建Creature App。

[英]Creating a Creature App with Java, Object oriented and Constructors.

newbie here at Java. Java的新手。 I'm working on a project to where I collect info from a user for two types of Creatures. 我正在开发一个项目,在该项目中我从用户那里收集了两种生物的信息。 The menu would look like: 1. Land based 2. Water based 3. Display Animal 4. Quit. 菜单看起来像:1.基于土地2.基于水域3.显示动物4.退出。

I'm wanting to collect the user information from the user and then display it back to them. 我想从用户那里收集用户信息,然后将其显示给他们。 I think I'm stuck on building the object constructors from the different object. 我认为我坚持从不同的对象构建对象构造函数。

Here's what I have so far: 这是我到目前为止的内容:

SuperClass: 超类:

package com.animal;

public class Creature {
private String size;
private String weight;

public Creature (String size, String weight){
    this.size = size;
    this.weight = weight;
}

public String getSize() {return size;}

public void setSize(String size) {this.size = size;}

public String getWeight() {return weight;   }

public void setWeight(String weight) {this.weight = weight; }

void displayCr(){
    System.out.println("***Creatures***");
    System.out.println("Size: " + size);
    System.out.println("Weight: " + weight);

This is my Land Subclass: 这是我的土地子类别:

package com.animal;

public class Land extends Creature {
private String landAnimal;

public String getLandAnimal() {return landAnimal;}
public void setLandAnimal(String landAnimal) {this.landAnimal = landAnimal;}

public Land(String size, String weight, String landAnimal) {
    super(size, weight);
    this.landAnimal = landAnimal;

This is my Water subclass: 这是我的Water子类:

package com.animal;

public class Water extends Creature {
private String fish;

public String getFish() {return fish;}
public void setFish(String fish) {this.fish = fish;}

public Water(String size, String weight, String fish) {
    super(size, weight);
    this.fish = fish;
}

Then this is my Main called Kingdom: 这就是我的主要王国:

package com.animal;

import java.util.ArrayList;
import java.util.Scanner;


public class Kingdom {

public static void main(String[] args) {
    ArrayList<Creature> user = new ArrayList<Creature>();
    Scanner input = new Scanner(System.in); 

    int selection = 0; 
    while(selection != 4){
        System.out.println("****Main Menu****");
        System.out.println("Enter 1 for Land Animal");
        System.out.println("Enter 2 for Water Animal");
        System.out.println("Enter 3 to Display Animal");
        System.out.println("Enter 4 to quit");

        selection = input.nextInt();
        if(selection==1 || selection==2){

            Creature userInfo = null;

            System.out.println("Enter Size ");
            String size = input.next();
            System.out.println("Enter Weight: ");
            String weight = input.next();
            }
        if(selection == 1){
            System.out.println("Enter Land animal type: ");
            String landAnimal = input.next();
            //userInfo = new Land(size, weight, landAnimal);
            //user.add(userInfo);
        }
        else if(selection == 2){
            System.out.println("Enter Water animal type: ");
            String fish = input.next();
            //userInfo = new Water(size, weight, fish);

        }
        //creature.add(userInfo);
        //System.out.println(user.displayCr());

    }

I feel like I'm on the right path, but the last steps just aren't clicking for me and I've been grinding on this, reading, videos and nothing is clicking. 我觉得自己走在正确的道路上,但是最后一步对我来说并不是单击,而我一直在努力,阅读,观看视频,没有任何点击。

Also, I apologize if I've made the newbie mistakes in this post. 另外,如果我在这篇文章中犯了新手错误,我深表歉意。 I will accept all criticism, suggestions and help as a positive lesson. 我将接受所有批评,建议和帮助,这是一个积极的教训。 Thanks. 谢谢。

Overall your code is fairly right. 总体而言,您的代码是正确的。

Except: Creature userInfo = null; 除了: Creature userInfo = null; you are defining it inside the first IF, its scope will be limited to that IF. 您在第一个IF中定义它,则其范围将限于该IF。 As soon as you leave that IF it ceases to exist, hence you can't use it in the following IFs. 离开该IF后,它便不复存在,因此您不能在以下IF中使用它。

Consider the following scope change: 请考虑以下范围更改:

if(selection==1 || selection==2){ // main IF
    Creature userInfo = null;
    ...
    if(selection == 1){
        ...
    }
    else {
        ...
    }
    System.out.println(userInfo.displayCr());
} // end of main IF

First of all expand the scope of size,weight and userInfo means keep it inside main but outside the conditions as it is being used everywhere. 首先扩大size,weight和userInfo的范围,这意味着将其保留在main范围之内,而将其保留在所有条件之外的条件下。 Something like this. 这样的事情。

    int selection = 0;
    String size = null;
    String weight = null;
    Creature userInfo = null;

Instead of this creature.add(userInfo); 代替此creature.add(userInfo); should be 应该

user.add(userInfo);

and call display like this 然后像这样调用显示

userInfo.displayCr(); because the return type of the method is void so cannot be used inside sysout and it should be inside that else if(){} 因为该方法的返回类型为void,所以不能在sysout内部使用,并且应该在else if(){}内部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM