我正在使用java开发一个二十一点游戏但是在我完成(或几乎完成)编写整个程序后,J抓住了“没有找到文件中的主要方法,applet或MIDlet”。代码如下。如何让J Grasp找到主要方法。如果您发现任何其他会阻止代码运行的东西,我将如何解决其他问题。
import java.util.*;
public class Blackjack{
private int points;
private int limit;
private Scanner scan;
private boolean firstTime;
private String response;
private int outcomeOfRoll;
//*******reminder to myself: the word void in the next line of code could be incorrect**********
public Blackjack(){
scan = new Scanner (System.in);
}
public void displayPoints(){
System.out.print("Your points: " + points + "/" + limit);
}
public void startGame () {
System.out.print("Enter point limit");
limit=scan.nextInt();
displayPoints();
}
public void Roll (){
Random randomRoll = new Random();
int outcomeOfRoll = randomRoll.nextInt(6)+1;
System.out.print("You rolled a " + outcomeOfRoll);
}
public String askUser (boolean firstTime){
String response = null;
if (firstTime== true){
System.out.print("Start playing?");
response = scan.next();
return response;
}
else {
System.out.print("Keep playing?");
response = scan.next();}
return response;
}
public void displayResult(){
if (points==limit)
System.out.print("Blackjack!");
else if (points>limit)
System.out.print("Bust!");
else if (points<limit)
System.out.print("Stand at " + points + " points!");
}
public void play(){
boolean gameOver = false;
startGame();
askUser(firstTime);
while(response.equals("yes") && gameOver==false){
points = points + outcomeOfRoll;
displayPoints();
if (points>=limit)
gameOver=true;
askUser(firstTime);
displayResult();
}
}
public void main(){
play();
}
}
答案 0 :(得分:2)
在Java中,您需要在至少一个类中使用名为main的方法,并且它必须是public static void并将String数组作为参数。
Java中的主要方法如下所示:
public static void main(String[] args)
只需修改您的主要方法即可完全匹配
有关详情,建议您阅读documentation