2015-04-06 80 views
1

所以我搞亂了這段代碼,我不會包含所有的文件,但是有一個Yahtzee.java文件,DiePanel.java,ScorePanel.java和YahtzeeHand.java文件,並且它們都不是有一個main()方法。我得到啓動錯誤,選擇不包含主類型。我覺得它應該可以運行嗎?我錯過了什麼?不包含主類型; Yahtzee

import java.awt.*; 
import javax.swing.*; 
import java.util.*; 

// ----------------------------- 
// The GraphicalYahtzee class 
// ----------------------------- 

class GraphicalYahtzee extends JFrame { 

    // -------------------------- 
    // class-oriented data 
    // -------------------------- 

    static final int 
    FRAME_WIDTH = 600, FRAME_HEIGHT = 600, 
    SCORE_LEFT_MARGIN = 50, SCORE_TOP_MARGIN = 50, 
    SCORE_WIDTH = 220, SCORE_HEIGHT = 320, 
    DICE_LEFT_MARGIN = 50, DICE_TOP_MARGIN = 400, 
    DICE_WIDTH = 38, DICE_HEIGHT = 38, 
    DICE_BORDER = 2, DICE_PADDING = 5; 

    // -------------------------- 
    // object-oriented data 
    // -------------------------- 

    // the content pane associated with the JFrame 
    Container cPane; 

    // the Yahtzee game 
    Yahtzee yahtz; 

    // an array of panels constituting the DICE 
    DiePanel[] dice = new DiePanel[5]; 

    // the panel for displaying the scores 
    ScorePanel scorePanel; 


    /*EDDDITTT*//// 

    public static void main(String []args){ 

    } 
    // ----------------------- 
    // constructor 
    // ----------------------- 
    GraphicalYahtzee() { 

    this.yahtz = new Yahtzee(); 

    // set the size of the onscreen window 
    this.setSize(FRAME_WIDTH, FRAME_HEIGHT); 
    this.setTitle("GRAPHICAL YAHTZEE!"); 
    // get a reference to the "content pane" from the JFrame 
    // store it in the "cPane" field 
    this.cPane = this.getContentPane(); 
    // tell the "content pane" that we will provide absolute 
    // coordinates for all panels to be added to it 
    this.cPane.setLayout(null); 

    this.scorePanel = new ScorePanel(yahtz); 
    // specify the location and size of the scorePanel 
    this.scorePanel.setBounds(SCORE_LEFT_MARGIN, 
           SCORE_TOP_MARGIN, 
           SCORE_WIDTH, 
           SCORE_HEIGHT); 
    // ask the "contentPane" to add the new scorePanel 
    // to its contents. 
    this.cPane.add(this.scorePanel); 

    // set up the DICE panels 
    for (int i=0; i<5; i++) { 
     // create a new DiePanel 
     this.dice[i] = new DiePanel(yahtz, i); 
     // specify the location and size of the new panel 
     int topLeft = DICE_LEFT_MARGIN 
     + i*(DICE_WIDTH + DICE_BORDER + DICE_PADDING); 
     this.dice[i].setBounds(topLeft, DICE_TOP_MARGIN, 
          DICE_WIDTH, DICE_HEIGHT); 
     // ask the "contentPane" to add the new panel to 
     // its contents. 
     this.cPane.add(this.dice[i]); 
    } 

    // Make the JFrame visible 
    this.setVisible(true); 

    // call the playGame function! 
    this.playGame(); 
    } 

    // getValidCategory 

    int getValidCategory() { 
    // set up connection to keyboard 
    Scanner sc = new Scanner(System.in); 
    // kat will eventually hold the category entered by the player 
    int kat = 0; 
    // need is a boolean variable that governs the while loop 
    boolean need = true; 
    while (need) { 
     // prompt the user to type something 
     System.out.println("Enter a category to score your hand!" 
          + " (or -1 to quit)"); 
     // get an int value from the user 
     kat = sc.nextInt(); 
     // check whether it is a 
     if ((kat == -1) 
      || ((kat >= 1) 
        && (kat <= 6) 
        && (yahtz.isValidCategory(kat)))) 
     need = false; 
    } 
    // now that we're out of the while loop, we know we have a good categ. 
    return kat; 
    } 

    // getValidKeeperString 

    String getValidKeeperString() { 

    // set up connection to keyboard 
    Scanner sc = new Scanner(System.in); 
    // str will eventually hold the string to return 
    String str = ""; 
    // need governs the while loop 
    boolean need = true; 
    while (need) { 
     // prompt the user to enter something 
     System.out.println("Enter a valid keeper string (5 chars) " 
          + "... or -1 to quit)"); 
     // get the next String from the keyboard 
     str = sc.next(); 
     // if the string is okay, set need to false so we can break 
     // out of the while loop 
     if (str.equals("-1") || str.length() == 5) need = false; 
    } 
    return str; 
    } 

    // playGame 

    void playGame() { 

    yahtz.resetGame(); 

    // outer for loop: 6 turns (one category filled for each turn) 
    for (int i=0; i<6; i++) { 
     // ask the yahtzee object to create a hand for the next turn 
     YahtzeeHand h = yahtz.getHandForNextTurn(); 
     repaint(); 
     // the player gets two chances to re-roll the dice 
     for (int roll = 0; roll < 2; roll++) { 
     // get a valid keeper string 
     String keeperStr = getValidKeeperString(); 
     // check if player wants to quit 
     if (keeperStr.equals("-1")) { 
      System.out.println("Okay... fine... ABORTING GAME!"); 
      return; 
     } 
     else { 
      // ask the yahtzeehand object to roll the dice selected by player 
      h.rollEm(keeperStr); 
      repaint(); 
     } 
     } 
     // get a category from the player 
     int cat = getValidCategory(); 
     // check if player wants to quit 
     if (cat == -1) { 
     System.out.println("Okay, fine... ABORTING GAME!"); 
     return; 
     } 
     else { 
     // ask the yahtzee object to score the hand in the selected category 
     yahtz.scoreHand(cat); 
     repaint(); 
     } 
    } 
    System.out.println("Well... you're done! Final Score: " + 
         yahtz.computeTotalScore()); 
    } 

} 
+0

只需創建您自己的'public static void main(String [] args)'您在哪裏創建'GraphicalYahtzee'對象 – gtgaxiola

回答

1

的入口點所有Java程序的主要方法:

public static void main(String[] args) { 
    GraphicalYahtzee game = new GraphicalYahtzee(); 
    game.playGame(); 
} 

沒有它,你的程序將無法運行。你的問題是你有一個主要的方法,它沒有被發現,或者你真的想要省略主要的方法嗎?

+0

我可以在哪裏插入主要方法?我假定代碼是寫成可運行的。所以我想我不是在試圖避免它,我只是不知道它可以去哪裏。 –

+0

將它添加到GraphicalYahtzee.java並執行:java GraphicalYahtzee –

+0

我添加了它,就像我在編輯中顯示的那樣,是什麼意思?因爲現在它運行,但之後立即完成 –

0

Terik,

如在Java documentation過多規定:

In Java, you need to have a method named main in at least one class. 

此外,

This method must appear within a class, but it can be any class. 

JVM Specs在理解加載,鏈接一個巨大的資源,並初始化步驟涉及在執行main。

如果您有任何問題,請讓我知道!

+0

謝謝,我現在明白這一點。我不知道如何在代碼中實現main()方法。你能否指點我正確的方向? –