不包含主要类型;的Yahtzee

时间:2015-04-06 18:26:09

标签: java

所以我正在搞乱这段代码,并且我不会包含所有文件,但是有一个Yahtzee.java文件,DiePanel.java,ScorePanel.java和YahtzeeHand.java文件,没有他们有一个main()方法。我得到启动错误,选择不包含主类型。我觉得它应该能够运行吗?我错过了什么?

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());
  }

}

2 个答案:

答案 0 :(得分:1)

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

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

没有它,你的程序将无法运行。你的问题是你有一个主方法,它没有找到,或者你真的想省略主方法?

答案 1 :(得分: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所涉及的加载,链接和初始化步骤的重要资源。

如果您有任何疑问,请与我们联系!