1

目前我正在为一个介绍性的 Java 类编写一个程序。我的拼图有两块。希望这是一个相对简单的问题。

首先,这是我试图用作我的主程序的内容:

import java.util.Scanner;

public class TheATMGame
{
 public static void main(String[] args){


    Scanner input = new Scanner(System.in);


    double newBalance = 0;
    double monthlyInterest = 0;
    int answer = 0;


    int i=1;
    while (i < 100) {
        System.out.print ("Please enter your ID: ");
        answer = input.nextInt();
        System.out.println(" ");
        if (answer >=0 && answer<10) 
        TheATMGame.runGame (answer);
        else
         System.out.println("Sorry, this ID is invalid.");
     }
    }


    public static void runGame(int id) {
      double amount = 0;
      int continueOn = 0;
      while (continueOn < 4) {
        ATMGame myATM = new ATMGame();
        Scanner input = new Scanner(System.in);
        System.out.println ("---Main menu--- ");
        System.out.println ("1: Check balance ");
        System.out.println ("2: Withdraw ");
        System.out.println ("3: Deposit ");
        System.out.println ("4: exit ");

        int answer = input.nextInt();

        if (answer == 1)
            System.out.println("your balance is: " + myATM.getBalance (id));
        else if (answer == 2){
            System.out.println("Enter an amount to withdraw: ");
            amount = input.nextInt();
            myATM.withdraw(amount, id);
        }
        else if (answer == 3)
{
            System.out.println("Enter an amount to deposit: ");
            amount = input.nextInt();
            myATM.deposit(amount, id);
        }
        else if (answer == 4)
            continueOn = 4;
        else if (answer > 4)
            System.out.println ("Please review the main menu. " +
                    "Your selection must be between 1-4.");
      }
    }

//ATM class (balance, annualInterestRate2, id2)
//ATM myATM = new ATM (20000, 4.5, 1122 );
//newBalance = myATM.withdraw(2500);
//newBalance = myATM.deposit(3000);
//monthlyInterest = myATM.getMonthlyInterestRate();
//System.out.println("Your current balance is: " + newBalance);
//System.out.println ("Your monthly interest rate is: " +  monthlyInterest);



}

现在这里是我想在该程序中隐含的所有类:

import java.util.Date;

public class ATMGame  {

    private double annualInterestRate = 0;
    private double balance = 0;
    private int id = 11;
    private int[] ids = {0,1,2,3,4,5,6,7,8,9};
    private int[] balances = {100,100,100,100,100,100,100,100,100,100};
    public Date dateCreated;


    public ATMGame() {

    }
    public ATMGame (double balance2, double annualInterestRate2, int id2) {
        balance = balance2;
        annualInterestRate = annualInterestRate2;
        id = id2;
        dateCreated.getTime();
    }


    public double getMonthlyInterestRate() {
        double monthlyInterest = annualInterestRate/12;
        return monthlyInterest;
    }


    public double withdraw(double amountWithdrawn, int id) { //This method withdraws money from the account

        double newBalance = balances[id] - amountWithdrawn;
        System.out.println("Your withdrawel has processed. New balance: " + newBalance);
        balances[id] = (int) newBalance;
        return newBalance ;

    }


    public double deposit(double amountDeposited, int id) { //This method deposits money in the account
        double newBalance = balances[id] + amountDeposited;
        System.out.println("Your deposit has processed. New Balance is: " + newBalance);
        balances[id] = (int) newBalance;
        return newBalance ;

    }

    public double getBalance(int id) {
        double myBalance = balances[id];
        balance = myBalance;
        return myBalance ;
    }


}

当我尝试运行第一个程序时,它显示“未找到主类”。正如你所看到的,我写了“public void Main() ...”这一行来解决这个问题,但显然它不起作用。我究竟做错了什么?

用“public static void main(String[] args) {”替换“public void Main() {”仍然返回错误:“No Main classes found”。:/

http://img21.imageshack.us/img21/9016/asdfsdfasdfg.jpg

我可以通过将 Main.java 更改为 TheATMGame.java 然后从 ATMGame.java 运行来修复它。

4

5 回答 5

3

你应该使用:

public static void main(String[] args)

而不是 Main,因为 JVM 首先调用此方法。这是一个约定。

于 2009-05-05T15:36:45.583 回答
3

你只是错误地定义了 main。应该:

public static void main(String[] args) {
    ....
}

但是,您的代码会遇到其他一些问题。一眼看去...

  • 您的 main() 和 runGame() 都是一个函数 - 一个不应在另一个中定义。
  • 你不能将你的两个类命名为相同的东西——将 main() 类称为不同于 ATMGame 的东西。
  • 不确定你要去哪里ATM class (balance, annualInterestRate2, id2),但它不是有效的 Java。
于 2009-05-05T15:36:58.487 回答
3

您的方法签名必须是:

public static void main(String[] args) {
 ...
}

这是你必须遵守的约定。别的什么都行不通。

于 2009-05-05T15:37:43.943 回答
1

在您的班级ATMGame中,替换以下内容:

public void Main() {

和:

public static void main(String[] args) {

此外,由于此方法必须是静态的,因此您需要更改以下内容:

if (answer >=0 && answer<10)
     runGame (answer);
 else

和:

if (answer >=0 && answer<10)
     ATMGame.runGame (answer);
 else

最后,您需要将方法签名更改rungame为也是静态的。将其更改为:

public void runGame(int id) {

到:

public static void runGame(int id) {
于 2009-05-05T15:43:50.497 回答
0

公共类的名称应与文件名匹配。在您的屏幕截图中不是这种情况。还要确保一切都正确编译,然后重试(使用 public static void main(String[] args) { ... } 方法)。

于 2009-05-05T16:20:00.817 回答