目前我正在编写一个介绍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(){”with“public static void main(String [] args){”仍然返回错误:“找不到主类。”:/
http://img21.imageshack.us/img21/9016/asdfsdfasdfg.jpg
我能够通过将Main.java更改为TheATMGame.java然后从ATMGame.java运行来修复它。
答案 0 :(得分:3)
您应该使用:
public static void main(String[] args)
而不是Main,因为JVM首先调用此方法。这是一个惯例。
答案 1 :(得分:3)
你刚刚错误定义了主要内容。应该是:
public static void main(String[] args) {
....
}
但是,您的代码会遇到其他一些问题。快速浏览......
ATM class (balance, annualInterestRate2, id2)
的用途,但它不是有效的Java。答案 2 :(得分:3)
您的方法签名必须是:
public static void main(String[] args) {
...
}
这是你必须遵循的惯例。其他任何东西都行不通。
答案 3 :(得分: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) {
答案 4 :(得分:0)
公共类的名称应与文件名匹配。屏幕截图中的情况并非如此。还要确保所有内容都正确编译然后重试(使用public static void main(String [] args){...}方法。)