输入通过方法后,以下代码不运行

问题描述

我不确定如何表达,但我正在制作一个二十一点游戏,在玩家收到 2 张牌后,他们可以选择是否接受另一张牌。我通过一种方法将玩家的响应(y/n)放入无效输入时保持防弹和重新提示,然后将其返回到主代码,以便它运行。这是我的防弹方法代码

public class Bulletproofing {

public static String bulletproof (String another) throws IOException{
    DataInputStream input = new DataInputStream(system.in);
    
    while(!another.equalsIgnoreCase("Y")&&!another.equalsIgnoreCase("N"))
    {
        System.out.println("That was not a valid choice. Please type a 'Y' or 'N'");
        System.out.print("Would you like another card? Y/N ");
        another = input.readLine();
        
    }
    return another.toString();
    
}

}

我现在遇到的问题是bulletproofng 可以工作,但是在输入y/n 之后,玩家的卡片没有显示。这是我的主要方法代码

System.out.print("Would you like to get another card? Y/N ");
        String another = input.readLine();
        Bulletproofing.bulletproof(another);
        
        //player says yes 
        
        while(another.equalsIgnoreCase("Y") & playerTotal <=21)
        {
            //get card
            
            System.out.print(card[deck.get(nextCard)]);
            value = card[deck.get(nextCard)].getDenom();
            
            if(value==11 || value == 12 || value== 13)
            {
                value = 10;
            }
            
            playerTotal+=value;
            
            //show stats
            System.out.println();
            System.out.println("Player total " + playerTotal);
            nextCard++;
            System.out.println();
            
    //something wrong       //incase player goes over they don't have the choice to get another card
            if(playerTotal< 21)
            {
                System.out.print("Player: Another card? Y/N ");
                another = input.readLine();
                Bulletproofing.bulletproof(another);
            }
            
            else
            {
                another.equals("N");
            }
            count ++;
        }

当玩家输入无效输入时,程序会要求玩家再次输入,但是当他们输入 y 或 n 时,程序不会按照我想要的方式输出。我希望输出类似于:

  • 你想要另一张卡片吗?是/否 是

  • 7 颗钻石

  • 玩家总数:25

  • 经销商:

  • 8 颗心

  • 经销商总数:18

  • 经销商获胜。

但是,在特定情况下,例如我输入“i”然后重新提示并输入“y”,我会得到如下输出

  • 你想要另一张卡片吗?是/否我

  • 输入无效请重新输入:

  • 你想要另一张卡片吗?是/否 是

  • 经销商:

  • 8 颗心

  • 经销商总数:18

  • 经销商获胜。

在这里,玩家的卡片根本没有输出,我对如何解决这个问题有点困惑。

解决方法

那是因为在 main 中您分配给 another 输入“i”。然后您忽略防弹方法的输出。 Bulletproof 只是循环输入,但您永远不会为变量分配新值

改成这样:

String another = input.readLine();
another = Bulletproofing.bulletproof(another);

你为什么不尝试调试它?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...