java - If 语句不适用于 Getter 和 Setter 方法

标签 java if-statement boolean getter-setter

我目前正在与包括我在内的 3 个人一起开发一款控制台游戏,他们正在为学校项目创建游戏“Black jack”,但我们在 java 类“Menu.java”中的程序中遇到了问题在第 63 行的 switch 语句中创建了一个方法,用于在每次玩家选择“HIT”时检查卡牌的当前值,然后调用 Dealer.java 类中名为playerGetCard() 的方法,该方法随后调用方法 n.addValue( X);在第 143 行中,并将其返回到 Menu.java 类中,该类然后在 setter 方法中将 Dealer.java 中给出的随机卡的值的数量相加,该方法在类 Menu.java 中的第 22 行中重命名为 addValue,因为我想将给定玩家的所有数字相加,一旦有人在第 37 行的 Menu.Java 类中选择“HIT”,它就会调用第 26 行中名为“handValuePlayerCheck”的方法,该方法检查当前值是否为如果牌的点数超过 21,则游戏结束,并在第 14 行将 setter 方法中名为“setLose”的 boolean 值更改为 true,但由于某种原因,if 语句拒绝输出 System.out.println("YOU LOST ”);并且也不会将 boolean 值更改为 true,这会在 Menu.java 类下第 55 行的 menu() 方法中结束游戏。

我已经尝试了一切,例如不使用 setter 或 getter 方法,并尝试使用“this”。方法,但这似乎也不起作用,有谁知道为什么这似乎没有更新名为“lose”的 boolean 值?

  • 尝试不使用 Getter/Setter 方法。

  • 将 Getter/Setter 方法更改为 WinOrLose.java 类,但它不起作用,也尝试将其放在同一个类中,该类首先检查第 26 行 Menu.Java 类中的数字

  • 也尝试过使用 this.(variableName);但似乎并没有改变任何事情。

  • 尝试重命名变量,但似乎没有任何变化。

  • 还尝试检查变量是否覆盖同名的值,但我发现没有任何内容可以覆盖任何内容。

Main.java

package com.tony;
public class Main {

    public static void main(String[] args) {
        Dealer d = new Dealer();
        Menu menu = new Menu();
        d.Cards();
        menu.menu();


    }

}


Dealer.java

package com.tony;
import java.util.ArrayList;
import java.util.Random;
import java.util.HashMap;
public class Dealer {
    String cardName;
    private int randomInt;
    private int value;
    private WinOrLose wl = new WinOrLose();
    private Menu m = new Menu();
    private HashMap<Integer, String> hmap = new HashMap<Integer, String>();

    public void Cards(){
        int counter = 1;

        for(int i = 0; i<9; i++){
            counter++;
            hmap.put(i, counter + " of diamonds");
        }
        counter = 1;
        for(int i = 9; i<18; i++){
            counter++;
            hmap.put(i, counter + " of clubs");
        }
        counter = 1;
        for(int i = 18; i<27; i++){
            counter++;
            hmap.put(i, counter + " of hearts");
        }
        counter = 1;
        for(int i = 27; i<36; i++){
            counter++;
            hmap.put(i, counter + " of spades");
        }

        for(int i = 36; i<37; i++){
            hmap.put(i, "Ace of diamonds");
        }

        for(int i = 37; i<38; i++){
            hmap.put(i, "Ace of clubs");
        }

        for(int i = 38; i<39; i++){
            hmap.put(i, "Ace of hearts ");
        }

        for(int i = 39; i<40; i++){
            hmap.put(i, "Ace of spades");
        }

        for(int i = 40; i<41; i++){
            hmap.put(i, "Jack of diamonds");
        }

        for(int i = 41; i<42; i++){
            hmap.put(i, "Jack of clubs");
        }

        for(int i = 42; i<43; i++){
            hmap.put(i, "Jack of hearts");
        }

        for(int i = 43; i<44; i++){
            hmap.put(i, "Jack of spades");
        }

        for(int i = 44; i<45; i++){
            hmap.put(i, "Queen of spades");
        }

        for(int i = 45; i<46; i++){
            hmap.put(i, "Queen of diamonds");
        }

        for(int i = 46; i<47; i++){
            hmap.put(i, "Queen of clubs");
        }
        for(int i = 47; i<48; i++){
            hmap.put(i, "Queen of hearts");
        }

        for(int i = 48; i<49; i++){
            hmap.put(i, "King of spades");
        }

        for(int i = 49; i<50; i++){
            hmap.put(i, "King of diamonds");
        }

        for(int i = 50; i<51; i++){
            hmap.put(i, "King of clubs");
        }

        for(int i = 51; i<52; i++){
            hmap.put(i, "King of hearts");
        }
    }

    public void dealCard(){ // Once hit has been selected this will randomly generate a number 
        this.randomInt = (int) (Math.random()*52+1);
        setCardName(hmap.get(this.randomInt));
    }

    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }


    public void playerGetCard() { // that card and it's value and adds it to m.addValue();
        ArrayList<String> player = new ArrayList<String>();
        player.add(getCardName());


        if(getCardName().contains("Ace")){
            System.out.println("This is an " + getCardName());
            //Check players total card number if goes over add 1
            //if doesn't add 10
            if(m.getValue() <= 11){
                m.addValue(11);
            }else{
                m.addValue(1);
            }


        }else if(getCardName().contains("Jack") || getCardName().contains("Queen") || getCardName().contains("King")) {
            System.out.println("This is a " + getCardName());
            // Check max in winorlose
            m.addValue(10);
        }else {
            int n = Integer.parseInt(getCardName().substring(0, 1));
            System.out.println(n);
            m.addValue(n);
            // check max in winorlose
            System.out.println("Name of random card is " + getCardName());
        }
        System.out.println("Your total hand value is: " + m.getValue());
    }



}

菜单.java

package com.tony;

import java.util.Scanner;


public class Menu {
    private boolean lose;
    private int value;

    public boolean isLose() {
        return lose;
    }

    public void setLose(boolean lose) {
        this.lose = lose;
    }

    public int getValue() {
        return value;
    }

    public void addValue(int value) { // This takes the value of the randomly given card and adds it here.
        this.value = this.value + value;
    }

    public void handValuePlayerCheck(){ // Checks if the value of the cards that were given goes over 21 if it does then change boolean lose to false and display "You lost"
        System.out.println(isLose());
        if(this.value > 21){
            System.out.println("YOU LOST");
            setLose(true);
        }



    }

    public void menu(){
        Scanner i = new Scanner(System.in);
        Player p = new Player();
        Dealer d = new Dealer();
        System.out.println("Welcome to black jack!");
        d.Cards();
        System.out.println("What is your name?");
        p.setName(i.nextLine());
        System.out.println("Let's Begin!");
        System.out.println();
        System.out.println("1: Hit");
        System.out.println("2: Split");
        System.out.println("3: Hold");
        System.out.println("4: Double Down");
        System.out.println("5: Surrender");
        System.out.println("Your turn!");

        while(true){
            if (isLose()) {
                System.out.println("You lost! Try again!");
                break; // IF lose variable is set to TRUE then the game will end using the break command and display "You lost! Try Again!"
            }


            boolean has = i.hasNextInt();
            if (has) {
                switch (i.nextInt()) { //This calls the methods case 1: is the "HIT" option.
                    case 1:
                        handValuePlayerCheck();
                        d.dealCard();
                        d.playerGetCard();
                        break;
                }

            }





        }


    }


}



最佳答案

您正在使用菜单类的两个不同实例。 (两个独立的对象)

一个在您的 main 方法中,一个在您的 Dealer 类中。

因此,当您添加 Value 时,您将其添加到 Dealer 类中的两个值中。

但是菜单类中的值始终保持为 0。

当您在 Menu 类 handValuePlayerCheck 函数中打印值时,您可以看到这一点。

关于java - If 语句不适用于 Getter 和 Setter 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56094233/

相关文章:

java - 在 Java 中动态添加带有属性文件消息的字符串?

java - 将 Gradle 添加到 Java 项目 "Exception ... java.lang.NoClassDefFoundError"

改变字体颜色的 if 语句的 PHP 多个条件

c# - 绑定(bind) boolean 值

java - 正则表达式仅匹配一次,而不是全部

java - 具有 NIO channel 的简单客户端-服务器程序

bash - 如何使用通配符在 if 语句中指定存在条件?

c - opencl在内核中迭代数组

Python all([6,7,8,9]) = True。但是 6 = 假

ios - 使用 NSPredicate 按 boolean 属性过滤 NSManagedObjects