[英]Creating a Creature App with Java, Object oriented and Constructors.
Java的新手。 我正在開發一個項目,在該項目中我從用戶那里收集了兩種生物的信息。 菜單看起來像:1.基於土地2.基於水域3.顯示動物4.退出。
我想從用戶那里收集用戶信息,然后將其顯示給他們。 我認為我堅持從不同的對象構建對象構造函數。
這是我到目前為止的內容:
超類:
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);
這是我的土地子類別:
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;
這是我的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;
}
這就是我的主要王國:
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());
}
我覺得自己走在正確的道路上,但是最后一步對我來說並不是單擊,而我一直在努力,閱讀,觀看視頻,沒有任何點擊。
另外,如果我在這篇文章中犯了新手錯誤,我深表歉意。 我將接受所有批評,建議和幫助,這是一個積極的教訓。 謝謝。
總體而言,您的代碼是正確的。
除了: Creature userInfo = null;
您在第一個IF中定義它,則其范圍將限於該IF。 離開該IF后,它便不復存在,因此您不能在以下IF中使用它。
請考慮以下范圍更改:
if(selection==1 || selection==2){ // main IF
Creature userInfo = null;
...
if(selection == 1){
...
}
else {
...
}
System.out.println(userInfo.displayCr());
} // end of main IF
首先擴大size,weight和userInfo的范圍,這意味着將其保留在main范圍之內,而將其保留在所有條件之外的條件下。 這樣的事情。
int selection = 0;
String size = null;
String weight = null;
Creature userInfo = null;
代替此creature.add(userInfo);
應該
user.add(userInfo);
然后像這樣調用顯示
userInfo.displayCr();
因為該方法的返回類型為void,所以不能在sysout內部使用,並且應該在else if(){}
內部
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.