ダイアログボックスを使用して要素をObject Goals(string、int、int)に格納するプログラムを作成しようとしています。これはUniの休暇中に私が決めたホームプロジェクトです。Java arraylistの以前に入力した要素の値を返す変数
これまでのところ、私はどのように計画していますが、わからないバグが1つあります。
//asks user to input goals and stores them in an array list
public static void setup(){
int n = 0;
int i = 0;
boolean setupFinished = false;
boolean success = false;
List<Goals> setupList = new ArrayList<Goals>();
JOptionPane setupBox = new JOptionPane();
while(!setupFinished){
Goals goal = new Goals();
String str1 = JOptionPane.showInputDialog("A goal you wish to work on?");
if(str1 == null){
System.exit(0);
}
goal.setGoal(str1);
String goalPrefInput = JOptionPane.showInputDialog("What is the initial preference of this goal compare to the others?");
if(goalPrefInput == null){
System.exit(0);
} else if (goalPrefInput.equalsIgnoreCase("")){
n = Integer.parseInt("1");
} else {
while(!success){
try {
n = (Integer.parseInt(goalPrefInput));
success = true;
}
catch (Exception NumberFormatException){
JOptionPane.showMessageDialog(null, "You must enter a valid number");
}
}
}
goal.setGoalPref(n);
System.out.println(goal.getGoalPref());
success = false;
String goalFreqInput = JOptionPane.showInputDialog("What is the frequency rate you wish this preference to increase?");
if(goalFreqInput == null){
System.exit(0);
} else if (goalFreqInput.equalsIgnoreCase("")){
n = Integer.parseInt("1");
} else {
while(!success){
try {
n = (Integer.parseInt(goalFreqInput));
success = true;
}
catch (Exception NumberFormatException){
JOptionPane.showMessageDialog(null, "You must enter a valid number");
}
}
}
goal.setGoalPrefIncrease(n);
System.out.println(goal.getGoalPrefIncrease());
setupList.add(i, goal);
i++;
int f = JOptionPane.showConfirmDialog(null, "Have you finished setup?", "Setup Finished?", YES_NO_OPTION);
if(f == JOptionPane.YES_OPTION){
setupFinished = true;
}
}
System.out.print(setupList.toString());
String s = removeBrackets(setupList.toString());
JOptionPane.showMessageDialog(setupBox, "Here are your goals \n" + s);
}
}
それが終了していないですが、私は理解していないということで何が起こっているかは、ユーザが中に入れます目標の最初のインスタンスに入ることです - 目標:、goalPref:1、goalFreq:1。 ゴールに入れる2番目のインスタンス:b、goalPref:2、goalFreq:2 しかし、2番目のインスタンスのgoalPrefは2になり、goalFreqは2を正しく返します。 C、goalPref:3 goalFreq:次のインスタンスが の目標である場合3. そして、それは以前goalPrefとしてC、2、3を返します私を混乱させる2.
事がgoalPrefを入力するためのコードでありましたし、 goalFreqは同一なので、なぜ1つが動作し、1つは動作しないのですか?ここで
は、目標クラスのコードです:
public class Goals {
private String goal;
private int goalPref;
private int goalPrefIncrease;
public String getGoal() {
return goal;
}
public void setGoal(String goal) {
this.goal = goal;
}
public int getGoalPref() {
return goalPref;
}
public void setGoalPref(int goalPref) {
this.goalPref = goalPref;
}
public int getGoalPrefIncrease() {
return goalPrefIncrease;
}
public void setGoalPrefIncrease(int goalPrefIncrease) {
this.goalPrefIncrease = goalPrefIncrease;
}
@Override
public String toString() {
String s = "Goal: " + this.getGoal() + ", Goal Preference: " + this.getGoalPref() + ", Goal Frequency Rate: " + this.getGoalPrefIncrease();
//String s = goal + ", Goal Preference: " + goalPref + ", Goal Frequency Rate: " + goalPrefIncrease;
return s;
}
}
申し訳ありませんが、あなたのコードは混乱しています。共通のコードブロックをメソッドに移動して読みやすくするようにしてください。 –
私はそれほど疑わしい。あなたは各メソッドのタブを持っているか、一緒に働く小さなメソッドにメソッドを分割することを意味しますか? – Wade