Java中的类和方法的数组

时间:2012-02-16 00:26:41

标签: java arrays class methods

好的,所以我做了一个小小的任务,需要一些类的数组,并调用各种方法来完成工作。基本上,我希望通过这个程序学习一点,它的想法是创建一个客户帐户,存款,取款等。到目前为止我已经写了这个代码,我收到了下面给出的错误。请以任何可能的方式帮助我。并且var ++不是允许在Java中增加数字的选项吗?

import java.util.*;

public class Bank
    {
        int cId;
        String cName;
        float cBalance;
    }
class Banking
{
public static void main(String args[])
{


    int limit=1;
    System.out.println(+limit);
    while (limit==1)
    {
    System.out.println("Menu:");
    System.out.println("1. Add Customer");
    System.out.println("2. View Customer Details");
    System.out.println("3. Banking Options");
    System.out.println("Enter choice:");
    Scanner scan = new Scanner (System.in);
    int i=scan.nextInt();
    switch (i)
    {
        case 1:
            {
                System.out.println("Add the customer");
                Customer();
            }
        /*case 2:
            {
                System.out.println("Customer details");
                getCustomer();
            }
        case 3:
            {
                System.out.println("Banking options");
                System.out.println("1. Deposit");
                System.out.println("2. Withdraw");
                System.out.println("3. Balance Enquiry");
                Scanner scan1 = new Scanner (System.in);
                int j=scan1.nextInt();
                switch (j)
                {
                    case 1:
                        {
                                                           System.out.println("Deposit");
                            deposit();
                        }
                    case 2:
                        {
                                                           System.out.println("Withdraw");
                            withdraw();
                        }
                    case 3:
                        {
                                                           System.out.println("Balance Enquiry");
                            balance();
                        }
                }
            }
    }   */
    System.out.println("Enter 0 to exit or 1 to continue operations");
    Scanner s = new Scanner (System.in);
    limit=scan.nextInt();
    }
}
}
}
public static void Customer (String []a)
{
    int cust=0;
    Bank c[cust] = new Bank();
    System.out.println("Enter customer ID:");
    Scanner scan11 = new Scanner (System.in);
    int k=scan11.nextInt();
    c[cust].cId=k;
    System.out.println("Enter customer name:");
    Scanner scan12 = new Scanner (System.in);
    String name=scan12.next();
    c[cust].cName=name;
    System.out.println("Enter starting balance:");
    Scanner scan132 = new Scanner (System.in);
    float bal=scan132.nextFloat();
    c[cust].cBalance=bal;

    System.out.print("Customer id=" +c[cust].cId );
    cust++;


}

}

错误在这里给出:

bank.java:73: class, interface, or enum expected
        public static void Customer (String []a)
                      ^
bank.java:76: class, interface, or enum expected
                Bank c[cust] = new Bank();
                ^
bank.java:77: class, interface, or enum expected
                System.out.println("Enter customer ID:");
                ^
bank.java:78: class, interface, or enum expected
                Scanner scan11 = new Scanner (System.in);
                ^
bank.java:79: class, interface, or enum expected
                int k=scan11.nextInt();
                ^
bank.java:80: class, interface, or enum expected
                c[cust].cId=k;
                ^
bank.java:81: class, interface, or enum expected
                System.out.println("Enter customer name:");
                ^
bank.java:82: class, interface, or enum expected
                Scanner scan12 = new Scanner (System.in);
                ^
bank.java:83: class, interface, or enum expected
                String name=scan12.next();
                ^
bank.java:84: class, interface, or enum expected
                c[cust].cName=name;
                ^
bank.java:85: class, interface, or enum expected
                System.out.println("Enter starting balance:");
                ^
bank.java:86: class, interface, or enum expected
                Scanner scan132 = new Scanner (System.in);
                ^
bank.java:87: class, interface, or enum expected
                float bal=scan132.nextFloat();
                ^
bank.java:88: class, interface, or enum expected
                c[cust].cBalance=bal;
                ^
bank.java:90: class, interface, or enum expected
                System.out.print("Customer id=" +c[cust].cId );
                ^
bank.java:91: class, interface, or enum expected
                cust++;
                ^
bank.java:94: class, interface, or enum expected
        }
        ^
17 errors

非常感谢你们!

4 个答案:

答案 0 :(得分:1)

关于:

bank.java:73: class, interface, or enum expected
        public static void Customer (String []a)

通常这意味着查看上面的行。您可能缺少分号或者花括号过多或过少。确保您的花括号准确对齐,良好的代码格式和缩进将帮助您完成此操作。顺便说一句,你的格式很糟糕,这有助于你找不到你的错误。

这一行:Bank c[cust] = new Bank(); 没有任何意义。你想在这做什么?

最重要的是:阅读教程并学习基础知识。一次添加少量代码并经常编译。在解决所有编译问题之前不要添加任何新代码,否则你最终只能得到一个老鼠的错误。

答案 1 :(得分:0)

平衡你的牙箍,这样你就可以获得相同数量的开口和闭合牙箍。
当你得到class or enum expected时,这通常是个问题。

答案 2 :(得分:0)

删除这两个右括号

}  // <--
}  // <--
public static void Customer (String []a)
{

并为不同的错误做好准备..

答案 3 :(得分:0)

代码格式不正确,但我的猜测是在

之前有一个或多个不必要的大括号
public static void Customer (String []a)

编译器基本上告诉您将代码置于类定义(或接口或枚举)之外,但这在java中是不可能的。所有代码必须位于类(或接口或枚举)中。