Java 方法不返回给用户(继承)
我正在尝试将驱动程序中的值读入到一个Java 方法不返回给用户(继承),java,inheritance,hashmap,java.util.scanner,method-call,Java,Inheritance,Hashmap,Java.util.scanner,Method Call,我正在尝试将驱动程序中的值读入到一个HashMap,然后读入一个单独的抽象程序中。之后,我希望将内容打印给用户。然而,当我调用该方法时,什么也没有发生 游戏驱动程序 import java.io.*; import java.util.*; import java.awt.*; public class GameDriver { public static void main(String[] args) throws FileNotFoundException {
HashMap
,然后读入一个单独的抽象程序中。之后,我希望将内容打印给用户。然而,当我调用该方法时,什么也没有发生
游戏驱动程序
import java.io.*;
import java.util.*;
import java.awt.*;
public class GameDriver {
public static void main(String[] args) throws FileNotFoundException {
String s;
File f = new File (args[0]);
Scanner input = new Scanner(f);
while (input.hasNext()) {
s = input.next();
if (s.equalsIgnoreCase("h")) {
Hero c1 = new Cop();
System.out.println(c1);
}
if (s.equalsIgnoreCase("r")) {
Hero c2 = new Cop();
String cn = input.next();
int pts = input.nextInt();
c2.newMap.put(cn, pts);
c2.rescue();
System.out.println(c2);
}
}
}
}
Hero.java
import java.util.*;
import java.io.*;
import java.awt.*;
public abstract class Hero extends Character
{
private String heroname1;
public Hero() {
heroname1 = "Rick Grimes"; //the default player name
}
HashMap<String, Integer> newMap = new HashMap<String, Integer>();
public Hero(String newhero) {
if (newhero.length() > 0) {
heroname1 = newhero;
} else { heroname1 = "Rick Grimes"; } //defaulted as protagonist
}
public String getHeroName() {
return heroname1; //return the name
}
public String rescue() { //class to rescue people or things
return " rescued " + newMap + "pts!";
}
public String toString() { //print
return heroname1 + rescue();
}
}
import java.io.*;
import java.util.*;
import java.awt.*;
public class Cop extends Hero {
public Cop() {
super();
}
public void hero1(String newhero) {
newhero = getHeroName(); //get name from the Hero class
}
public void lieDetect() { //unique ability for cops
System.out.println("Cops can tell the good from the bad");
}
}
最后是我的输出和期望的输出
电流输出
Rick Grimes
Rick Grimes
Rick Grimes
Rick Grimes rescued Carl 100pts!
所需输出
Rick Grimes
Rick Grimes
Rick Grimes
Rick Grimes rescued Carl 100pts!
谢谢你的帮助!询问您是否需要任何澄清
编辑Cop.java
import java.util.*;
import java.io.*;
import java.awt.*;
public abstract class Hero extends Character
{
private String heroname1;
public Hero() {
heroname1 = "Rick Grimes"; //the default player name
}
HashMap<String, Integer> newMap = new HashMap<String, Integer>();
public Hero(String newhero) {
if (newhero.length() > 0) {
heroname1 = newhero;
} else { heroname1 = "Rick Grimes"; } //defaulted as protagonist
}
public String getHeroName() {
return heroname1; //return the name
}
public String rescue() { //class to rescue people or things
return " rescued " + newMap + "pts!";
}
public String toString() { //print
return heroname1 + rescue();
}
}
import java.io.*;
import java.util.*;
import java.awt.*;
public class Cop extends Hero {
public Cop() {
super();
}
public void hero1(String newhero) {
newhero = getHeroName(); //get name from the Hero class
}
public void lieDetect() { //unique ability for cops
System.out.println("Cops can tell the good from the bad");
}
}
在GameDriver中:
if (s.equalsIgnoreCase("h")) {
Hero c1 = new Cop();
System.out.println(c1.getHeroName());
}
英雄类中的营救方法:
public String rescue() { //class to rescue people or things
String toReturn = "";
for(String _key : newMap.keySet()) {
toReturn += " rescued " + _key + " " + newMap.get(_key) + "pts!";
}
return toReturn;
}
我获得了所需的输出。添加Cop类的代码。这可能会有帮助。好吧,我的编辑补充到:?这个问题已经得到了一些回答,但是是的,你可以在那里找到我的所有代码。这里有点更新了,你运行代码了吗?因为我已经完成了,并且获得了以下输出:Rick Grimes解救了{}pts!瑞克·格里姆斯救了{Carl=100}分代码>这几乎是您所期望的。