[英]Pass user input value from class to class on Java
我正在研究从 Java 书开始的停车票模拟器,所以我的想法是要求用户输入警官的姓名、警官的徽章编号和其他一些信息,我在 class ParkingCarSimulator
class ParkingCarSimulator ZA6142F2ED4F8DC402 ParkingCarSimulator.java
,
所有这些工作正常,现在在 class 中称为PoliceOfficer
,位于PoliceOfficer.java
文件中,我想知道我是否可以将用户输入从PoliceOfficer
ParkingCarSimulator
。
任何想法将不胜感激,这是我的代码:
import java.util.Scanner;
public class ParkingCarSimulator {
public static void main(String[] arsg)
{
String officerName, Make, carModel, carColor, carLicense;
int badgeNumber, minOnCar, minPurchased;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the officer's name");
officerName = keyboard.nextLine();
System.out.println("Enter the officer's badge number");
badgeNumber = keyboard.nextInt();
System.out.println("Enter the car's make");
Make = keyboard.nextLine();
System.out.println("Enter the car's model");
carModel = keyboard.nextLine();
System.out.println("Enter the car's color");
carColor = keyboard.nextLine();
System.out.println("Enter the car's liscence number");
carLicense = keyboard.nextLine();
System.out.println("Enter minutes on car");
minOnCar = keyboard.nextInt();
System.out.println("Enter the number of minutes purchased");
minPurchased = keyboard.nextInt();
}
}
这是警官。java
public class PoliceOfficer
{
String policeName = ParkingCarSimulator.officerName;(this throws an error)
}
要创建PoliceOfficer
,您需要给它一个constructor
,然后在主代码中实例化它
public class PoliceOfficer{
String policeName;
int badgeNumber;
public PoliceOfficer(String policeName, int badgeNumber){
this.policeName = policeName;
this.badgeNumber = badgeNumber;
}
}
利用
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the officer's name");
String officerName = keyboard.nextLine();
System.out.println("Enter the officer's badge number");
int badgeNumber = keyboard.nextInt();
PoliceOfficer po = new PoliceOfficer(officerName, badgeNumber);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.