我似乎在使用Java运行Yahtzee游戏时遇到了一些问题。我的代码中不断出现“找不到主要方法”错误,即使我在代码中早先指定了它。任何帮助,将不胜感激。
import java.util.Scanner;
public class NathanMassey_221_Midterm {
int faceValue;
public void roll(){
faceValue = (int)(Math.random() * 6 + 1);
}
public int getValue(){
return faceValue;
}
public String toString(){
return Integer.toString(faceValue);
}
}
class Yahtzee {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
introduction();
boolean runAgain = true;
while (runAgain){
boolean rolledYahtzee = false;
int numRolls = 0;
NathanMassey_221_Midterm [] dieArray = dieArray(keyboard);
while(!rolledYahtzee){
dieArray = rollDice (dieArray);
numRolls++;
printDots(numRolls);
rolledYahtzee = checkForYahtzee(dieArray, numRolls);
}
printResults (numRolls, dieArray);
runAgain = runAgain(keyboard);
}
}
public static void introduction(){
System.out.println("This program will");
}
public static NathanMassey_221_Midterm [] dieArray(Scanner keyboard){
int numDice = getInt(keyboard, "How many dice do you want to throw? ");
while (numDice <= 0){
System.out.println("Sorry, you must enter a positive number.");
numDice = getInt(keyboard, "How many dice do you want to throw? ");
}
NathanMassey_221_Midterm [] dieArray = new NathanMassey_221_Midterm [numDice];
return dieArray;
}
public static int getInt (Scanner keyboard, String prompt){
System.out.print(prompt);
while (!keyboard.hasNextInt()){
keyboard.next();
System.out.println("Sorry, you must enter an integer.");
System.out.print(prompt);
}
return keyboard.nextInt();
}
public static NathanMassey_221_Midterm [] rollDice(NathanMassey_221_Midterm [] dieArray){
for (int i = 0; i < dieArray.length; i++){
dieArray[i] = new NathanMassey_221_Midterm ();
dieArray[i].roll();
dieArray[i].faceValue = dieArray[i].getValue();
}
return dieArray;
}
public static void printDots(int numRolls){
System.out.print(".");
if ((numRolls%50) == 0){
System.out.println();
}
}
public static boolean checkForYahtzee(NathanMassey_221_Midterm[] dieArray, int numRolls) {
for(int i = 0; i < dieArray.length; i++) {
for(int j = i+1; j < dieArray.length; j++) {
if(dieArray[i].getValue() != dieArray[j].getValue()) {
return false;
}
}
}
return true;
}
public static void printResults (int numRolls, NathanMassey_221_Midterm[] dieArray){
System.out.println();
System.out.println("Yahtzee!!");
System.out.print("After " + numRolls + " rolls, I finally rolled ");
System.out.println(dieArray.length + " " + dieArray[0].getValue() + "'s");
System.out.println();
}
public static boolean runAgain (Scanner keyboard){
System.out.print("Do you want to run another experiment? (y|n)): ");
String answer = keyboard.next().trim().toLowerCase();
keyboard.nextLine();
System.out.println();
return (answer.charAt(0) == 'y');
}
}
答案 0 :(得分:2)
您是否尝试过分类,例如有一个主类。将一切都推到一个文件中并不是很好的做法我也不相信java喜欢在另一个类中拥有一个主类这是当前的问题。您还使用IDE或某种构建实用程序吗?以及哪个特定的IDE或构建实用程序?如果你确实希望一个类中的所有东西都摆脱主方法之前的类标题
答案 1 :(得分:0)
我假设您正在尝试访问公共类NathanMassey_221_Midterm的主要方法,该方法不存在。例如,如果您在Eclipse中工作,则必须自己创建启动配置并指定Yahtzee作为主类(iirc,默认情况下,单击“run”将尝试运行公共类的main方法)。 / p>