Java 方法和类,它们如何组合在一起?

发布于 2024-07-19 03:23:20 字数 4211 浏览 5 评论 0原文

目前我正在为 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) {”仍然会返回错误:“未找到主类。” :/

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

我能够修复通过将 Main.java 更改为 TheATMGame.java,然后从 ATMGame.java 运行。

Currently I am writing a program for an introductory Java class. I have two pieces to my puzzle. Hopefully this is a relatively simple to answer question.

Firstly, here is what I am trying to use as my main program:

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);



}

Now here are all of the classes I want to impliment into that program:

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 ;
    }


}

When I try to run the first program it says "No Main classes found."
As you can see I have written the line " public void Main() ..." to take care of this, but eveidently it does not work. What am I doing wrong?

Replacing "public void Main() {" with "public static void main(String[] args) {" still returns the error: "No Main classes found." :/

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

I was able to fix it by changing Main.java to TheATMGame.java and then running from ATMGame.java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美人迟暮 2024-07-26 03:23:20

您应该使用:

public static void main(String[] args)

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

You should use:

public static void main(String[] args)

Instead of Main because the JVM calls this method first. It is a convention.

夏见 2024-07-26 03:23:20

你刚刚错误定义了 main. 应该是:

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

不过,您的代码将会遇到一些其他问题。 快速浏览一下...

  • 您的 main() 是一个函数,以及 runGame() - 一个不应该在另一个中定义。
  • 您不能将两个类命名为相同的名称 - 将 main() 类命名为与 ATMGame 不同的名称。
  • 不确定您要使用 ATM 类(余额、annualInterestRate2、id2),但它不是有效的 Java。

You've just misdefined main. Should be:

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

You're going to run into some other problems with your code, though. From a quick glance...

  • Your main() is a function, as well as runGame() - one shouldn't be defined within the other.
  • You cannot name your two classes the same thing - call the main() class something different than ATMGame.
  • Not sure where you're going with ATM class (balance, annualInterestRate2, id2), but it's not valid Java.
一百个冬季 2024-07-26 03:23:20

您的方法签名必须是:

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

这是您必须遵循的约定。 其他任何事情都行不通。

Your method signature must be:

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

That's the convention you have to follow. Anything else won't work.

终陌 2024-07-26 03:23:20

在您的类 ATMGame 中,将以下内容替换

public void Main() {

为:

public static void main(String[] args) {

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

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

with:

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

最后,您需要更改rungame 也是静态的。 将其从: 更改

public void runGame(int id) {

为:

public static void runGame(int id) {

In your class ATMGame, replace the following:

public void Main() {

with:

public static void main(String[] args) {

Additionally, since this method has to be static, you'll need to change the following:

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

with:

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

Then finally, you need to change the method signature of rungame to also be static. Change it from:

public void runGame(int id) {

to:

public static void runGame(int id) {
你是暖光i 2024-07-26 03:23:20

公共类的名称应与文件名匹配。 您的屏幕截图中并非如此。 还要确保所有内容都正确编译,然后重试(使用 public static void main(String[] args) { ... } 方法)。

The names of your public classes should match the file names. This is not the case in your screenshot. Also make sure that everything compiles correctly and then retry (using a public static void main(String[] args) { ... } method).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文