如何在方法之间调用字符串?

时间:2018-10-18 00:48:58

标签: java string eclipse methods

所以我是编程新手,我正在尝试用Java创建文本RPG。每当我尝试在设置字符名称的地方以外的方法中调用字符名称时,它都将返回null。有什么解决方案吗?我不确定要删掉哪些位,因为它们可能很重要,所以这里是整个程序。

代码:

import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class RPG {
    static int health;
    static int enedamage;
    static int Low = 10;
    static int High = 100;
    static Scanner kbReader = new Scanner(System.in);
    static int eventnum = 0;
    static String[] enemies = {"goblin", "bat", "skeleton", "beast", "guard", "demon"};
    static String Char;
    static String occup;
    static String weap;
    static int Enehealth;
    static int weapondmg;
    public static void main(String args[]) {


        System.out.print("Input character name:");
        String Char = kbReader.nextLine();
        System.out.print("Input character occupation:");
        String occup = kbReader.nextLine();
        System.out.print("Input character weapon:");
        String weap = kbReader.nextLine();
        System.out.println("And so "+Char+" the brave "+occup+" sets out on their journey, brandishing their mighty "+weap);
        System.out.println(Char+" the " + occup +" kills a " + enemies[(int)Math.round(Math.random()*5)]+" with their "+weap+"!");
        weaponroller(100,90);
        System.out.println(weapondmg);
        eventroller(2,1);
        event();
    }

    public static void event(){
        if (eventnum == 1) {
            System.out.println(Char+"walks into a deep dungeon; who knows what they'll encounter here...");
            enemyroller(50,45,25,15);
            System.out.println(Char+" encounters a "+enemies[(int)Math.round(Math.random()*5)]+"!");

        }
        if (eventnum == 2) {
            System.out.println(Char+" walks into a dense forest; its misty echoes are unsettling...");
            enemyroller(50,45,25,15);
            System.out.println(Char+" encounters a "+enemies[(int)Math.round(Math.random()*5)]+"!");

        }

    }
    public static void weaponroller(int a, int b) {
        Random r = new Random();
        High = a;
        Low = b;
        weapondmg = r.nextInt(High-Low) + Low;
    }
    public static void enemyroller(int a, int b, int c, int d){
        Random r = new Random();
        High = a;
        Low = b;
        enedamage = r.nextInt(High-Low) + Low;
        High = c;
        Low = d;
        Enehealth = r.nextInt(High-Low) + Low;
    }
    public static void eventroller(int a, int b) {
        Random r = new Random();
        High = a;
        Low = b;
        eventnum = r.nextInt(High-Low) + Low;
    }
    public static void charrunner(int a, int b) {
        Random r = new Random();
        High = a;
        Low = b;
        health = r.nextInt(High-Low) + Low;
    }


}

1 个答案:

答案 0 :(得分:0)

您的代码存在多个问题。我将尝试解释为什么您遇到问题:

  • 您在上述RPG类中至少具有两个范围:
    • static范围-声明为static的所有内容基本上都是该类的全局值。
    • main范围-此处声明的所有内容仅在该main方法的上下文内是“活动的”。因此,
  • 您要使用不同的作用域(在本例中为static和main)使用相同的名称多次实例化变量。例如,您的String Char变量被定义为静态(全局),但随后您在main方法中再次对其进行了重新定义,并根据用户从键盘输入的内容对其进行分配。允许重复使用变量名,也称为 shadowing ,但是您必须清楚使用的是哪一个。如果您更改一个的值,则另一个将不知道该更改。这就是通常不建议使用阴影的原因,因为在代码中更容易出错。
  • 在代码示例中,您分配了main范围内的Char用户输入的字符名称,但是当您在event方法中打印出字符名称时,作用域中只有Charstatic,从未初始化过的 。这就是为什么当您尝试将其打印出来时得到null值的原因。
  • 变量的camelCase不一致,从而导致代码混乱。类名应该大写(例如RPG是大写),但是变量名称应该用驼峰式大写,并且最好不要缩写,因此很清楚它们代表什么(characterName而不是Char
  • 在Java / OO编程中不建议使用
  • static变量,因为您可以使用范围适当的私有/公共变量来执行几乎所有操作。不过,对于像这样的简单示例,可以使用它们。

要解决您提到的问题,而不是在Char方法中重新实例化main名称,您只需使用全局Char并将其分配给用户输入的值

这是我对“固定” RPG课的看法。

import java.util.Random;
import java.util.Scanner;

public class RPG {

static int health;
static int enemyDamage;
static int eventnum = 0;
static String[] enemies = {"goblin", "bat", "skeleton", "beast", "guard", "demon"};
static String character;
static String occupation;
static String weapon;
static int enemyHealth;
static int weaponDamage;

public static void main(String args[]) {
    Scanner kbReader = new Scanner(System.in);

    System.out.print("Input character name:");
    character = kbReader.nextLine();
    System.out.print("Input character occupation:");
    occupation = kbReader.nextLine();
    System.out.print("Input character weapon:");
    weapon = kbReader.nextLine();
    System.out.println("And so " + character + " the brave " + occupation + " sets out on their journey, brandishing their mighty " + weapon);
    System.out.println(character + " the " + occupation + " kills a " + enemies[(int) Math.round(Math.random() * 5)] + " with their " + weapon + "!");
    weaponRoller(100, 90);
    System.out.println(weaponDamage);
    eventRoller(2, 1);
    event();
}

public static void event() {
    if (eventnum == 1) {
        System.out.println(character + " walks into a deep dungeon; who knows what they'll encounter here...");
        enemyRoller(50, 45, 25, 15);
        System.out.println(character + " encounters a " + enemies[(int) Math.round(Math.random() * 5)] + "!");

    }
    if (eventnum == 2) {
        System.out.println(character + " walks into a dense forest; its misty echoes are unsettling...");
        enemyRoller(50, 45, 25, 15);
        System.out.println(character + " encounters a " + enemies[(int) Math.round(Math.random() * 5)] + "!");

    }

}

public static void weaponRoller(int a, int b) {
    Random r = new Random();
    weaponDamage = r.nextInt(a - b) + b;
}

public static void enemyRoller(int a, int b, int c, int d) {
    Random r = new Random();
    enemyDamage = r.nextInt(a - b) + b;
    enemyHealth = r.nextInt(c - d) + d;
}

public static void eventRoller(int a, int b) {
    Random r = new Random();
    eventnum = r.nextInt(a - b) + b;
}

public static void charrunner(int a, int b) {
    Random r = new Random();
    health = r.nextInt(a - b) + b;
}
}