Java 不包含主类型;雅特泽
因此,我正在处理这段代码,我不会包括所有的文件,但是有一个Yahtzee.java文件、DiePanel.java、ScorePanel.java和YahtzeeHand.java文件,它们都没有main()方法。我得到启动错误,选择不包含主类型。我觉得它无论如何都应该可以运行?我错过了什么Java 不包含主类型;雅特泽,java,Java,因此,我正在处理这段代码,我不会包括所有的文件,但是有一个Yahtzee.java文件、DiePanel.java、ScorePanel.java和YahtzeeHand.java文件,它们都没有main()方法。我得到启动错误,选择不包含主类型。我觉得它无论如何都应该可以运行?我错过了什么 import java.awt.*; import javax.swing.*; import java.util.*; // ----------------------------- // The
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());
}
}
import java.awt.*;
导入javax.swing.*;
导入java.util.*;
// -----------------------------
//GraphicalYahtzee类
// -----------------------------
类GraphicalYahtzee扩展了JFrame{
// --------------------------
//面向类的数据
// --------------------------
静态最终整数
框架宽度=600,框架高度=600,
分数左差=50,分数上差=50,
分数宽度=220,分数高度=320,
骰子左边缘=50,骰子上边缘=400,
骰子宽度=38,骰子高度=38,
骰子边框=2,骰子填充=5;
// --------------------------
//面向对象数据
// --------------------------
//与JFrame关联的内容窗格
集装箱cPane;
//Yahtzee游戏
雅特泽雅特兹;
//组成骰子的一组板
模具面板[]骰子=新模具面板[5];
//用于显示分数的面板
计分板计分板;
/*艾迪特*////
公共静态void main(字符串[]args){
}
// -----------------------
//建造师
// -----------------------
GraphicalYahtzee(){
this.yahtz=新的Yahtzee();
//设置屏幕窗口的大小
此.setSize(框宽、框高);
这个.setTitle(“图形YAHTZEE!”);
//从JFrame获取对“内容窗格”的引用
//将其存储在“cPane”字段中
this.cPane=this.getContentPane();
//告诉“内容窗格”,我们将提供绝对
//要添加到其中的所有面板的坐标
this.cPane.setLayout(null);
this.scorePanel=新的scorePanel(yahtz);
//指定记分板的位置和大小
此.scorePanel.setBounds(分数左边缘,
得分(上)(下)(下),
分数宽度,
得分(身高);
//要求“contentPane”添加新的scorePanel
//它的内容。
this.cPane.add(this.scorePanel);
//设置骰子面板
对于(int i=0;i=1)
&&(kat所有Java程序的入口点都是主要方法:
public static void main(String[] args) {
GraphicalYahtzee game = new GraphicalYahtzee();
game.playGame();
}
如果没有它,您的程序将无法运行。您的问题是您有一个main方法,并且找不到它,还是您真的想省略main方法?Terik
如以下内容中所述:
此外,
This method must appear within a class, but it can be any class.
在理解执行main所涉及的加载、链接和初始化步骤方面,是一个很好的资源
如果您有任何问题,请告诉我!只需创建自己的公共静态void main(String[]args)
在哪里创建GraphicalYahtzee
object我可以在哪里插入main方法?我假设编写的代码是可运行的。所以我想我并不是在试图避免它,我只是不确定它可以去哪里。将它添加到GraphicalYahtzee.java并执行:java GraphicalYahtzee添加了它,正如我在编辑中所示,这就是你想要的吗an?因为现在,它会运行,但随后会立即结束。在主方法的主体中,您希望实例化GraphicsYahzee对象。类似于:GraphicsYahzee game=new GraphicsYahzee();谢谢,我现在明白了。我不确定如何实现main()方法导入到代码中。你能给我指出正确的方向吗?
This method must appear within a class, but it can be any class.