【问题标题】:Java methods and classes, how do they fit together?Java 方法和类,它们如何组合在一起?
【发布时间】:2009-05-05 15:35:24
【问题描述】:

目前我正在编写一个介绍 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 void Main() {”替换为“public static void main(String[] args){”仍然返回错误:“No Main classes found”。:/

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

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

【问题讨论】:

  • LOL 三个相同的答案在一分钟内发布。为大家 +1。
  • 看起来你有 2 个同名的类。那是对的吗?如果是这样,那肯定是个问题。

标签: java class methods main-method


【解决方案1】:

你应该使用:

public static void main(String[] args)

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

【讨论】:

  • 用“public static void main(String[] args) {”替换“public void Main() {”仍然返回错误:“No Main classes found”。
【解决方案2】:

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

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

不过,您的代码还会遇到一些其他问题。一眼看去……

  • 您的 main() 和 runGame() 都是一个函数 - 不应在另一个中定义一个。
  • 您不能将两个类命名为相同的名称 - 将 main() 类称为不同于 ATMGame 的名称。
  • 不确定ATM class (balance, annualInterestRate2, id2) 的用途,但它不是有效的 Java。

【讨论】:

  • 修复主要问题后,程序运行正常。 “ATM 类(余额,年利率 2,id2)”有什么问题?我应该如何改变它以使其更好?
【解决方案3】:

您的方法签名必须是:

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

这是您必须遵守的约定。其他的都行不通。

【讨论】:

    【解决方案4】:

    在您的班级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) {
    

    【讨论】:

    • 感谢您的帮助。我已尝试修复您提到的问题,但我仍然面临同样的错误:“未找到主类。”
    • 我正在用 netbeans 编译它。这是结构的图像:img21.imageshack.us/img21/9016/asdfsdfasdfg.jpg
    • 啊,我可以通过将 Main.java 更改为 TheATMGame.java 然后从 ATMGame.java 运行来修复它。感谢您的帮助!
    • 在 Java 中,如果你有一个公共类,文件的名称必须是“TheActualNameOfTheClass.java”。在您的屏幕截图中,这就是 NetBeans 为类名加下划线的原因——公共类是 TheATMGame,文件名是 Main.java。此外,由于这个原因,Java 中每个文件只能有一个公共类。
    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2015-01-26
      • 1970-01-01
      相关资源
      最近更新 更多