vendredi 12 janvier 2018

java program runs with no display only (happens with a random number generator)

Im making this program with a random number generator (with using collections.shuffle). The expected output is to display the random number including other values associated with that number. When I run the program its working fine and displays the values that I set in the codes. But after tweaking it more later, I found out that there are some times when I run the program, it doesn't display at all. I thought it was just an isolated case but I went to spam running it again and again and it seems that there are instances where it doesn't display anything at all. I think this already happened to me in a program i made before so I think this may have something to do with having a rng in the code. I really need this to work.

Here' s the code for the program. (Currently using BlueJ)

    Random random = new Random();

    int plevel=0, mlevel=0, leveldif=0, mlevelchance=0;

    String lvl1="";
    int lvl=0;

    BufferedReader playerlvl = new BufferedReader (new FileReader ("playerlvl.txt"));
    while ((lvl1 = playerlvl.readLine())!=null)
    {
        int lvl2 = Integer.parseInt(lvl1);
        lvl = lvl2;
    }

    Set<Integer> generated = new LinkedHashSet<Integer>();

    mlevelchance = random.nextInt ((99)+1)+1;

    if (mlevelchance<=80)
    {
        while (leveldif<=0)
        {
            leveldif = random.nextInt (((lvl)-(lvl-2))+1)+(lvl-2);
            mlevel = leveldif;
            generated.add(leveldif);
        }
    }
    if (mlevelchance>80 && mlevelchance<95)
    {
        while (leveldif<=0)
        {
            leveldif = random.nextInt (((lvl-3)-(lvl-5))+1)+(lvl-5);
            mlevel = leveldif;  
            generated.add(leveldif);
        }
    }
    if (mlevelchance>=95){mlevel = lvl+1;}

    System.out.println("Spawn Chance: "+mlevelchance);
    System.out.println("Player Level: "+lvl);
    System.out.println("Monster Level: "+mlevel);
}

}

Just for context, this program is for randomizing the level of monster spawns* based on my level.




Aucun commentaire:

Enregistrer un commentaire