Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [Java] Errore istruzioni Try - Cacth

    Mi sto avvicinando per la prima volta al linguaggio Java, ma c'č una difficoltā che mi blocca.
    Come editor per i programmi sto usando il JBUILDER X della Borland.

    Ho provato a digitare un programma riportato su un manuale (JAVA guida alla programmazione)e si tratta del programma che simula il gioco del Black Jack.

    Nell'esecuzione mi da un errore nel seguente blocco di codice :

    1 boolean playerTakesAHit() throws IOException {
    2 char ch = ' ';
    3 do{
    4 System.out.print("Hit or Stay: ");
    5 System.out.flush();
    6 String playersDecision = keyboardInput.readLine();
    7 try ch =playersDecision.charAt(0);
    8 catch (StringIndexOutOfBoundsException exception) ;
    9 if(ch == 'H' || ch == 'h') return true;
    10 if(ch == 'S' || ch == 's') return false;
    11 } while(true);
    12 }

    Mi da i seguenti errori:
    Nella linea 7 dice ( '}' expected )
    Nella linea 8 dice ( "catch" without "try" )
    Sempre nella 7 dice ( "try" without "catch" or "finaly" at line 7


    Potete aiutarmi ? Grazie anticipate

    Nel caso vi occorra vi sottoscrivo l'intero programma anche se č molto lungo

    codice:
    import java.lang.System; 
    import java.lang.Integer; 
    import java.lang.NumberFormatException; 
    import java.io.DataInputStream; 
    import java.io.IOException; 
    import java.util.Random; 
    
    class BlackJack { 
    public static void main (String args[]) throws IOException { 
    // Create a BlackJackGame object ... 
    BlackJackGame game = new BlackJackGame(); 
    // and play it! 
    game.play(); 
    } 
    } 
    
    class BlackJackGame { 
    // Variable declarations 
    int bet; 
    int money; 
    Deck deck; 
    Hand playersHand; 
    Hand dealersHand; 
    DataInputStream keyboardInput; 
    
    // Method declarations 
    public BlackJackGame() { // Constructor 
    bet = 0; 
    money = 1000; 
    deck = new Deck(); 
    keyboardInput = new DataInputStream(System.in); 
    } 
    
    void play() throws IOException { 
    System.out.println("Welcome to Blackjack!"); 
    System.out.println("You have $"+Integer.toString(money)+"."); 
    do { 
    placeBet(); 
    if(bet>0) { 
    initialDeal(); 
    if(playersHand.blackjack()) playerWins(); 
    else{ 
    while(playersHand.under(22) && playerTakesAHit()) { 
    playersHand.addCard(deck.deal()); 
    playersHand.show(false,false); 
    } 
    while(dealersHand.mustHit()) 
    dealersHand.addCard(deck.deal()); 
    dealersHand.show(true,false); 
    showResults(); 
    } 
    } 
    } while (bet>0); 
    } 
    
    void placeBet() throws IOException, NumberFormatException { 
    do{ 
    System.out.print("Enter bet: "); 
    System.out.flush(); 
    bet = Integer.parseInt(keyboardInput.readLine()); 
    } while(bet<0 || bet>money); 
    } 
    
    void initialDeal() { 
    System.out.println("New hand..."); 
    playersHand = new Hand(); 
    dealersHand = new Hand(); 
    for(int i = 0;i<2;++i) { 
    playersHand.addCard(deck.deal()); 
    dealersHand.addCard(deck.deal()); 
    } 
    dealersHand.show(true,true); 
    playersHand.show(false,false); 
    } 
    
    void playerWins() { 
    money += bet; 
    System.out.println("Player wins $"+Integer.toString(bet)+"."); 
    System.out.println("Player has $"+Integer.toString(money)+"."); 
    } 
    
    void dealerWins() { 
    money -= bet; 
    System.out.println("Player loses $"+Integer.toString(bet)+"."); 
    System.out.println("Player has $"+Integer.toString(money)+"."); 
    } 
    
    void tie() { 
    System.out.println("Tie."); 
    System.out.println("Player has $"+Integer.toString(money)+"."); 
    } 
    
    boolean playerTakesAHit() throws IOException { 
    char ch = ' '; 
    do{ 
    System.out.print("Hit or Stay: "); 
    System.out.flush(); 
    String playersDecision = keyboardInput.readLine(); 
    try ch =playersDecision.charAt(0); 
    catch (StringIndexOutOfBoundsException exception) ; 
    if(ch == 'H' || ch == 'h') return true; 
    if(ch == 'S' || ch == 's') return false; 
    } while(true); 
    } 
    
    void showResults() { 
    if(playersHand.busted() && dealersHand.busted()) tie(); 
    else if(playersHand.busted()) dealerWins(); 
    else if(dealersHand.busted()) playerWins(); 
    else if(playersHand.bestScore() > dealersHand.bestScore()) playerWins(); 
    else if(playersHand.bestScore() < dealersHand.bestScore()) dealerWins(); 
    else tie(); 
    } 
    } 
    
    // End of BlackJackGame class 
    
    class Deck { 
    // Variable declarations 
    int cards[]; // Array of 52 cards 
    int topCard; // 0-51 (index of card in deck) 
    Random random; 
    
    // Method declarations 
    public Deck() { // Constructor 
    cards = new int[52]; 
    for(int i = 0;i<52;++i) cards[i] = i; 
    topCard = 0; 
    random = new Random(); 
    shuffle(); 
    } 
    
    public void shuffle() { 
    // Repeat 52 times 
    for(int i = 0;i<52;++i) { 
    // Randomly exchange two cards in the deck. 
    int j = randomCard(); 
    int k = randomCard(); 
    int temp = cards[j]; 
    cards[j] = cards[k]; 
    cards[k] = temp; 
    } 
    } 
    
    int randomCard() { 
    int r = random.nextInt(); 
    if(r<0) r = 0-r; 
    return r%52; 
    } 
    
    Card deal() { 
    if(topCard>51) { 
    shuffle(); 
    topCard = 0; 
    } 
    Card card = new Card(cards[topCard]); 
    ++topCard; 
    return card; 
    } 
    } // End of Deck class 
    
    class Hand { 
    // Variable declarations 
    int numCards; 
    Card cards[]; 
    static int MaxCards = 12; 
    
    //Method declarations 
    public Hand() { // Constructor 
    numCards = 0; 
    cards = new Card[MaxCards]; 
    } 
    
    void addCard(Card c) { 
    cards[numCards] = c; 
    ++numCards; 
    } 
    
    void show(boolean isDealer,boolean hideFirstCard) { 
    if(isDealer) System.out.println("Dealer:"); 
    else System.out.println("Player:"); 
    for(int i = 0;i<numCards;++i) { 
    if(i == 0 && hideFirstCard) System.out.println(" Hidden"); 
    else System.out.println(" "+cards[i].value+" of "+cards[i].suitsuite); 
    } 
    } 
    
    boolean blackjack() { 
    if(numCards == 2) { 
    if(cards[0].iValue == 1 && cards[1].iValue == 10) return true; 
    if(cards[1].iValue == 1 && cards[0].iValue == 10) return true; 
    } 
    return false; 
    } 
    
    boolean under(int n) { 
    int points = 0; 
    for(int i = 0;i<numCards;++i) points += cards[i].iValue; 
    if(points<n) return true; 
    else return false; 
    } 
    
    int bestScore() { 
    int points = 0; 
    boolean haveAce = false; 
    for(int i = 0;i<numCards;++i) { 
    points += cards[i].iValue; 
    if(cards[i].iValue == 1) haveAce = true; 
    } 
    if(haveAce) { 
    if(points+10 < 22) points += 10; 
    } 
    return points; 
    } 
    
    boolean mustHit() { 
    if(bestScore()<17) return true; 
    else return false; 
    } 
    
    boolean busted() { 
    if(!under(22)) return true; 
    else return false; 
    } 
    } // End of Hand class 
    
    class Card { 
    // Variable declarations 
    int iValue; // Numeric value corresponding to card. 
    String value; // "A" "2" through "9" "T" "J" "Q" "K" 
    String suitsuite; // "S" "H" "C" "D" 
    
    // Method declarations 
    public Card(int n) { // Constructor 
    int iSuitSuite = n/13; 
    iValue = n%13+1; 
    switch(iSuitSuite) { 
    case 0: 
    suitsuite = "Spades"; 
    break; 
    case 1: 
    suitsuite = "Hearts"; 
    break; 
    case 2: 
    suitsuite = "Clubs"; 
    break; 
    default: 
    suitsuite = "Diamonds"; 
    } 
    if(iValue == 1) value = "Ace"; 
    else if(iValue == 10) value = "Ten"; 
    else if(iValue == 11) value = "Jack"; 
    else if(iValue == 12) value = "Queen"; 
    else if(iValue == 13) value = "King"; 
    else value = Integer.toString(iValue); 
    if(iValue>10) iValue = 10; 
    } 
    
    int getValue() { 
    return iValue; 
    } 
    } }

  2. #2
    Utente di HTML.it L'avatar di MMarzia
    Registrato dal
    Mar 2001
    Messaggi
    1,781
    che bisogno c'era di aprirne un'altra? :master:
    io sono festosamente cicciottello :: e. cartman

    t'amo senza sapere come, nč quando nč da dove,
    t'amo direttamente senza problemi nč orgoglio:
    cosė ti amo perchč non so amare altrimenti

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.