函数多次调用

时间:2019-02-01 07:03:10

标签: java function

尝试编写文本游戏的代码,并要求输入GameSettings类时,该函数被调用3次。我试图在类之间来回发送代码,这就是为什么我使用不同的类来使代码更加简洁,以便在发送monsterHealth ... etc时可以读取的原因。

Game.Java

package src;

import java.io.IOException;

public class Game {

public static void main(String[] args) throws IOException { 

    GameSettings GameSettings = new GameSettings();
    GameSettings.init();


//  GameSettings.Classes();

    GameSettings.StartLogic();

    if (src.GameSettings.Classes().equals("mage")) {
        System.out.println("mage"); 
    }
    else if (src.GameSettings.Classes().equals("warrior")) {
        System.out.println("warrior");  
    }
    else if (src.GameSettings.Classes().equals("archer")) {
        System.out.println("archer");   
    }
    else {
        System.out.println("Non valid");

    }
}
}

GameSettings.Java

package src;
 import java.util.Scanner;
 public class GameSettings extends Game {

public interface classChoice {

}
public int playerHp;
private static Scanner scanner;
private static String nameInput;
private static String classChoice;

private String mage;
private String archer;
private String warrior;

public void init() {
scanner = new Scanner(System.in);

    System.out.println("Welcome To Fizzle's Text Based RPG\nWhat Is Your 
    Name?");
    nameInput = scanner.nextLine(); 
}
    public static String Classes() {
    System.out.println("Welcome " + nameInput + " What Class Would You Like 
    To Be?\n(mage)\n(warrior)\n(archer)");
    classChoice = scanner.nextLine();

    return classChoice;

}

public void StartLogic() {
    playerHp = 10;
    System.out.println(classChoice);    


}


 }

2 个答案:

答案 0 :(得分:0)

我明白了你的问题。在

 GameSettings.StartLogic();

    if (src.GameSettings.Classes().equals("mage")) {
        System.out.println("mage"); 
    }
    else if (src.GameSettings.Classes().equals("warrior")) {
        System.out.println("warrior");  
    }
    else if (src.GameSettings.Classes().equals("archer")) {
        System.out.println("archer");   
    }
    else {
        System.out.println("Non valid");

    }

您要三次调用GameSettings.Classes()。equals()方法。而不是这样做,而是在if / else块之前定义一个String变量:

 GameSettings.StartLogic();

    String input = src.GameSettings.Classes();

    if (input.equals("mage")) {
        System.out.println("mage"); 
    }
    else if (input.equals("warrior")) {
        System.out.println("warrior");  
    }
    else if (input.equals("archer")) {
        System.out.println("archer");   
    }
    else {
        System.out.println("Non valid");

    }

这是因为使用if / else语句时,不应调用依赖运气或用户输入的方法,而应将它们事先定义为变量,并将其作为参数传递给if / else。声明。希望这会有所帮助!

答案 1 :(得分:0)

你好菲兹尔! :)

请澄清您的问题。 我在您的代码中添加了一些注释:

Game.java

import java.io.IOException;

public class Game {

    public static void main(String[] args) throws IOException {

        GameSettings GameSettings = new GameSettings();
        GameSettings.init();

        GameSettings.StartLogic(); //returns null

        if (GameSettings.Classes().equals("mage")) {
            System.out.println("mage");
        } else if (GameSettings.Classes().equals("warrior")) {
            System.out.println("warrior");
        } else if (GameSettings.Classes().equals("archer")) {
            System.out.println("archer");
        } else {
            System.out.println("Non valid");

        }

    }
}

GameSettings.java

import java.util.Scanner;

public class GameSettings extends Game {

    //why did you declare an Interface?
    public interface classChoice {

    }

    public int playerHp;
    private static Scanner scanner;
    private static String nameInput;
    private static String classChoice;

    public void init() {
        scanner = new Scanner(System.in);
        System.out.println("Welcome To Fizzles Text Based RPG What Is Your Name?");
        nameInput = scanner.nextLine();
    }

    public static String Classes() {
        System.out.println("Welcome " + nameInput + " What Class Would You Like To Be?\n(mage)\n(warrior)\n(archer)");
        classChoice = scanner.nextLine();

        return classChoice;

    }

    //why are you calling this method beforehand?
    public void StartLogic() {
        playerHp = 10;
        System.out.println("Your Class:" + classChoice);

    }

}