I'm trying to calculate something properly. A user enters an amount of trees they want to plant, and there is a rule that a certain percent of trees die every single year. It says after 7 years the trees are sellable. To find the minimum number of seeds needed, you'd take however many die over the course of 7 years for each tree type, and add that to the initial number the user inputted, right?
Well, this is what I get when i enter 20,40,30
for each tree type respectively:
I get the same number of minimum seeds regardless. I assume this has to do with my index or something in my for loop. I'd just like to know how to properly calculate this.
The other issue: I am trying to format my output properly so that it tabs over for each variable so it doesn't look so ugly the way it does. I tried "\\t"
but it did nothing. How do I fix this, and the calculations so that each iteration is actually kept track of in the end? should I be using decayRate[i]
, desiredYield[i]
etc instead of desiredYield[index]
etc?
public class TreeCalc {
public static void main(String[] args){
//Initializes methods and arrays
String[] treeTypes = new String[] {"Fir", "Pine", "Spruce"};
int[] desiredYield = new int [treeTypes.length];
double[] decayRate = new double[] {0.07, 0.12, 0.08};
desiredYield = getYield(decayRate, desiredYield, treeTypes);
int[] data = getCalculate(decayRate, desiredYield, treeTypes);
printMessage(decayRate, desiredYield, treeTypes);
}
//Asks user to input # of trees for each tree type
public static int[] getYield(double[]decayRate, int[] desiredYield, String[]treeTypes) {
int index= 0;
for(int i=0;i < treeTypes.length;i++) {
try {
desiredYield[index] = Integer.parseInt(JOptionPane.showInputDialog("Please enter your desired yield for: " +treeTypes[i]));
//Catches any non-number input, displays error to user
} catch(NumberFormatException e){
desiredYield[index] = 0;
JOptionPane.showMessageDialog(null, "Error: Please enter your desired yield for "+treeTypes[i]);
}
if (desiredYield[index] <= 0) {
JOptionPane.showMessageDialog(null, "Error: Please enter your desired yield for "+treeTypes[i]);
} else{
index++;
}
}
return desiredYield;
}
//Calculates totals and minimums
public static int[] getCalculate(double[]decayRate, int[]desiredYield, String[]treeTypes){
int totalSeeds =0;
int totalTrees=0;
int minSeeds=0;
int index=0;
//For each iteration of the loop, calculates totals/mins
for(int i = 0; i < treeTypes.length; i++){
minSeeds += (desiredYield[index] * (decayRate[index]*7)) + desiredYield[index];
totalSeeds += minSeeds;
totalTrees += desiredYield[index];
}
return new int[] {totalSeeds, totalTrees, minSeeds};
}
//Prints the totals, minimums, and input from the user
public static void printMessage(double[]decayRate, int[]desiredYield, String[]treeTypes){
//Calls method and stores values within array
int[]data = getCalculate(decayRate, desiredYield, treeTypes);
int totalSeeds = data[0];
int totalTrees = data[1];
int minSeeds = data[2];
//Report displays after user is done entering inputs
String treeReport = "Tree Type | Desired Yield | Minimum Seeds | Total Seeds | Total Trees ";
for(int i=0; i<treeTypes.length; i++){
treeReport += "\n"+treeTypes[i] + " "+desiredYield[i] + " "+minSeeds + " "+totalSeeds + " "+totalTrees;
}
JOptionPane.showMessageDialog(null, treeReport);
}
}
No, your equation for the minimum number of seeds needed is not correct. You need something like M=Y/((1-r)^7)
Where r is the proportion of trees that die each year, and Y is the desired yield. You solve for M, the minimum # of seeds.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.