如何显示我从子类设置的参数的输出?
我需要帮助访问使用子类输入的变量。我在主班上为子类做了一个对象,但是,我不知道我将如何访问已输入的输入并在代码末尾显示它们。
public abstract class player {
public abstract String name(String name);
public abstract void race(int race);
public abstract int[] getStatArray();
}
import java.util.Scanner;
public class childplayer extends player {
Scanner sc = new Scanner(System.in);
public String name(String name) {
System.out.print("Enter your name: ");
name = sc.nextLine();
return name;
}
public void race(int race) {
System.out.println("Race options: ");
System.out.println("\t 1 - Human \n\t 2 - Elf \n\t 3 - Orc \n\t 4 - Angel \n\t 5 - Demon");
System.out.print("Enter character's race: ");
race = sc.nextInt();
if((race>5)||(race<1)) {
while ((race>5)||(race<1)) {
System.out.print("Enter character's race: ");
race = sc.nextInt();
}
}System.out.println(" ");
}
public int[] getStatArray(){
int [] stat = new int [3];
int x = 0, y = 0, pts = 0;
System.out.println("Enter character's stats.");
while(y<3) {
System.out.print("Enter value: ");
x = sc.nextInt();
y++;
pts = pts + x;
}
int i = 0;
if(pts>10) {
System.out.println("Invalid input. Try again.");
while(i<3) {
System.out.print("Enter value: ");
x = sc.nextInt();
i++;
pts = pts + x;
}
}else {
stat[i] = x;
i++;
}
return stat;
}
}
I need help accessing the variables I have inputted using the child class. I made an object for the child class in my main class, however, I do not know how will I have access to the inputs I have entered and display them at the end of the code.
public abstract class player {
public abstract String name(String name);
public abstract void race(int race);
public abstract int[] getStatArray();
}
import java.util.Scanner;
public class childplayer extends player {
Scanner sc = new Scanner(System.in);
public String name(String name) {
System.out.print("Enter your name: ");
name = sc.nextLine();
return name;
}
public void race(int race) {
System.out.println("Race options: ");
System.out.println("\t 1 - Human \n\t 2 - Elf \n\t 3 - Orc \n\t 4 - Angel \n\t 5 - Demon");
System.out.print("Enter character's race: ");
race = sc.nextInt();
if((race>5)||(race<1)) {
while ((race>5)||(race<1)) {
System.out.print("Enter character's race: ");
race = sc.nextInt();
}
}System.out.println(" ");
}
public int[] getStatArray(){
int [] stat = new int [3];
int x = 0, y = 0, pts = 0;
System.out.println("Enter character's stats.");
while(y<3) {
System.out.print("Enter value: ");
x = sc.nextInt();
y++;
pts = pts + x;
}
int i = 0;
if(pts>10) {
System.out.println("Invalid input. Try again.");
while(i<3) {
System.out.print("Enter value: ");
x = sc.nextInt();
i++;
pts = pts + x;
}
}else {
stat[i] = x;
i++;
}
return stat;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要保留以后使用的值,则需要存储它们。最好的方法就是简单地使用类似类的类变量:
然后,我们只是修改您使用这些变量的方法,例如:
另一个示例:
然后,为了稍后获取信息:
我强烈建议您使用一个填充类数据的构造方法。为了此示例,我简化了代码和价值检查:
If you want to keep the values to use later then you need to store them. The best way to do this is simply with a class variable like so:
Then we just modify your methods to use these variables, for example:
And another example:
Then to get the information later we simply use:
I strongly recommend making use of a constructor method to populate the class data. I have simplified the code and value checking for the sake of this example: