gpt4 book ai didi

java - Yahtzee游戏主要方法

转载 作者:行者123 更新时间:2023-12-02 04:52:16 25 4
gpt4 key购买 nike

我在 Java 中运行 Yahtzee 游戏时似乎遇到了一些问题。我在代码中不断收到“未找到主方法”错误,即使我在代码中之前指定了它。任何帮助,将不胜感激。

导入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');
}
}

最佳答案

您是否尝试过分离类,例如拥有一个主类。将所有内容都放入一个文件中并不是一种很好的做法,而且我不相信 java 喜欢在另一个类中拥有一个主类,这是迫在眉睫的问题。您还使用 IDE 或某种构建实用程序吗?以及哪个特定的 IDE 或构建实用程序?如果您确实希望所有内容都在一个类中,请在主方法之前删除类 header

关于java - Yahtzee游戏主要方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com