当我运行带有Scanner对象的Java程序时,Windows等待光标总是在那里。我怎样才能改变这个?
我有一个Java游戏,你可以玩21点,我用一个扫描器来获取诸如“点击”和“停留”之类的输入。当我运行它时,它会显示出来,直到我关闭程序才会消失。我尝试使用Scanner的close()方法,但这会引发异常 代码如下:当我运行带有Scanner对象的Java程序时,Windows等待光标总是在那里。我怎样才能改变这个?,java,java.util.scanner,Java,Java.util.scanner,我有一个Java游戏,你可以玩21点,我用一个扫描器来获取诸如“点击”和“停留”之类的输入。当我运行它时,它会显示出来,直到我关闭程序才会消失。我尝试使用Scanner的close()方法,但这会引发异常 代码如下: import java.util.Scanner; import java.util.ArrayList; public class CardGame { public static ArrayList<Card> dealIn(ArrayList<
import java.util.Scanner;
import java.util.ArrayList;
public class CardGame {
public static ArrayList<Card> dealIn(ArrayList<Card> hand) {
Card a = new Card();
Card b = new Card();
hand.add(a);
while (hand.size() < 2) {
if (!b.equals(a)) {
hand.add(b);
} else
dealIn(hand);
}
return hand;
}
public static int calcScore(ArrayList<Card> hand) {
int sum = 0;
int aces = 0;
for (Card c : hand) {
if (c.getCardValue().getFace().equals("A")) {
aces++;
} else {
sum += c.getCardValue().getNumeric();
}
}
while (aces > 0) {
aces--;
if (sum <= 10) {
sum += 11;
} else {
sum += 1;
}
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Blackjack!\n");
ArrayList<Card> playerhand = new ArrayList<Card>();
ArrayList<Card> dealerhand = new ArrayList<Card>();
dealIn(playerhand);
for (int i = 0; i < 2; i++) {
Card a = new Card();
if ((!playerhand.contains(a)) && (!dealerhand.contains(a))) {
dealerhand.add(a);
}
}
// System.out.println("DEALER HAND: [hidden]" + " and " +
// dealerhand.get(1));
System.out.println("TEST DEALER HAND: " + dealerhand);
System.out.println("Your hand: " + playerhand);
while (!isBusted(playerhand)) {
System.out.println("Your score is " + calcScore(playerhand) + ". Hit or stay?");
String hOrS = in.nextLine();
if (hOrS.equalsIgnoreCase("hit")) {
hit(playerhand);
System.out.println(playerhand);
if (isBusted(playerhand)) {
System.out.println("Your score is " + calcScore(playerhand) + "! YOU LOSE");
break;
}
// System.out.println(playerhand);
}
if (hOrS.equalsIgnoreCase("stay")) {
while (calcScore(dealerhand) <= 17) {
Card n = new Card();
if (!dealerhand.contains(n)) {
dealerhand.add(n);
// System.out.println(dealerhand);
}
}
}
if (isBusted(dealerhand) || (!isBusted(playerhand) && (calcScore(playerhand) > calcScore(dealerhand)))) {
System.out.println("DEALER HAND: " + dealerhand);
System.out.println("YOUR HAND: " + playerhand);
System.out.println("YOU WIN");
// break;
} else {
System.out.println(dealerhand);
System.out.println("YOU LOSE");
break;
}
}
}
}
import java.util.Scanner;
导入java.util.ArrayList;
公开课纸牌游戏{
公共静态ArrayList处理(ArrayList手动){
卡a=新卡();
卡b=新卡();
另加(a);
while(hand.size()<2){
如果(!b等于(a)){
另加(b);
}否则
交易(手);
}
回手;
}
公共静态int calcScore(ArrayList手动){
整数和=0;
int aces=0;
用于(卡c:手){
如果(c.getCardValue().getFace().equals(“A”)){
aces++;
}否则{
sum+=c.getCardValue().getNumeric();
}
}
而(aces>0){
王牌--;
if(sum Wait)光标通常表示您的应用程序正忙,这通常发生在您有无限循环时。但在这种情况下,在每个迭代中,控制流都应该在.nextLine()中的处停止,所以您可能在CalcCore(playerhand)处进入无限循环
method。它是否也包含一些循环?如果没有.OT,不能说更多,但您需要检查readLine()
result for null.OK我已经添加了整个class@EJP它是Scanner#nextLine
而不是readLine
,因此它不能返回null
。如果没有更多数据要读取,则不应引发任何TouchElementException。