如果语句不使用Getter和Setter方法



我目前正在与包括我自己在内的3个人组成的组合游戏,他们正在为学校项目创建游戏" Black Jack",但我们在该计划中有一个问题Java class'菜单。然后调用方法n.addvalue(x(;在第143行中并将其返回菜单。Java类,然后在setter方法中添加了经销商中给出的随机卡的值数量我想添加播放器给出的所有数字,并且一旦有人在菜单中选择" hit"。第37行中的java类,它将在第26行中称为" handvalueplayercheck"的方法,以查看当前值是否是否该卡的命中在21上面的命中率,如果这样做的话,游戏结束并将名为" setlose"的布尔人更改为第14行中的true,但是由于某种原因,IF语句拒绝输出System.out.out.println("您丢失了"(;而且,也不会将布尔值更改为true,true在菜单下的第55行菜单((方法中结束了游戏。java类。

我已经尝试了所有不使用二阶或Getter方法的所有内容,并尝试使用" this"。方法但这似乎也不起作用,有人知道为什么这似乎没有更新称为"丢失"的布尔值?

  • 尝试不使用getter/setter方法。

  • 将getter/setter方法更改为winorlose.java类,但它也不起作用,也尝试将其放在同一类中,该类将数字在菜单中首先检查数字。/p>

  • 也尝试使用此。(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;
                }
            }


        }

    }

}

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

您的主要方法中的一个,在您的经销商类中。

因此,当您添加值时,您将其添加两个是经销商类中的一个。

但是您的菜单类中的值总是保持0。

当您在菜单类中打印值时,您可以看到这一点。

您的代码有些混乱。但是您有多个课程的实例。

主要问题是,您有两个菜单实例。一个执行检查并打印菜单的一个,在您的经销商实例中创建一个实例,该实例增加了值。

但是您还创建了多个经销商实例。您在Main中创建了一个,以后不使用...

您应该清楚地定义您需要的实例以及彼此需要的实例...因此,您可以在构造函数内创建新的经销商和菜单的新实例,而是可以在main内部创建实例,并通过setters设置实例变量。<<<<<<<<<<<<

好吧,在您的主要方法中,您可以声明菜单和一个实例化两个菜单对象的经销商对象。同样在您的菜单方法中,您正在创建一个新的经销商实例。

您应该做的是将您的菜单((方法从菜单类移动到经销商类并进行编辑。

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

最新更新