Java:如何从另一个类打印数组?我的数组包含来自该类的另一个变量

好的,这是我的代码,我知道它很混乱,而且充满了其他错误,我仍在努力学习。我知道我可以在一个班级中做到这一点,但这会脱离 java 的 OOP 部分。我想如果我要学习,我也可以正确地学习。


charHpArray两天来,我一直试图弄清楚如何打印出来,但我在谷歌上找到的所有内容都没有用,因为它们不包含另一个数组。本质上,我试图将它设置为参数级别,因此当level= 1 时hp为 5,何时level为 2 时hp为 10,依此类推。


另外,如果你能告诉我为什么level + #不起作用那会很棒。//如果你也想尝试一下,我正在努力让mage, arch, 和war都有不同的起始奖金,所以war比基本level奖金多一点健康。


** 鼓励所有答案,即使这意味着新想法我只是想学习**


谢谢。


编码:


import java.io.*;

import java.util.Scanner;

import java.util.Random;


public class Main {


    public static void main(String[]args) {


        Scanner input = new Scanner(System.in);

        Random roll = new Random();


        System.out.println("Welcome To Adventure Heroes");

        System.out.println("-------------------");


        System.out.println("You awake to the sound of screaming, \"GET AWAY FROM ME!\"");

        System.out.println("You call out to the voice in the distance...");

        System.out.println("There is no answer");

        System.out.println("You run towards the troubled screams in the distance");

        System.out.println("You see a man on the floor being kicked, without hesitation you charge in and push the attacker off the man.");

        System.out.println("You examine the man to see if he is okay. When you look up the attacker is gone.");

        System.out.println("\"Thank you so much... erm, come to think of it I've never seen you around these parts. What is Your name?\"");


        String yourName = input.next();


        System.out.println("\"What a weird name, Hi " + yourName + ", I'm Borgan\"");

        System.out.print("\"Well "  + yourName + ", your journey begins here.\"");


        Character.getcharHp() ;



        public class Character {


            int[] level ={1,2,3,4,5,6,7,8,9,10};

            int[] charHpArray = {5,10,15,20,25,30,35,40,45,50};


            Character(int[]  charHp)

            {

                charHp = charHpArray;

            }


            public int[] getCharHp() {

                return charHpArray; 

            } 



暮色呼如
浏览 173回答 2
2回答

小怪兽爱吃肉

您有一个 level 数组,但没有指明 level int 值。这就是 add 不起作用的原因(因为 level 是一个数组,而您正在向它添加一个 int)。我建议尝试使用 HashMap 将级别映射到 hp(例如,将此字段称为 levelHpMap,并使其保持不变),然后使用每个字符设置的单独字段,称为级别,用于初始化字符属性/fields(而不是 hp)。附加字符将通过继承扩展 Character 类。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java