How do I fix a Java:26: error: illegal start of type (throws)
How do I fix a Java:26: error: illegal start of type (throws)
我是编写 Java 代码的新手,我很难理解投掷的工作原理。
当我编译与投掷有关的部分代码时,我不断出错。我该如何解决这些问题?
我的代码:
class BankAccount {
public String name;
public double balance;
public BankAccount(String name, double balance) throws NegativeAmountException {
this.name = name;
this.balance = balance; // set name and balance
// throw exception if balance is negative
if (balance < 0) { // make sure balance is not negative
throw new NegativeAmountException(
"Can not create an account with a negative amount.");
}
}
public BankAccount(String name) throws NegativeAmountException {
this(name, 0); // set name and use 0 balance
}
// update balance by adding deposit amount
// make sure deposit amount is not negative
// throw exception if deposit is negative
public void deposit(double amount) {
if (amount > 0) {
this.balance += amount;
} else {
throw new NegativeAmountException("Deposit amount must not be negative");
}
}
throws NegativeAmountException
// update balance by subtracting withdrawal amount
// throw exception if funds are not sufficient
// make sure withdrawal amount is not negative
// throw NegativeAmountException if amount is negative
// throw InsufficientFundsException if balance < amount
public void withdraw(double amount) throws InsufficientFundsException,
NegativeAmountException {
if (amount > getBalance()) {
throw new InsufficientFundsException(
"You do not have sufficient funds for this operation.");
} else if (amount < 0) {
throw new NegativeAmountException(
"Withdrawal amount must not be negative.");
} else {
this.balance -= amount;
}
}
// return current balance
public double getBalance() {
return this.balance;
}
// print bank statement including customer name
// and current account balance
public void printStatement() {
System.out.println("Balance for " + this.name + ": " + getBalance());
}
}
我得到的错误是:
BankAccount.java:26: error: illegal start of type
throws NegativeAmountException
^
BankAccount.java:26: error: ';' expected
throws NegativeAmountException
^
检查以下代码删除了第 26 行并添加了所需的抛出。这应该适合你
class BankAccount {
public String name;
public double balance;
public BankAccount(String name, double balance) throws NegativeAmountException
{
this.name = name;
this.balance = balance; // set name and balance
if (balance < 0) { // make sure balance is not negative
throw new NegativeAmountException("Can not create an account with a negative amount."); // throw exception if balance is negative
}
}
public BankAccount(String name) throws NegativeAmountException
{
this(name, 0); // set name and use 0 balance
}
// update balance by adding deposit amount
// make sure deposit amount is not negative
// throw exception if deposit is negative
public void deposit(double amount) throws NegativeAmountException {
if ( amount > 0) {
this.balance += amount;
} else {
throw new NegativeAmountException("Deposit amount must not be negative");
}
}
// update balance by subtracting withdrawal amount
// throw exception if funds are not sufficient
// make sure withdrawal amount is not negative
// throw NegativeAmountException if amount is negative
// throw InsufficientFundsException if balance < amount
public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException {
if (amount > getBalance()) {
throw new InsufficientFundsException("You do not have sufficient funds for this operation.");
} else if (amount < 0) {
throw new NegativeAmountException("Withdrawal amount must not be negative.");
} else {
this.balance -= amount;
}
}
// return current balance
public double getBalance() {
return this.balance;
}
// print bank statement including customer name
// and current account balance
public void printStatement() {
System.out.println("Balance for " + this.name + ": " + getBalance());
}
}
第 26 行的语句无效。删除那个。您可能忽略了它,因为它靠近评论
您的 deposit
方法定义不正确。您的理解是正确的,它必须声明它抛出哪些已检查的异常,但您没有使用正确的语法。目前,您的代码如下所示:
public void deposit(double amount) {
// Implementation of the method
}
throws NegativeAmountException
throws
子句是方法声明的一部分,因此应该 在 其实现之前出现:
public void deposit(double amount) throws NegativeAmountException {
// Implementation of the method
}
我是编写 Java 代码的新手,我很难理解投掷的工作原理。 当我编译与投掷有关的部分代码时,我不断出错。我该如何解决这些问题?
我的代码:
class BankAccount {
public String name;
public double balance;
public BankAccount(String name, double balance) throws NegativeAmountException {
this.name = name;
this.balance = balance; // set name and balance
// throw exception if balance is negative
if (balance < 0) { // make sure balance is not negative
throw new NegativeAmountException(
"Can not create an account with a negative amount.");
}
}
public BankAccount(String name) throws NegativeAmountException {
this(name, 0); // set name and use 0 balance
}
// update balance by adding deposit amount
// make sure deposit amount is not negative
// throw exception if deposit is negative
public void deposit(double amount) {
if (amount > 0) {
this.balance += amount;
} else {
throw new NegativeAmountException("Deposit amount must not be negative");
}
}
throws NegativeAmountException
// update balance by subtracting withdrawal amount
// throw exception if funds are not sufficient
// make sure withdrawal amount is not negative
// throw NegativeAmountException if amount is negative
// throw InsufficientFundsException if balance < amount
public void withdraw(double amount) throws InsufficientFundsException,
NegativeAmountException {
if (amount > getBalance()) {
throw new InsufficientFundsException(
"You do not have sufficient funds for this operation.");
} else if (amount < 0) {
throw new NegativeAmountException(
"Withdrawal amount must not be negative.");
} else {
this.balance -= amount;
}
}
// return current balance
public double getBalance() {
return this.balance;
}
// print bank statement including customer name
// and current account balance
public void printStatement() {
System.out.println("Balance for " + this.name + ": " + getBalance());
}
}
我得到的错误是:
BankAccount.java:26: error: illegal start of type
throws NegativeAmountException
^
BankAccount.java:26: error: ';' expected
throws NegativeAmountException
^
检查以下代码删除了第 26 行并添加了所需的抛出。这应该适合你
class BankAccount {
public String name;
public double balance;
public BankAccount(String name, double balance) throws NegativeAmountException
{
this.name = name;
this.balance = balance; // set name and balance
if (balance < 0) { // make sure balance is not negative
throw new NegativeAmountException("Can not create an account with a negative amount."); // throw exception if balance is negative
}
}
public BankAccount(String name) throws NegativeAmountException
{
this(name, 0); // set name and use 0 balance
}
// update balance by adding deposit amount
// make sure deposit amount is not negative
// throw exception if deposit is negative
public void deposit(double amount) throws NegativeAmountException {
if ( amount > 0) {
this.balance += amount;
} else {
throw new NegativeAmountException("Deposit amount must not be negative");
}
}
// update balance by subtracting withdrawal amount
// throw exception if funds are not sufficient
// make sure withdrawal amount is not negative
// throw NegativeAmountException if amount is negative
// throw InsufficientFundsException if balance < amount
public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException {
if (amount > getBalance()) {
throw new InsufficientFundsException("You do not have sufficient funds for this operation.");
} else if (amount < 0) {
throw new NegativeAmountException("Withdrawal amount must not be negative.");
} else {
this.balance -= amount;
}
}
// return current balance
public double getBalance() {
return this.balance;
}
// print bank statement including customer name
// and current account balance
public void printStatement() {
System.out.println("Balance for " + this.name + ": " + getBalance());
}
}
第 26 行的语句无效。删除那个。您可能忽略了它,因为它靠近评论
您的 deposit
方法定义不正确。您的理解是正确的,它必须声明它抛出哪些已检查的异常,但您没有使用正确的语法。目前,您的代码如下所示:
public void deposit(double amount) {
// Implementation of the method
}
throws NegativeAmountException
throws
子句是方法声明的一部分,因此应该 在 其实现之前出现:
public void deposit(double amount) throws NegativeAmountException {
// Implementation of the method
}