无法获取识别卡值的程序
Can't Get Program To Recognize Card Values
我的程序不会进入这个代码块
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){
display.setText("Correct");
}
else
display.setText("Incorrect");
}
我认为这是因为当出现的牌只是我牌组中的随机牌时,我试图获得 card1
的牌值。我可以用什么来评估出现的新卡?这是我的完整代码:
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 javafx.scene.image.ImageView;
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");
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(3));
center.getChildren().add(card3);
Card card4 = new Card(deck.get(4));
center.getChildren().add(card4);
btShuffle.setOnAction(
e -> {
Collections.shuffle(deck);
center.getChildren().clear();
center.getChildren().add(new Card(deck.get(0)));
center.getChildren().add(new Card(deck.get(1)));
center.getChildren().add(new Card(deck.get(2)));
center.getChildren().add(new Card(deck.get(3)));
});
HBox bottom = new HBox(10);
Label expression = new Label("Please Enter the expression: ");
TextField tfExpress = new TextField();
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();
int express = (str != null && !"".equals(str)) ? Integer.parseInt(str) : -1;
// expInput.removeIf(p-> p.equals(signs));
Button btVerify = new Button("Verify");
bottom.getChildren().add(expression);
bottom.getChildren().add(tfExpress);
bottom.getChildren().add(btVerify);
btVerify.setOnAction(
(ActionEvent e) ->
{
LinkedList<Character> expInput = new LinkedList<Character>();
for(char c: tfExpress.getText().toCharArray()){
expInput.add(c);
}
expInput.removeIf(p-> p.equals(signs));
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){
display.setText("Correct");
}
else
display.setText("Incorrect");
}
else
display.setText("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");
getChildren().add(new ImageView(cardImage));
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);
}
}
您正在将数字的字符值与整数值进行比较。那些不匹配。为了使这一点在视觉上清晰:
String numbers = "0123456789";
for(char c: numbers.toCharArray()){
System.out.println( c);
}
产生
0 1 2 3 4 5 6 7 8 9
但是如果像在程序中那样将字符值转换为整数,您将得到这个
48 49 50 51 52 53 54 55 56 57
你的字符的ascii值是多少。
换句话说:对于字符“1”和卡值 1,如果 1 == 49,则比较。
解决方案:你需要解析字符而不是将它们转换为int,e。 G。使用
Character.getNumericValue(...)
一般说明:找出代码问题的最简单方法是使用调试器。或者,一个简单的日志输出会告诉你哪里出了问题。
编辑:仅供参考,你有一个错误:
int i = 1;
while(i < 52){
deck.add(i);
i++;
}
会导致最后一张牌丢失。
我的程序不会进入这个代码块
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){
display.setText("Correct");
}
else
display.setText("Incorrect");
}
我认为这是因为当出现的牌只是我牌组中的随机牌时,我试图获得 card1
的牌值。我可以用什么来评估出现的新卡?这是我的完整代码:
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 javafx.scene.image.ImageView;
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");
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(3));
center.getChildren().add(card3);
Card card4 = new Card(deck.get(4));
center.getChildren().add(card4);
btShuffle.setOnAction(
e -> {
Collections.shuffle(deck);
center.getChildren().clear();
center.getChildren().add(new Card(deck.get(0)));
center.getChildren().add(new Card(deck.get(1)));
center.getChildren().add(new Card(deck.get(2)));
center.getChildren().add(new Card(deck.get(3)));
});
HBox bottom = new HBox(10);
Label expression = new Label("Please Enter the expression: ");
TextField tfExpress = new TextField();
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();
int express = (str != null && !"".equals(str)) ? Integer.parseInt(str) : -1;
// expInput.removeIf(p-> p.equals(signs));
Button btVerify = new Button("Verify");
bottom.getChildren().add(expression);
bottom.getChildren().add(tfExpress);
bottom.getChildren().add(btVerify);
btVerify.setOnAction(
(ActionEvent e) ->
{
LinkedList<Character> expInput = new LinkedList<Character>();
for(char c: tfExpress.getText().toCharArray()){
expInput.add(c);
}
expInput.removeIf(p-> p.equals(signs));
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){
display.setText("Correct");
}
else
display.setText("Incorrect");
}
else
display.setText("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");
getChildren().add(new ImageView(cardImage));
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);
}
}
您正在将数字的字符值与整数值进行比较。那些不匹配。为了使这一点在视觉上清晰:
String numbers = "0123456789";
for(char c: numbers.toCharArray()){
System.out.println( c);
}
产生
0 1 2 3 4 5 6 7 8 9
但是如果像在程序中那样将字符值转换为整数,您将得到这个
48 49 50 51 52 53 54 55 56 57
你的字符的ascii值是多少。
换句话说:对于字符“1”和卡值 1,如果 1 == 49,则比较。
解决方案:你需要解析字符而不是将它们转换为int,e。 G。使用
Character.getNumericValue(...)
一般说明:找出代码问题的最简单方法是使用调试器。或者,一个简单的日志输出会告诉你哪里出了问题。
编辑:仅供参考,你有一个错误:
int i = 1;
while(i < 52){
deck.add(i);
i++;
}
会导致最后一张牌丢失。