我是java的新手,在尝试使用bluej创建一个售票机后,我真的不知道该怎么办。
public class CarParkTicketMachine
{
private double RunningBalance;
private double CurrentBalance;
private String CarReg;
private static double MinTicketCharge = 0.7;
//Constructor
public CarParkTicketMachine()
{
RunningBalance = 0;
CurrentBalance = 0;
}
//Try and request a ticket
public void RequestTick()
{
if(Validate())
{
//Create a ticket for the customer
CarParkTicket ticket = new CarParkTicket (CarReg, CurrentBalance);
//keep a running total
RunningBalance += CurrentBalance;
//clear the customer's balance
CurrentBalance = 0;
//print ticket for the customer
ticket.PrintTicket();
}
}
//Perform check to ensure we can purchase a ticket
private boolean Validate()
{
boolean valid = false;
//Check that the user has entered their car registration
if(!(CarReg == null || CarReg.isEmpty())) //must NOT be null
{
//Check that the user has entered enough money
if(CurrentBalance >= MinTicketCharge)
{
return true;
}
else
{
System.out.println("You have not entered enough money");
}
}
else
{
System.out.println("Please enter your car registration");
}
return false;
}
}
public class CarParkTicket
{
//Customer's Car Registration
private String CarRegistration;
//Cost of the ticket
private double charge;
public CarParkTicket (String CarReg, double charge)
{
CarRegistration = CarReg;
double Charge = charge;
}
public void PrintTicket()
{
System.out.println("*****************");
System.out.println("Your Ticket");
System.out.println("Registration:" + CarRegistration);
System.out.println("Cost: £" + charge );
System.out.println("*****************");
}
}
public class Coin
{
//property to hold the value of the coin
private double Value;
//Constructor
public Coin(double value)
{
this.Value = value;
}
//accessor
public double GetValue()
{
return Value;
}
}
我的三个类编译时没有任何问题但是当我创建一个'CarParkTicketMachine'的新对象并尝试输入一个方法调用来设置汽车注册或插入一个硬币我得到一个'错误:找不到符号 - 变量。
我觉得问题出在这一部分,但是因为我对java / bluej非常新,我真的不知道如何解决这个问题:
private Boolean Validate()
{
boolean valid = false;
//Check that the user has entered their car registration
if(CarReg == (" "))
{
//Check that the user has entered enough money
if(CurrentBalance >= MinTicketCharge)
{
valid = true;
}
else
{System.out.println("You have not entered enough money");
}
}
else
{
System.out.println("Please enter your car registration");
}
return valid;
}
答案 0 :(得分:0)
以下是我对Validate()的建议: 请注意,您正在尝试检查用户是否已输入" "然后检查输入的现金。 但是你需要确保用户没有输入" " (这意味着他输入了一个或一个名字)
private bool Validate()
{
//Check that the user has entered their car registration
if(!(CarReg == null || CarReg.isEmpty())) // must NOT be null
{
//Check that the user has entered enough money
if(CurrentBalance >= MinTicketCharge)
{
return true;
}
else
{
System.out.println("You have not entered enough money");
}
}
else
{
System.out.println("Please enter your car registration");
}
return false;
}
编辑:我应该告诉你,在"返回true"之后,该方法停止执行。因此,当您不返回true时(这意味着现金的if语句不为真),该方法将返回到返回false的末尾。
您可以使用所谓的" tenary operator"来缩短if语句。
这应该是这样的:
return CurrentBalance >= MinTicketCharge ? true : false;
你的if语句和你的回报在一行。