【问题标题】:I have made a BlackJack game myself but a part of the program does not execute我自己做了一个二十一点游戏,但程序的一部分没有执行
【发布时间】:2014-04-27 10:39:19
【问题描述】:

问题是在 User2 之间的输入不被接受?

 public static void main() throws IOException
 {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      int user1total = 0, user2total=0, i;
      char userchoice;

      System.out.println("Welcome to the game of BlackJack!!!");
      System.out.println("User1 will be drawing two cards at a time");
      System.out.println("User2 will also be drawing two cards at a time");
      System.out.println("User1 shall win if User1's total reaches 21 or the User2's total exceeds 21");
      System.out.println("User2 shall win if User1's total reaches 21 or the User1's total exceeds 21");

      for(i=0;i<=4;i++)
      {
          System.out.println("User1's Total ="+user1total);
          System.out.println("User2's Total ="+user2total);

          if(user1total>21)
          {
              System.out.println("User2 wins as User1's total has exceeded 21");
              System.exit(0);
          }

          if(user2total>21)
          {
              System.out.println("User1 wins as User2's total has exceeded 21");
              System.exit(0);
          }

          if(user1total==21)
          {
              System.out.println("User2 wins as his total is 21");
              System.exit(0);
          }

          if(user2total==21)
          {
              System.out.println("User1 wins as his total is 21");
              System.exit(0);
          }
          System.out.println("Want to take two more cards, User1???");
          System.out.println("Enter Y or N");
          userchoice = Character.toUpperCase( (char)in.read() );
          if (userchoice=='Y')
          user1total=user1total+randomnumbersforuser();

          System.out.println("User1's Total ="+user1total);
          System.out.println("User2's Total ="+user2total);

          System.out.println("Want to take two more cards, User2???");
          System.out.println("Enter Y or N");
          userchoice = Character.toUpperCase( (char)in.read() ); ***//This part does not take input at all***
          if (userchoice=='Y')
          user2total=user2total+randomnumbersforuser();

          System.out.println("User1's Total ="+user1total);
          System.out.println("User2's Total ="+user2total);
          }
      if(user1total>user2total)
      System.out.println("User1 wins as his final total is greater than User2's total");
      if(user2total>user1total)
      System.out.println("User2 wins as his final total is greater than User1's total"); 
}
}

【问题讨论】:

  • 我建议你学习调试。
  • 不要让任何人告诉你因为你才 14 岁就不能做某事!不过说真的,如果人们使用您不熟悉的术语,Google it!
  • 是的,debug
  • @GoldRoger:这个答案与这个问题无关。
  • @Yuki 不,这正是问题所在..\n 输入键是从第一个 read 留下的,第二个 read 使用它而不是用户输入..无论如何我给了链接扫描仪..删除它

标签: java input blackjack


【解决方案1】:
  1. String[] args 作为参数添加到main。 public static void main(String[] args)
  2. 从主目录中删除 throws。你不能让main 扔东西。无处可扔。
  3. 初始化您使用的所有变量。例如你没有初始化userchoice。在开头插入行char userchoice = 0;

您的代码现在应该可以工作了。

【讨论】:

  • 1.String[] args 仅在需要方法调用窗口时才需要,我不需要。 2.throws 是一个关键字,用于将 IOException 与 main 链接,所以我需要它。 3.并且用户选择不需要设置为零,因为值会直接更改为用户输入。即使我要实例化它,我也会实例化为“”而不是 0。
猜你喜欢
  • 2022-01-19
  • 2023-03-21
  • 2020-05-18
  • 1970-01-01
  • 2013-10-08
  • 2014-10-22
  • 2022-11-12
  • 2017-12-01
  • 2015-06-02
相关资源
最近更新 更多