0

さて、私の Java クラスの最終プロジェクトでは、10 ラウンド実行する単純化されたバージョンのクラップスを実装することになっています。これを実行すると、「int b=std.nextInt()」という行で NoSuchElementsException が発生します。何故ですか?スキャナー オブジェクトなどを開きましたが、ゲームを続行するためのデータを入力できず、代わりに例外がスローされます。「System.out.println(now.toString());」でも同じ例外が発生します。メインメソッドの行。要素がないのはどうしてですか?

public class Player {
private int bet;//how much was bet
private boolean Pass;//they chose pass
private boolean DPass;//they chose dont pass
private boolean win;//did they win?
private int money=20;//how much they have
//private String continuity="initial";

//Modifier methods
public void newBet(int x){this.bet=x;}
public void Pass(boolean x){this.Pass=x;}
public void DPass(boolean x){this.DPass=x;}
public void didYouWin(boolean x){this.win=x;}
public void newMoney(int x){this.money+=x;}
//public void keepPlaying(String s){this.continuity=s;}

//Accessor methods
public int getBet(){return this.bet;}
public boolean getPass(){return this.Pass;}
public boolean getDPass(){return DPass;}
public boolean getResult(){return this.win;}
public int getMoney(){return this.money;}
public boolean isWinning(){return this.win;}
//public String playing(){return continuity;}
public String toString(){
    return "Bet: $"+this.bet+"\nBet on Pass: "+this.Pass+"\nBet on Don't Pass: "+this.DPass+"\nMoney:S"+this.money;
}

//Constructor method
public Player(int bet, boolean pass, boolean dpass){
    this.bet=bet;
    this.Pass=pass;
    this.DPass=dpass;
}

}

実際のゲームプレイコード<<<<<<<<<<<<<<<<<<<<<<<<

import java.util.*;

公開クラストライアル1 {

public static int RollDice(){           //Method for Dice Roll
    int[] die1={1, 2, 3, 4, 5, 6};
    int[] die2={1, 2, 3, 4, 5, 6};
    Random r=new Random();
    int i1=r.nextInt(6-0);
    int i2=r.nextInt(6-0);
    int sum=die1[i1]+die2[i2];
    System.out.println("\nDie 1: "+die1[i1]+"\nDie 2: "+die2[i2]+"\nTotal Sum: "+sum);
    return sum; 
}

public static int Roll7(){//roll for 7
    if (RollDice()==7){
        return 2;
    }
    else return Roll7();
}

public static int PointRoll(int x){//If person rolled 4,5,6,8,9,10...
    int a=RollDice();
    if (a==x){
        return Roll7();
    }
    else if (a==7){
        return 1;
    }
    else return PointRoll(x);
}

public static int ComeOutRoll(){//1 = pass loses, 2 = pass wins, 3 = pass loses and dont pass gets nothing
    int x=RollDice();
    if ((x==2)||(x==3)) {
        return 1;
        }
    else if ((x==7)||(x==11)){
        return 2;
    }
    else if (x==12){
        return 3;
    }
    else return PointRoll(x);
}
public static Player InitializeGame(){
    //initialize stats and player
    System.out.println("Please enter how much you'd like to bet (max is $5)");
    Scanner std=new Scanner(System.in);
    int b=std.nextInt();
    System.out.println("Please enter 1 if you bet PASS or 2 if you bet DON'T PASS");
    int p=std.nextInt();
    boolean betpass, betdpass;
    if (p==1){
        betpass=true;
        betdpass=false;
    }
    else {
        betpass=false;
        betdpass=true;
    }
    Player name=new Player(b, betpass, betdpass);
    System.out.print(name.toString());
    std.close();
    return name;
}
public static Player BeginGame(Player name){
    //Start actual game process without the betting ie all the dice rolling and stat changing -->will return player's status
    //boolean pass=name.getPass();
    //boolean neutral=false;
    int result=ComeOutRoll();
    //find out if player won money or lost money
    if (name.getPass()){//if player bet on pass
        if (result==1){
            name.newMoney(name.getMoney()-name.getBet());
        }
        else if (result==2){
            name.newMoney(name.getMoney()+name.getBet());
        }
        else {
            name.newMoney(name.getMoney()-name.getBet());
        }
    }
    else {//if player bet dont pass
        if (result==1){
            name.newMoney(name.getMoney()+name.getBet());
        }
        else if (result==2){
            name.newMoney(name.getMoney()-name.getBet());
        }
        else {
            name.didYouWin(false);
        }

    }
    if (name.getMoney()<=0){name.didYouWin(false);}//setting win data for yes or no. IF no money, u lose
    else {name.didYouWin(true);}

public static Player Continue(Player name){//just like begin game, but adding the new bet
    System.out.println("\nPlease enter how much you'd like to bet (max is $5)");
    Scanner std=new Scanner(System.in);
    int b=std.nextInt();
    System.out.println("Please enter 1 if you bet PASS or 2 if you bet DON'T PASS");
    int p=std.nextInt();
    boolean betpass, betdpass;
    if (p==1){
        betpass=true;
        betdpass=false;
    }
    else {
        betpass=false;
        betdpass=true;
    }
    name.Pass(betpass);
    name.DPass(betdpass);
    name.newBet(b);
    System.out.println(name.toString());
    return BeginGame(name);
}
public static void Loss(Player name){//losing message
    System.out.println("YOU LOSE!!!!!!!! HAHAHAHAHAHA!!!!!\n"+name.toString());
}
public static void End(Player name){//End game message
    System.out.println("Thank you for playing!");
}

public static Player Run(){
    Player name = InitializeGame();
    return BeginGame(name);
}

public static void main(String[] args){
    System.out.println("Welcome to my version of craps!");
    Player now=Run();
    for (int i=1;i<=10;i++){
        if (now.isWinning()){
            System.out.println("ROUND "+i);
            System.out.println(now.toString());
            now=Continue(now);
            i++;
        }
        else {
            Loss(now);
            System.out.print(now.toString());
            End(now);
            i=11;
        }
    }
}

}

4

2 に答える 2

0

NoSuchElementException から、スキャナーから int を読み取っていないように見えます。入力を他のデータ型として解釈しています。

于 2013-05-09T14:58:25.923 に答える
0

intNoSuchElementsException は、スキャナからを取得しようとしたstdが、次の がなかったことを意味しますint。したがって、このエラーをスローして、入力が間違っていることを知らせます。

于 2013-05-09T14:54:34.067 に答える