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

If statement not working with Getter and Setter Methods

我目前正在与包括我在内的 3 人一起开发主机游戏,他们正在为学校项目 "Black jack" 创建游戏,但我们在 java 中的程序中遇到问题class 'Menu.java' 每当玩家在第 63 行的 switch 语句中选择 "HIT" 时,我创建了一个方法来检查卡的当前值,然后调用名为 playerGetCard() 的方法Dealer.java class 然后调用方法 n.addValue(x);在第 143 行和 returns 中,它进入 Menu.java class 然后将 Dealer.java 中给出的随机卡的值的数量加起来 setter在 class Menu.java 中的第 22 行中重命名为 addValue 的方法,因为我想把玩家得到的所有数字相加,并且一旦有人在 Menu.Java class 在第 37 行它调用一个名为 "handValuePlayerCheck" 的方法在第 26 行检查卡片的当前值是否超过 21 如果超过则游戏结束并更改名为的布尔值"setLose" 将第 14 行的 setter 方法设置为 true,但由于某种原因 if 语句拒绝输出 System.out.println("YOU LOST");并且也不会将布尔值更改为 true,这会在 Menu.java class.

下第 55 行的 menu() 方法中结束游戏

我已经尝试了所有方法,例如不使用 setter 或 getter 方法,还尝试使用 "this." 方法,但这似乎也不起作用,是吗任何人都知道为什么这似乎没有更新名为 "lose"?

的布尔值

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



}

Menu.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;
                }

            }





        }


    }


}



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

一个在您的主要方法中,一个在您的 Dealer 中 class。

因此,当您添加价值时,您将其添加到经销商 class 中的两倍。

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

当您打印菜单中的值时,您可以看到这一点 classes handValuePlayerCheck 函数。

您的代码有点混乱。但是您有多个 类.

实例

主要问题是,您有两个菜单实例。一个正在检查,一个正在打印菜单,一个实例是在您的 Dealer 实例中创建的,它使值增加。

但是您还创建了多个 Dealer 实例。您在 main 中创建了一个以后不会使用的 ...

您应该清楚地定义您需要哪些实例以及哪些实例相互需要...因此,您可以在 main 中创建实例并通过设置器设置实例变量,而不是在构造函数中创建 Dealer 和 Menu 的新实例.

好吧,在您的 main 方法中,您声明了一个 Menu 和一个实例化两个 Menu 对象的 Dealer 对象。同样在您的 menu() 方法中,您正在创建一个新的 Dealer 实例。

你应该做的是将你的 menu() 方法从你的 Menu class 移动到 Dealer class 并稍微编辑一下。

Menu.java

//the other methods and instance variables
//you should move your menu() method, I just commented it out
//for your conveniance

/*
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;
                }
            }
        }
    }
*/

Dealer.java

//the other methods and instance variables

public void menu() {
        Scanner i = new Scanner(System.in);
        Player p = new Player();
        System.out.println("Welcome to black jack!");
        Cards(); //formerly, 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 (m.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:
                        m.handValuePlayerCheck();
                        dealCard(); //formerly d.dealCard()
                        playerGetCard(); //formerly d.playerGetCard()
                        break;
                }

            }
        }
    }

你的主要方法应该是这样的;

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