2016-07-06 77 views
-5

我想創建一個程序,使用對話框和對象目標(字符串,int,int)中的元素存儲。這只是我在Uni假期決定要做的一個家庭項目。先前在java arraylist中輸入元素的變量返回值

到目前爲止,它的工作方式如何計劃,但有一個錯誤,我不明白。

//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。 然後他們將投入第二個實例 goal:b,goalPref:2,goalFreq:2. 但是對於第二個實例,假設爲2的goalPref將返回1,而goalFreq將正確返回2。如果下一個實例 目標:C,goalPref:3 goalFreq:3 然後,它會返回C,2,3與前goalPref爲2

,混淆我是輸入goalPref代碼中的事情, goalFreq是相同的,所以爲什麼一個人在工作,一個人不在?

這裏是目標類代碼:

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; 
    } 
} 
+0

對不起,但你的代碼是一團糟。嘗試將常見的代碼塊移入方法中,以便讀取更容易。 –

+0

我很懷疑。你的意思是每個方法都有一個標籤,或者將方法分解成可以一起工作的較小方法? – Wade

回答

1

你應該用一個調試器,看看有什麼是你的代碼錯誤。這裏的問題是你的success變量,它爲你的輸入值確定n的值。

目前在你的循環到底是true所以在下一個週期它的價值仍然是true和進入目標PREF後,它不是在你的while(!success)循環要確定nn所以最後一個值的值(這是最後一次您輸入的目標頻率值)分配給新目標優先權。

爲了更正它,您需要在每次迭代開始時將success的值重置爲false。

while(!setupFinished){ 
    success = false; 
    Goals goal = new Goals(); 

希望這會有所幫助。

+0

啊,謝謝,我試過使用調試器,但我想我沒有把足夠的小點旁邊的代碼。仍然沒有經驗使用它。 – Wade

+0

這發生在一開始..很高興這有助於你..有趣的編碼:) – Sanjeev