我無法正確初始化變量。之前我得到一個數字格式異常,因爲我正在解析一個空文本字段內的整數。現在我創建了一個if語句,首先檢查該字段是否爲空,然後解析其中的整數。我現在遇到的問題是我的事件處理程序無法識別變量,因爲它在if語句中。我試圖在if語句之外聲明它,但那也不起作用。任何提示指向我在正確的方向?這裏是我的代碼:使用事件處理程序找不到符號錯誤
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicReference;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
ArrayList<Integer> deck;
deck = new ArrayList<>();
int i = 1;
while(i < 52){
deck.add(i);
i++;
}
final AtomicReference<String> result = new AtomicReference<>("go.");
Collections.shuffle(deck);
BorderPane pane = new BorderPane();
HBox top = new HBox(10);
Label display = new Label(result.toString());
Button btShuffle = new Button("Shuffle");
btShuffle.setOnAction(
e -> {
Collections.shuffle(deck);
});
top.getChildren().add(display);
top.getChildren().add(btShuffle);
HBox center = new HBox(10);
Card card1 = new Card(deck.get(0));
center.getChildren().add(card1);
Card card2 = new Card(deck.get(1));
center.getChildren().add(card2);
Card card3 = new Card(deck.get(2));
center.getChildren().add(card3);
Card card4 = new Card(deck.get(3));
center.getChildren().add(card4);
HBox bottom = new HBox(10);
Label expression = new Label("Please Enter the expression: ");
TextField tfExpress = new TextField();
LinkedList<Object> expInput = new LinkedList<>();
ArrayList<Character> signs = new ArrayList<>();
signs.add('/');
signs.add('+');
signs.add('(');
signs.add(')');
signs.add('-');
signs.add('^');
signs.add('*');
signs.add('%');
String str = tfExpress.getText();
char tempStor[] = str.toCharArray();
for(char c: tempStor){
expInput.add(c);
}
if(tfExpress.getText() != null && tfExpress.getText().equals(""))
{
int express = Integer.parseInt(str);
}
expInput.removeIf(p-> p.equals(signs));
Button btVerify = new Button("Verify");
btVerify.setOnAction(
(ActionEvent e) -> {
if(card1.CardValue() == (int)expInput.get(0)
&& card2.CardValue() == (int)expInput.get(1)
&& card3.CardValue() == (int)expInput.get(2)
&& card4.CardValue() == (int)expInput.get(3)){
if(express == 24){
result.set("Correct");
}
else
result.set("Incorrect");
}
else
result.set("The numbers in the expression don't "
+ "match the numbers in the set.");
});
pane.setTop(top);
pane.setCenter(center);
pane.setBottom(bottom);
Scene scene = new Scene(pane);
primaryStage.setTitle("24 card game");
primaryStage.setScene(scene);
primaryStage.show();
}
public class Card extends Pane {
public int cardVal;
Card(int card){
Image cardImage;
cardImage = new Image("card/"+ card +".png");
cardVal = card;
}
public int CardValue(){
int card = 0;
if(cardVal <= 13){
card = cardVal;
}
else if(cardVal > 13 && cardVal <= 26){
card = cardVal - 13;
}
else if(cardVal > 26 && cardVal <= 39){
card = cardVal - 26;
}
else if(cardVal > 39 && cardVal <= 52){
card = cardVal - 39;
}
return card;
}
}
public static void main(String[] args) {
launch(args);
}
}
同樣的問題,因爲我在下面發佈這個。 「錯誤:從lambda表達式引用的局部變量必須是最終的或有效的最終」。 –
@JohnDoe我更新了答案。 –
非常感謝!那麼爲什麼認爲這個符號在我的代碼中更好?我非常感興趣。 –