BlueJ错误:不兼容的类型:int无法转换为java.lang.String(票证机模拟)
我对java非常陌生,在尽我所能用bluej创建了一个票证机之后,我真的不知道该怎么做BlueJ错误:不兼容的类型:int无法转换为java.lang.String(票证机模拟),java,oop,bluej,Java,Oop,Bluej,我对java非常陌生,在尽我所能用bluej创建了一个票证机之后,我真的不知道该怎么做 public class CarParkTicketMachine { private double RunningBalance; private double CurrentBalance; private String CarReg; private static double MinTicketCharge = 0.7; //Constructor
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;
}
以下是我对验证的建议: 请注意,您正在尝试检查用户是否已输入,然后检查输入的现金。 但是您需要确保用户没有输入,这意味着他输入了一个数字或名称
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;
}
Edit:我应该告诉您,在返回true之后,该方法停止执行。因此,如果您不返回true,这意味着现金的if语句不为true,那么该方法将返回false
通过使用所谓的十进制运算符,可以缩短if语句
这应该是这样的:
return CurrentBalance >= MinTicketCharge ? true : false;
在java中,字符串是对象,因此在比较字符串时,应调用.equals方法或.compare方法。CarReg==这是无效代码。我有3个信息供您参考:1;2.CarReg==不正确,请使用CarReg.equals比较字符串类型;3.如果您对错误/异常有疑问,请发布整个错误/异常,并在出现错误/异常的地方标记行。编辑:第四信息:!CarReg.Trim.Equals这将永远不会编译,请将其更改为!CarReg.trim.isEmpty。@Tom感谢您的回复,但是现在当我为“CarParkTicketMachine”创建一个新对象并右键单击它时,我只有“void RequestTick”选项,而没有以前的任何选项,例如SetCarRegistration。正如我之前所说的,我对java非常陌生,所以我不知道能为您提供什么信息来帮助我,所以如果能为我指明正确的方向,我将不胜感激。您能更新您问题中的代码吗?