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 看来,您没有从 Scanner 读取 int。它将您的输入解释为其他数据类型。

于 2013-05-09T14:58:25.923 回答
0

NoSuchElementsException 表示您试图int从扫描仪中获取 an std,但没有 next int。所以它会抛出这个错误,让你知道你的输入是错误的。

于 2013-05-09T14:54:34.067 回答