1
import java.util.Scanner;

public class PayAndGoParking
{

    public static void main(String Args[])
    {
       int choice=0;
       int numCars=0;
       String Plates[]=new String[50];
       String ccNumbers[]=new String[50];
       Scanner b = new Scanner(System.in);


        choice = printMenu();
        if(choice==1) {
            numCars=addCars(Plates,ccNumbers,numCars,b);
        }

    }

    public static int printMenu()
    {
        Scanner s = new Scanner(System.in);
        int choice;

        System.out.println("Welcome to Park and Go Parking");
        System.out.println("Park from 6 - Midnight for a flat fee of $4.00");

        System.out.println("1. Register your vehicle");
        System.out.println("2. Verify vehicle registration");

        System.out.print("Hello enter a Selection: ");
        choice = s.nextInt();

        if(choice==1) {
            System.out.println("Register your vehicle");
        }
        else {
            System.out.println("Verify your registration");
        }

        return choice;
    }


   public static int addCars (String [] plates,String [ ] ccNumbers, int numCars, Scanner keyboard)
   {
        Scanner s = new Scanner(System.in);


        System.out.println("Enter your plate number: ");
        plates[numCars]=s.nextLine();

        System.out.println("Enter your Credit Card Number ($4.00 charge): ");
        ccNumbers[numCars]=s.nextLine();



        System.out.println("Thank you, your plate " +plates[numCars]+ " has been added to the lot");

        numCars++;


        return numCars;



    }

所以几乎我想要发生的事情是,我希望用户在 addCars 方法中输入的任何车牌号和 ccNumber 都存储在 main 方法中的车牌和 ccNumber 数组中。对不起,如果我听起来好像我不知道我在说什么,我对编程是全新的并且正在学习。

4

2 回答 2

3

GanGnaMStYleOverFlowErroR 谈到了您提出的直接问题,但解决您的问题的更好方法是稍微修改您的代码。您可以将它们设置为对象 PayAndGoParking 的属性,而不是将所有内容作为参数传递给您的方法。这使您可以通过将其范围更改为整个类来直接访问它们,而不仅仅是单个方法。

通常,您希望避免只是将“静态”放在所有内容上以使其正常工作。它是一个特殊的关键字是有原因的(不是因为它只是让一切正常工作:)。在下面查看我的示例代码,看看我会怎么做。随意问任何问题。

import java.util.Scanner;
public class PayAndGoParking {
    private String Plates[];
    private String ccNumbers[];
    private int numCars;

    public static void main(String Args[])
    {
        PayAndGoParking park = new PayAndGoParking();
        //set the defaults
        park.Plates = new String[50];
        park.ccNumbers =new String[50];
        park.numCars=0;

        int choice = park.printMenu();

        if(choice==1)
            park.addCars();

        //since arrays start at 0, this will print out the first license and cc number added
        System.out.println(park.Plates[0]);
        System.out.println(park.ccNumbers[0]);
    }

    public int printMenu()
    {
        Scanner s = new Scanner(System.in);
        int choice;

        System.out.println("Welcome to Park and Go Parking");
        System.out.println("Park from 6 - Midnight for a flat fee of $4.00");

        System.out.println("1. Register your vehicle");
        System.out.println("2. Verify vehicle registration");

        System.out.print("Hello enter a Selection: ");
        choice = s.nextInt();

        if(choice==1)
            System.out.println("Register your vehicle");
        else
            System.out.println("Verify your registration");

        return choice;
    }

    public void addCars() {
        Scanner s = new Scanner(System.in);

        System.out.println("Enter your plate number: ");
        Plates[numCars]= s.nextLine();

        System.out.println("Enter your Credit Card Number ($4.00 charge): ");
        ccNumbers[numCars]= s.nextLine();

        System.out.println("Thank you, your plate " +Plates[numCars]+ " has been added to the lot");

        numCars++;
    }
}
于 2012-11-20T23:59:37.970 回答
1
String Plates[]=new String[50];
   String ccNumbers[]=new String[50];

您的 Plates 和 ccNumber 数组是仅限于方法范围的局部变量(方法变量) ,即您不能在方法之外访问它们。使它们成为您的静态实例变量,以便在类中的任何位置访问它们。

public class PayAndGoParking {
 static String Plates[]=new String[50];
 static  String ccNumbers[]=new String[50];
public static void main(String Args[]) {
 // code in your main
}
public static int addCars (String [] plates,String [ ] ccNumbers, int numCars, Scanner keyboard) { 
// you  can now access ccNumber and Plates array here.
}

变量范围:

  1. 静态变量 - 与类本身一样存在。
  2. 实例变量 - 与类的实例一样存在
  3. 局部变量或方法变量 - 存在于方法中。
  4. 块变量 - 只存在于块中
于 2012-11-20T23:44:48.440 回答