家庭作业帮助-Java继承和getter/setter
需要一些家庭作业帮助,在我的控制台上有一些奇怪的输出 这是作业问题: 创建一个名为Horse的类,该类包含名称、颜色和出生的数据字段 一年。包括这些字段的get和set方法。接下来,创建一个名为 赛马,它包含一个额外的字段,用于记录比赛的数量 这匹马已经参加了比赛,还有其他的方法来获得和设置新的场地。 编写一个应用程序,演示如何使用每个类的对象。将文件另存为 java、racemarse.java和DemoHorses.java 这是我的密码: Horse.java家庭作业帮助-Java继承和getter/setter,java,Java,需要一些家庭作业帮助,在我的控制台上有一些奇怪的输出 这是作业问题: 创建一个名为Horse的类,该类包含名称、颜色和出生的数据字段 一年。包括这些字段的get和set方法。接下来,创建一个名为 赛马,它包含一个额外的字段,用于记录比赛的数量 这匹马已经参加了比赛,还有其他的方法来获得和设置新的场地。 编写一个应用程序,演示如何使用每个类的对象。将文件另存为 java、racemarse.java和DemoHorses.java 这是我的密码: Horse.java public class H
public class Horse {
private String name;
private String color;
private int birthYear;
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 int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
}
public class RaceHorse extends Horse {
private int races;
public int getRaces() {
return races;
}
public void setRaces(int races) {
this.races = races;
}
}
import java.util.Scanner;
public class DemoHorses {
public static void main(String[] args) {
Horse aHorse = new Horse();
RaceHorse aRaceHorse = new RaceHorse();
String DemoName;
String DemoColor;
String RaceDemoName;
String RaceDemoColor;
int DemoYear;
int RaceDemoYear;
int DemoRace;
Scanner input = new Scanner(System.in); // Horse
System.out.print("Enter the name of the horse >> ");
DemoName = input.nextLine();
aHorse.setColor(DemoName);
System.out.print("Enter the color of the horse >> ");
DemoColor = input.nextLine();
aHorse.setColor(DemoColor);
System.out.print("Enter the birth year of the horse >> ");
DemoYear = input.nextInt();
aHorse.setBirthYear(DemoYear);
System.out.print("Enter the name of the racehorse >> "); //Racehorse
RaceDemoName = input.nextLine();
aRaceHorse.setName(RaceDemoName);
System.out.print("Enter the color of the racehorse >> ");
RaceDemoColor = input.nextLine();
aRaceHorse.setColor(RaceDemoColor);
System.out.print("Enter the birth year of the racehorse >> ");
RaceDemoYear = input.nextInt();
aRaceHorse.setBirthYear(RaceDemoYear);
System.out.print("Enter the number of races the racehorse has completed >> ");
DemoRace = input.nextInt();
aRaceHorse.setRaces(DemoRace);
System.out.println(aHorse.getName() + " is " + aHorse.getColor() + " and was born in " + aHorse.getBirthYear());
System.out.println(aRaceHorse.getName() + " is " + aRaceHorse.getColor() + " and was born in " + aRaceHorse.getRaces());
input.close();
}
}
racehouse.java
public class Horse {
private String name;
private String color;
private int birthYear;
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 int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
}
public class RaceHorse extends Horse {
private int races;
public int getRaces() {
return races;
}
public void setRaces(int races) {
this.races = races;
}
}
import java.util.Scanner;
public class DemoHorses {
public static void main(String[] args) {
Horse aHorse = new Horse();
RaceHorse aRaceHorse = new RaceHorse();
String DemoName;
String DemoColor;
String RaceDemoName;
String RaceDemoColor;
int DemoYear;
int RaceDemoYear;
int DemoRace;
Scanner input = new Scanner(System.in); // Horse
System.out.print("Enter the name of the horse >> ");
DemoName = input.nextLine();
aHorse.setColor(DemoName);
System.out.print("Enter the color of the horse >> ");
DemoColor = input.nextLine();
aHorse.setColor(DemoColor);
System.out.print("Enter the birth year of the horse >> ");
DemoYear = input.nextInt();
aHorse.setBirthYear(DemoYear);
System.out.print("Enter the name of the racehorse >> "); //Racehorse
RaceDemoName = input.nextLine();
aRaceHorse.setName(RaceDemoName);
System.out.print("Enter the color of the racehorse >> ");
RaceDemoColor = input.nextLine();
aRaceHorse.setColor(RaceDemoColor);
System.out.print("Enter the birth year of the racehorse >> ");
RaceDemoYear = input.nextInt();
aRaceHorse.setBirthYear(RaceDemoYear);
System.out.print("Enter the number of races the racehorse has completed >> ");
DemoRace = input.nextInt();
aRaceHorse.setRaces(DemoRace);
System.out.println(aHorse.getName() + " is " + aHorse.getColor() + " and was born in " + aHorse.getBirthYear());
System.out.println(aRaceHorse.getName() + " is " + aRaceHorse.getColor() + " and was born in " + aRaceHorse.getRaces());
input.close();
}
}
DemoHorse.java
public class Horse {
private String name;
private String color;
private int birthYear;
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 int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
}
public class RaceHorse extends Horse {
private int races;
public int getRaces() {
return races;
}
public void setRaces(int races) {
this.races = races;
}
}
import java.util.Scanner;
public class DemoHorses {
public static void main(String[] args) {
Horse aHorse = new Horse();
RaceHorse aRaceHorse = new RaceHorse();
String DemoName;
String DemoColor;
String RaceDemoName;
String RaceDemoColor;
int DemoYear;
int RaceDemoYear;
int DemoRace;
Scanner input = new Scanner(System.in); // Horse
System.out.print("Enter the name of the horse >> ");
DemoName = input.nextLine();
aHorse.setColor(DemoName);
System.out.print("Enter the color of the horse >> ");
DemoColor = input.nextLine();
aHorse.setColor(DemoColor);
System.out.print("Enter the birth year of the horse >> ");
DemoYear = input.nextInt();
aHorse.setBirthYear(DemoYear);
System.out.print("Enter the name of the racehorse >> "); //Racehorse
RaceDemoName = input.nextLine();
aRaceHorse.setName(RaceDemoName);
System.out.print("Enter the color of the racehorse >> ");
RaceDemoColor = input.nextLine();
aRaceHorse.setColor(RaceDemoColor);
System.out.print("Enter the birth year of the racehorse >> ");
RaceDemoYear = input.nextInt();
aRaceHorse.setBirthYear(RaceDemoYear);
System.out.print("Enter the number of races the racehorse has completed >> ");
DemoRace = input.nextInt();
aRaceHorse.setRaces(DemoRace);
System.out.println(aHorse.getName() + " is " + aHorse.getColor() + " and was born in " + aHorse.getBirthYear());
System.out.println(aRaceHorse.getName() + " is " + aRaceHorse.getColor() + " and was born in " + aRaceHorse.getRaces());
input.close();
}
}
以下是我得到的输出:
Enter the name of the horse >> Old Paint
Enter the color of the horse >> black
Enter the birth year of the horse >> 2005
Enter the name of the racehorse >> Enter the color of the racehorse >> black
Enter the birth year of the racehorse >> 2222
Enter the number of races the racehorse has completed >> 12
null is black and was born in 2005
is black and was born in 12
我不知道为什么它会在我的控制台上显示为输入赛马的名称>>输入赛马的颜色>
,我认为用户输入的数据保存不正确。有人知道我如何解决这个问题吗?添加
input.nextLine();
之后
RaceDemoYear=input.nextInt();
第二年(赛马年);