Java语法把我难住了
我认为我的代码存在一些基本问题。我对java语法不太熟悉,所以我不太确定哪里出错了。任何帮助都将不胜感激 <>我试图在中间使用构造函数,一些访问器指向底部,但是我想我已经为自己复杂化了。Java语法把我难住了,java,syntax,Java,Syntax,我认为我的代码存在一些基本问题。我对java语法不太熟悉,所以我不太确定哪里出错了。任何帮助都将不胜感激 我试图在中间使用构造函数,一些访问器指向底部,但是我想我已经为自己复杂化了。 import java.util.Scanner; public class FerryBooking { public static void main(String args[]){ class VehicleBooking { private Stri
import java.util.Scanner;
public class FerryBooking {
public static void main(String args[]){
class VehicleBooking {
private String booking_ID = new String("");
private String registration = new String("");
private String make_model = new String("");
private int number_passengers = 1;
private boolean insurance_choice = false;
private boolean insurance_flag = false;
public static final int booking_fee= 100;
public static final int extra_passenger = 50;
public static final int insurance_fee = 50;
VehicleBooking() {
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.print("Enter booking ID");
booking_ID = input.next();
System.out.print("Enter registration number");
registration = input.next();
System.out.print("Enter vehicle make/model");
make_model = input.next();
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
}
public String getBookingID(){
return booking_ID;
}
public String getRegistration(){
return registration;
}
public String getMakeModel(){
return make_model;
}
public int getPassengers(){
return number_passengers;
}
public boolean getInsurance(){
return insurance_choice;
}
public boolean addInsurance(){
insurance_choice = true;
if (insurance_flag = false) {
insurance_flag = true;
return true;
} else if (insurance_flag = true) {
return false;
}
return true;
}
public double getBookingFee(){
int final_cost = booking_fee + (getPassengers()*extra_passenger);
if (insurance_choice = true){
final_cost = final_cost + insurance_fee;
}
return final_cost;
}
}
}
}
--编辑--
我已经重新编写了很多代码,并将其缩小到我的大问题所在;构造器。但是,我得到了一个与提供给我的代码的构造函数相关的错误
import java.util.Scanner;
public class VehicleBooking {
private String booking_ID = "";
private String registration = "";
private String make_model = "";
private int number_passengers = 1;
private boolean insurance_choice = false;
public static final int BOOKING_FEE= 100;
public static final int EXTRA_PASSENGER = 50;
public static final int INSURANCE_FEE = 50;
public VehicleBooking(String booking_ID1, String registration1, String make_model1, int number_passengers1) {
/** Initialise the variables **/
booking_ID = booking_ID1;
registration = registration1;
make_model = make_model1;
number_passengers = number_passengers1;
}
public static void main(String args[]) {
VehicleBooking vb = new VehicleBooking(booking_ID1, registration1, make_model1, number_passengers1);
}
}
首先,不要使用
private String booking_ID = new String("");
您正在创建一个新的空不可变表。你至少应该让JVM实习生空白字符串!
if (insurance_flag = false)
这是分配,不是评估,使用==,如果(!insurance_flag)更好
整个函数没有多大意义,你已经用圆圈编码了。只需将保险标志设置为true。除退货外,保险标志不在任何地方使用。。。为什么您只想在第一次设置标志时返回true?把它弄空就行了。用于“设置”操作
此外,您将make和model合并到一个字符串中,这并不是构建项目的一种非常面向对象的方式。理想情况下,您应该将车辆对象附加到车辆预订,而不是车辆本身的属性
丢弃内部类,不要在构造函数中执行输入。从输入构建(即传入)或使用生成器。首先,不要使用
private String booking_ID = new String("");
您正在创建一个新的空不可变表。你至少应该让JVM实习生空白字符串!
if (insurance_flag = false)
这是分配,不是评估,使用==,如果(!insurance_flag)更好
整个函数没有多大意义,你已经用圆圈编码了。只需将保险标志设置为true。除退货外,保险标志不在任何地方使用。。。为什么您只想在第一次设置标志时返回true?把它弄空就行了。用于“设置”操作
此外,您将make和model合并到一个字符串中,这并不是构建项目的一种非常面向对象的方式。理想情况下,您应该将车辆对象附加到车辆预订,而不是车辆本身的属性
丢弃内部类,不要在构造函数中执行输入。从输入构造(即传入)或使用生成器。首先不要初始化以下字符串:-
private String booking_ID = new String("");
public boolean addInsurance(){
boolean returnValue = !insurance_flag;
insurance_flag = true;
return returnValue;
}
而是使用String.valueOf()
或只为变量指定一个空字符串:-
private String booking_ID = "";
其次,养成用大写字母声明你的常量的习惯:-
public static final int BOOKING_FEE = 100;
第三,看看您的构造函数:-
VehicleBooking() {
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.print("Enter booking ID");
booking_ID = input.next();
System.out.print("Enter registration number");
registration = input.next();
System.out.print("Enter vehicle make/model");
make_model = input.next();
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
}
您不应该在构造函数中执行I/O
操作<代码>构造函数
用于初始化对象的状态。它的唯一用途是初始化
出于I/O目的,创建不同的方法readInput()
,并在创建对象后调用它
另一件事:-这是您使用的if-else if
块
if (insurance_flag = false) {
insurance_flag = true;
return true;
} else if (insurance_flag = true) {
return false;
}
在此代码中,您的if
始终为false
,您的else if
始终为true
。。因为您实际上是将这些值
分配给您的保险标志
。。您应该使用==
进行比较
因此,如果(保险标志==false)使用
。。事实上,你不需要和布尔文字进行比较
只要使用:-如果(!insurance\u flag)
。。它们是等价的
理想情况下,您应该将包含上述if的方法更改为以下方法:-
private String booking_ID = new String("");
public boolean addInsurance(){
boolean returnValue = !insurance_flag;
insurance_flag = true;
return returnValue;
}
因为你的方法就是这么做的,但方式很奇怪
您还可以替换最终成本=最终成本+保险费代码>
使用以下代码:-
final_cost += insurance_fee;
通过这种方式,最终成本
不会被评估两次
编辑**:-
您的构造函数应如下所示:-
public VehicleBooking(String bookingId, String registration, String makeModel,
String numberOfPassengers) {
/** Initialize the instance variables **/
/** this represent the reference to current object **/
this.booking_ID = bookingId;
this.registration = registration;
this.make_model = makeModel;
this.number_passengers = numberOfPassengers;
}
因此,实际上,您正在将读取的值作为参数传递到构造函数中,并使用这些参数初始化实例属性
我想这会消除您的疑虑。首先,不要像下面这样初始化字符串:-
private String booking_ID = new String("");
public boolean addInsurance(){
boolean returnValue = !insurance_flag;
insurance_flag = true;
return returnValue;
}
而是使用String.valueOf()
或只为变量指定一个空字符串:-
private String booking_ID = "";
其次,养成用大写字母声明你的常量的习惯:-
public static final int BOOKING_FEE = 100;
第三,看看您的构造函数:-
VehicleBooking() {
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.print("Enter booking ID");
booking_ID = input.next();
System.out.print("Enter registration number");
registration = input.next();
System.out.print("Enter vehicle make/model");
make_model = input.next();
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
}
您不应该在构造函数中执行I/O
操作<代码>构造函数
用于初始化对象的状态。它的唯一用途是初始化
出于I/O目的,创建不同的方法readInput()
,并在创建对象后调用它
另一件事:-这是您使用的if-else if
块
if (insurance_flag = false) {
insurance_flag = true;
return true;
} else if (insurance_flag = true) {
return false;
}
在此代码中,您的if
始终为false
,您的else if
始终为true
。。因为您实际上是将这些值
分配给您的保险标志
。。您应该使用==
进行比较
因此,如果(保险标志==false)使用
。。事实上,你不需要和布尔文字进行比较
只要使用:-如果(!insurance\u flag)
。。它们是等价的
理想情况下,您应该将包含上述if的方法更改为以下方法:-
private String booking_ID = new String("");
public boolean addInsurance(){
boolean returnValue = !insurance_flag;
insurance_flag = true;
return returnValue;
}
因为你的方法就是这么做的,但方式很奇怪
您还可以替换最终成本=最终成本+保险费代码>
使用以下代码:-
final_cost += insurance_fee;
通过这种方式,最终成本
不会被评估两次
编辑**:-
你的构造函数应该看起来像