简体   繁体   English

使用事件处理程序找不到符号错误

[英]Cannot Find Symbol Error with event handler

I am having trouble trying to initialize a variable correctly. 我在尝试正确初始化变量时遇到麻烦。 Earlier I was getting a Number Format Exception because I was parsing an integer inside of an empty text field. 早些时候我遇到了数字格式异常,因为我正在解析一个空文本字段内的整数。 Now I have created an if statement that checks first to see if the field is empty, then parses an integer inside of it. 现在,我创建了一个if语句,该语句首先检查该字段是否为空,然后解析其中的整数。 The problem i'm having now is that my event handler can't recognize the variable, because it is inside the if statement. 我现在遇到的问题是我的事件处理程序无法识别该变量,因为它位于if语句内。 I tried declaring it outside of the if statement but that didn't work either. 我尝试在if语句之外声明它,但这也不起作用。 Any tips to point me in the right direction? 有什么提示可以指示我正确的方向吗? Here is my code: 这是我的代码:

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);
   }
}

Instead of 代替

if(tfExpress.getText() != null && tfExpress.getText().equals(""))
{
     int express = Integer.parseInt(str);
}

try 尝试

final int express = (tfExpress.getText() != null && !tfExpress.getText().equals("")) : Integer.parseInt(str) : 0;

If str being empty is an acceptable behavior for your code, you need to define a default value for express if that is the case. 如果您的代码可以接受str为空,那么您需要为express定义一个默认值。 If the default value were -1 , for example, you would do: 例如,如果默认值为-1 ,则可以执行以下操作:

int express = (str != null && !"".equals(str)) ? Integer.parseInt(str) : -1;

And then later, you should treat the -1 case accordingly. 然后,您应该相应地处理-1情况。

You need to initialize express like this, so it is effectively final and can be used in the lambda expression. 您需要像这样初始化express ,因此它实际上是最终的,可以在lambda表达式中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM