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;
}
所以我想要发生的事情是我想要用户输入的板号和ccCumber在addCars方法中存储在主方法中的板和ccNumber数组中。对不起,如果我听起来像我不知道我在说什么,我是编程和学习的新手。
答案 0 :(得分: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++;
}
}
答案 1 :(得分:1)
String Plates[]=new String[50];
String ccNumbers[]=new String[50];
您的Plates和ccNumber数组局部变量(方法变量)仅限于方法范围,即您无法在 main 之外访问它们方法。将它们设为静态实例变量,以便在类中的任何位置访问它们。
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.
}