【问题标题】:Getting an error with constructor(error: '(' or '[' expected) [closed]构造函数出错(错误:'('或'['预期)[关闭]
【发布时间】:2022-01-19 06:17:17
【问题描述】:

我在 1 arg 构造函数中遇到错误,由于某种原因,它给了我 (error: '(' or '[' expected) 错误。当我很确定我的括号都很好并且如果有问题我有不知道它们是什么。如果有人可以帮助我,我将不胜感激。(我是一年级程序员,所以请放轻松:))

public class Order{
//fields
double salesTaxRate;
double price;
double shippingCost;
double total;
double salesTax;
String array= new String
//1 arg constructor
public Order(double set1){
salesTaxRate=set1;
price=0;
shippingCost=0;
total=0;
salesTax=0;
}
//setPrice method
public void  setPrice(double p){
price=p;
salesTax=salesTaxRate*price;
double subTotal=(price+salesTaxRate);
if(subTotal<50){
shippingCost=0.8*subTotal;
}

else{
shippingCost=0;
}
}
//getTotalCost method
public double getTotalCost(){
   return total;
}
}
```

【问题讨论】:

  • 我想你忘了在new String 后面加上括号。它是一个构造函数,所以应该调用它。 new String()

标签: java constructor compiler-errors arguments syntax-error


【解决方案1】:

String array= new String 应该做什么? 如果它应该声明一个带有名称数组的String 变量,那么 将String array= new String 更改为String array。 如果它应该创建一个String 的数组,则将其更改为String[] array

【讨论】:

  • 感谢您的帮助,是的,数组在错误的类中,它应该进入我正在编写的另一个类中,但我从未完成它。
【解决方案2】:
String array= "";

甚至

String array;

而不是 String array= new String

【讨论】:

  • 100% 正确,我删除了数组,它起作用了,非常感谢!
【解决方案3】:

问题其实出在构造函数上面的那一行:

String array= new String
//1 arg constructor
public Order(double set1){

您的new String 缺少括号。相反,它应该是

String array= new String();
//1 arg constructor
public Order(double set1){

【讨论】:

  • 是的,它是数组,我看到它删除了它并编译了它,感谢大家的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2016-03-14
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多