我如何创建一个循环以将值随机分配给类实例数组的每个特征

时间:2018-10-24 01:08:04

标签: arrays loops class random instance

因此,我正在创建一个需要不同纸牌的纸牌游戏,因此我创建了一个纸牌类,在其中声明了字符串值名称和其他例如幂的整数值。情报

public static class hero{
static String name;
static int strength;
static int intellect;
static int flight;
static int tech;
}

因此,我创建了这些类的实例数组。 它们的名称是从文本文件中读取的,并分配给名称值。 Q1)我在读取文件并将字符串分配给类的每个实例的名称值时遇到麻烦。 这是我到目前为止所做的

public static void readLines(File f)throws IOException{
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while((line = br.readLine()) != null){
        System.out.println(line);
        }
        br.close();
        fr.close();
        }
        static File f = new File("C:/Users/jeff/Desktop/test/names.txt");
        try{
        readLines(f);
        } catch  (IOException e){
        e.printStackTrace();
    }

Q2)我也遇到麻烦的部分是我需要创建一个循环以将值随机分配给类的每个实例的每个幂的部分。 这是我到目前为止所做的

               {
               hero [] cards = new hero[cardNumber];
               for(int i=4;i<cardNumber;i++){ cards[i]=new hero();}
               Random rand = new Random();
               for(int i=0; i<cards.length; ++i)
               {
               cards[i].strength = rand.nextInt(25) + 1;
               cards[i].intellect = rand.nextInt(25) + 1;
               cards[i].flight = rand.nextInt(25) + 1;
               cards[i].tech = rand.nextInt(25) + 1;
               }    

但是当我打印出这些值时,所有实例的功效都具有相同的值。 例如,卡片12的智力= 6 卡14智力= 6

任何人都可以帮助我解决这些问题,我们将不胜感激任何指导 谢谢

0 个答案:

没有答案