我自己做了一个BlackJack游戏,但程序的一部分没有执行

时间:2014-04-27 10:39:20

标签: java input blackjack

问题在于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"); 
}
}

1 个答案:

答案 0 :(得分:0)

  1. String[] args作为参数添加到main。 public static void main(String[] args)
  2. 从main中删除throws。你不能让main扔东西。无处可扔。
  3. 初始化您使用的所有变量。例如你没有初始化userchoice。在开头插入行char userchoice = 0;
  4. 您的代码现在应该可以使用。