我对java很新,只是想了解基础知识。我无法让我的代码运行,而其余的错误似乎无法弄清楚如何修复。
我想创建一个帐户ID为00455420,余额为$ 100.50,名称为“John Doe”和地址的帐户对象。然后,使用withdraw方法提取4.50美元,使用存款方法存入7.75美元,并使用clear方法将余额设置回0.0。 使用余额字段的getter在对象创建后,提取后和存款后打印此余额值。
public class Account { // class name
//data fields: dataFieldName: dataFieldType
int studentId = 00000000;//*string must be String, cant convert from int to string
double Balance = 0.0;
String name = ("") ;
String address= ("");
//constructors
// has no parameters, must have same name as class, no return type/void, invoked using "new" operator initialize objects
Account(){// create default Account no-arg constructor
}
Account(int studentId, double balance, String name, String address){ // constructor syntax
}
//Methods, get/set, main, withdraw deposit clear
public static void main (String[] args) { // main method
Account a1 = newAccount("00455420",100.5,"John Doe","1313 Mockingbird Lane");
System.out.println(a1.getBalance());
a1.withdraw(4.5);
System.out.println(a1.getBalance());
a1.deposit(7.75);
System.out.println(a1.getBalance());
a1.clearBalance();
System.out.println(a1.getBalance());
}
// get and set methods for balanceGet methods provide access to the value a variable holds while set methods assign values to the variables of the objects.
public void setBalance(double num1){
Balance = num1;
}
public String getBalance(){
return static String Balance;
//withdraw/deposit method to - specified amt from balance
public void withdraw(double withdrawAmount) {
Balance -= withdrawAmount;
}
public void deposit(double depositAmount) {
Balance += depositAmount;
}
// clear balance
public void double clearBalance(); {
double Balance.clear();
}
//methods are capable of returning values. These values can be of any primitive data type or reference type like a class.
}
控制台错误---------------
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method newAccount(String, double, String, String) is undefined for the type Account
at Account.main(Account.java:19)
问题--------------------
Description Resource Path Location Type
Duplicate field Account.Balance Account.java /Assignment/src line 33 Java Problem
Syntax error on token "void", volatile expected Account.java /Assignment/src line 43 Java Problem
This method requires a body instead of a semicolon Account.java /Assignment/src line 43 Java Problem
Syntax error on token ".", ; expected Account.java /Assignment/src line 44 Java Problem
The method newAccount(String, double, String, String) is undefined for the type Account Account.java /Assignment/src line 19 Java Problem
Syntax error, insert ";" to complete BlockStatements Account.java /Assignment/src line 33 Java Problem
Syntax error, insert "}" to complete MethodBody Account.java /Assignment/src line 33 Java Problem
The project cannot be built until build path errors are resolved Assignment 1 Unknown Java Problem
This method must return a result of type String Account.java /Assignment/src line 33 Java Problem
Duplicate field Account.Balance Account.java /Assignment/src line 6 Java Problem
Unbound classpath container: 'JRE System Library [JavaSE-1.8]' in project 'TEST' TEST Build path Build Path Problem
The project cannot be built until build path errors are resolved TEST Unknown Java Problem
Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment. Assignment Build path JRE System Library Problem
Unbound classpath container: 'JRE System Library [JavaSE-1.8]' in project 'Assignment 1' Assignment 1 Build path Build Path Problem
Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment. Exercise4a Build path JRE System Library Problem
The method clear() is undefined for the type Account Account.java /Assignment/src line 44 Java Problem
答案 0 :(得分:1)
new
和Account
Account a1 = new Account("00455420",100.5,"John Doe","1313 Mockingbird Lane");
除此之外,您已为int
定义了studentId
的构造函数,但解析了一个String
所以将studentId
更改为String并将构造函数更改为接受字符串而不是int
还有很多其他事情需要改变。如果你想要的东西,那么编译我的版本。我不知道这是否是您想要的,但您可以找到您所犯的错误
public class Account { // class name
//data fields: dataFieldName: dataFieldType
String studentId = "00000000";//*string must be String, cant convert from int to string
double Balance = 0.0;
String name = ("") ;
String address= ("");
//constructors
// has no parameters, must have same name as class, no return type/void, invoked using "new" operator initialize objects
Account(){// create default Account no-arg constructor
}
Account(String studentId, double balance, String name, String address){ // constructor syntax
}
//Methods, get/set, main, withdraw deposit clear
public static void main (String[] args) { // main method
Account a1 = new Account("00455420",100.5,"John Doe","1313 Mockingbird Lane");
System.out.println(a1.getBalance());
a1.withdraw(4.5);
System.out.println(a1.getBalance());
a1.deposit(7.75);
System.out.println(a1.getBalance());
a1.clearBalance();
System.out.println(a1.getBalance());
}
// get and set methods for balanceGet methods provide access to the value a variable holds while set methods assign values to the variables of the objects.
public void setBalance(double num1){
Balance = num1;
}
public double getBalance(){
return Balance;
}
//withdraw/deposit method to - specified amt from balance
public void withdraw(double withdrawAmount) {
Balance -= withdrawAmount;
}
public void deposit(double depositAmount) {
Balance += depositAmount;
}
// clear balance
public void clearBalance() {
Balance = 0.0;
}
}