This question already has an answer here:
- Division of integers in Java 4 answers
Writing a program that estimates a person's expenses for 4 years based on their expenses for one week. Program assumes this weekly expense will fluctuate between a 20% decrease and a 20% increase from week to week. A random number is generated for each week of the 4 years to determine how much it actually fluctuates from the given week.
Code that is giving me trouble:
int randomNumber;
float fourYearSum = 0;
float randomPercent;
for(int p=0; p<208; p++){
Random random = new Random();
randomNumber = (random.nextInt(41)-20);
//to get %
randomPercent = randomNumber/100;
fourYearSum = fourYearSum + (weeklyExpense*(1+randomPercent));
}
I tested and saw that the variable randomNumber is correct and is returning values from [-20,20] but when I try to divide by 100 in the variable randomPercent it is returning every time as a value of 0.0, why is this?
Thanks for your help
Aucun commentaire:
Enregistrer un commentaire