我想弄明白为什么会有这些错误。
我正在制作程序的提示基于:http://puu.sh/lv7ho/2e415d805b.png
我已经完成了这个程序的大部分,我只是遇到了一些我希望得到一些帮助的错误。我为程序的每个功能制作了单独的方法,比如打印,计算和收集输入。
我在第43-45行的错误中为totalTrees,totalSeeds和minSeeds说“找不到符号”。然后第56行我得到了所有三个相同的错误。
这是因为我只在main方法中声明了它们并且需要复制它们。但是,我是一个菜鸟,我不知道如何将数据从getCalculate复制到程序中。我怀疑它与我的getCalculate返回有关。
我的代码:
import javax.swing.JOptionPane;
public class TreeCalc{
public static void main(String[] args){
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);
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]));
}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(int i=0; i<treeTypes.length;i++){
minSeeds+=(desiredYield[index] * (decayRate[index]*7))+desiredYield[index];
totalSeeds+=minSeeds;
totalTrees+=desiredYield[index];
}
return desiredYield;
}
public static void printMessage(double[]decayRate, int[]desiredYield, String[]treeTypes){
getCalculate(decayRate, desiredYield, treeTypes);
String treeReport = "Tree Type | Minimum Seeds | Total Seeds | Total Trees ";
for(int i=0; i<treeTypes.length; i++){
treeReport += "\n"+treeTypes[i] + " " + minSeeds + " " + totalSeeds + " " + totalTrees;
}
JOptionPane.showMessageDialog(null, treeReport);
}
}
答案 0 :(得分:0)
将这些字段设为静态,并将其从该方法中删除,如此
package treecalc;
import javax.swing.JOptionPane;
public class TreeCalc {
static int totalSeeds = 0;
static int totalTrees = 0;
static int minSeeds = 0;
static int index = 0;
public static void main(String[] args) {
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);
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]));
} 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) {
for (int i = 0; i < treeTypes.length; i++) {
minSeeds += (desiredYield[index] * (decayRate[index] * 7)) + desiredYield[index];
totalSeeds += minSeeds;
totalTrees += desiredYield[index];
}
return desiredYield;
}
public static void printMessage(double[] decayRate, int[] desiredYield, String[] treeTypes) {
getCalculate(decayRate, desiredYield, treeTypes);
String treeReport = "Tree Type | Minimum Seeds | Total Seeds | Total Trees ";
for (int i = 0; i < treeTypes.length; i++) {
treeReport += "\n" + treeTypes[i] + " " + minSeeds + " " + totalSeeds + " " + totalTrees;
}
JOptionPane.showMessageDialog(null, treeReport);
}
}
或者您可以创建一个名为Result的类,以便您可以在getCalculate()
中一次性返回所有值,如:
public class Result {
int[] desiredYield;
int totalSeeds;
int totalTrees;
int minSeeds =;
public Result(){
//construct a result
}
//getters and setter
}
然后将getCalculate()
的返回类型更改为结果并返回Result
而不只是desiredYeilds
,然后将该结果传递给printMessage()
,这样您就可以可以在Result