我有一個正在處理的任務,我需要創建一個系統來檢查輸入的字串是否已經存在于 ArrayList 中。在它不存在的情況下,它將使用輸入的確切文本的名稱生成一個新整數,并將值分配給 1。如果字串已經存在(意味著其他人已經輸入了該選項),那么它將添加一到已經存在的整數。我只是在努力用字串的值分配一個整數。
我嘗試直接獲取輸入并將其分配給一個字串,然后我嘗試使用字串值創建一個新整數。到目前為止,這一直不成功。
static int totalSubmissions = 0; // tracks # of custom submissions (used to navigate Array List)
static ArrayList<String> userInputs = new ArrayList<String>(); // contains strings (not listed ice creams) from user input
private static JLabel pollOtherLabel = new JLabel("Unlisted Flavors: ");
private static JTextField customInput = new JTextField();
private static JButton submitInput = new JButton("Submit");
// Above is the totalSubmmissions (used to navigate ArrayList, the list itself, and my label, submit button, and the neccesary code to record what was entered.
@Override
public void actionPerformed(ActionEvent e) {
// checks vote chocolate and contains output
if(e.getSource() == voteChocolate){
chocolateVotes ;
pollOption1Label.setText("Chocolate Votes: " chocolateVotes);
} else if (e.getSource() == voteVanilla){ // checks vote vanilla and contains output
vanillaVotes ;
pollOption2Label.setText("Vanilla Votes: " vanillaVotes);
} else if (e.getSource() == voteStrawberry){ // checks vote strawberry and contains output
strawberryVotes ;
pollOption3Label.setText("Strawberry Votes: " strawberryVotes);
} else if (e.getSource() == submitInput){ // checks custom input and contains output
String currentString = customInput.getText(); // << ISSUE IS HERE
if (userInputs.contains(currentString)){ // want to assign the text field input
int currentString = 1; // to a new integer, that will track
} else { // the number of times that exact string is entered
int customInput.getText() = 1;
}
userInputs.add(customInput.getText());
customInput.setText("");
pollOtherLabel.setText("Unlisted Flavors: " userInputs.get(totalSubmissions));
totalSubmissions ;
}
}
uj5u.com熱心網友回復:
我猜這個任務有一些預定義的口味(巧克力、香草等),他們有指定的按鈕來投票。此外,它還允許您將自定義口味添加到集合中并計入其投票。
您不能在運行時初始化變數。這是因為您撰寫了代碼,因此計算機會為您運行它。您無法撰寫代碼,因此程式將自行撰寫更多新代碼。(至少這不是本次作業的目的)
在這種情況下,我建議您存盤風味并投票給統一的資料結構。我可以看到您將預定義的口味存盤在strawberryVotes,中chocolateVotes。您可以制作地圖來存盤您需要的所有資訊、預定義或自定義風味。您需要存盤2個資訊。1)風味名稱 2)投票。我們可以使用地圖。它的概念類似于一張桌子。
static int totalSubmissions = 0; // tracks # of custom submissions (used to navigate Array List)
private Map<String, Integer> flavorVotes = new HashMap<String, Integer>();
// contains flavor and vote (not listed ice creams) from user input
private static JLabel pollOtherLabel = new JLabel("Unlisted Flavors: ");
private static JTextField customInput = new JTextField();
private static JButton submitInput = new JButton("Submit");
// define constant for predefined flavor
private static final String FLAVOR_CHOCOLATE = "Chocolate";
private static final String FLAVOR_VANILLA_ = "Vanilla";
private static final String FLAVOR_STRAWBERRY_ = "Strawberry";
// Above is the totalSubmmissions (used to navigate ArrayList, the list itself,
// and my label, submit button, and the neccesary code to record what was
// entered.
@Override
public void actionPerformed(ActionEvent e) {
String flavor;
// checks vote chocolate and contains output
if (e.getSource() == voteChocolate) {
flavor = FLAVOR_CHOCOLATE;
addVote(flavor);
pollOption1Label.setText("Chocolate Votes: " flavorVotes.get(flavor));
} else if (e.getSource() == voteVanilla) { // checks vote vanilla and contains output
flavor = FLAVOR_VANILLA_;
addVote(flavor);
pollOption2Label.setText("Vanilla Votes: " flavorVotes.get(flavor));
} else if (e.getSource() == voteStrawberry) { // checks vote strawberry and contains output
flavor = FLAVOR_STRAWBERRY_;
addVote(flavor);
pollOption3Label.setText("Strawberry Votes: " flavorVotes.get(flavor));
} else if (e.getSource() == submitInput) { // checks custom input and contains output
flavor = customInput.getText();
addVote(flavor);
customInput.setText("");
pollOtherLabel.setText("Unlisted Flavors: " flavor);
totalSubmissions ;
}
}
private void addVote(String flavor) {
if (flavorVotes.containsKey(flavor)) {
// flavor exists, increment by 1
flavorVotes.put(flavor, flavorVotes.get(flavor) 1);
} else {
// new flavor, set its vote to 1
flavorVotes.put(flavor, 1);
}
}
ps 我認為某些屬性不應該宣告為靜態的。但這是另一個話題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/457793.html
上一篇:如何用字串不斷更新畫布?
下一篇:否則,如果只顯示最后一個面板
