如果声明不适用于Getter和Setter方法

时间:2019-05-11 20:59:14

标签: 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,因为我想累加玩家得到的所有数字,并且一旦有人在Menu.Java类的第37行中选择“ HIT”,它就会在第26行调用一个名为“ handValuePlayerCheck”的方法,以检查当前值的纸牌如果高于21,则游戏结束,然后将第14行中名为setLose的布尔值的setter方法更改为true,但由于某些原因,if语句拒绝输出System.out.println(“ YOU LOST “);并且也不会将布尔值更改为true,从而使游戏在Menu.java类下的第55行的menu()方法内结束。

我已经尝试了所有方法,例如不使用setter或getter方法,还尝试使用“ this”。方法,但这似乎也不起作用,是否有人知道为什么它似乎不更新称为“ lose”的布尔值?

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

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

  • 也尝试使用此方法。但它似乎并没有改变任何东西。

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

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

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

            }





        }


    }


}



3 个答案:

答案 0 :(得分:2)

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

一种是您的主要方法,另一种是您的Dealer类。

因此,当您添加Value时,要在Dealer类中将其一二相加。

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

当您在菜单类的handValuePlayerCheck函数中打印值时,您会看到此信息。

答案 1 :(得分:0)

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

主要问题是,您有两个菜单实例。在您的Dealer实例中创建一个实例进行检查并打印菜单,并创建一个实例,以增加价值。

但是您还创建了多个Dealer实例。您在主目录中创建了一个,以后不再使用...

您应该清楚定义所需的实例以及彼此之间需要的实例...因此,可以在main内部创建实例并通过setter设置实例变量,而不是在构造函数中创建Dealer和Menu的新实例。 / p>

答案 2 :(得分:0)

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

您应该做的是将menu()方法从Menu类移至Dealer类并对其进行一些编辑。

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