到目前为止我有以下代码:
import java.util.Scanner;
public class HallLanceMemoryCalculator {
private double currentValue;
public static int displayMenu(){
Scanner input=new Scanner(System.in);
int choice=0;
while(choice<1||choice>5){
System.out.println("1.Add");
System.out.println("2.Subtract");
System.out.println("3.Multiply");
System.out.println("4.Divide");
System.out.println("5.Clear");
System.out.println("What would you like to do?");
choice=input.nextInt();
}
return choice;
}
public static double getOperand(String prompt){
Scanner input=new Scanner(System.in);
System.out.println("What is the second number?");
double secondNumber=input.nextDouble();
return secondNumber;
}
public double getCurrentValue(){
return currentValue;
}
public void add(double operand2){
currentValue+=operand2;
}
public void subtract(double operand2){
currentValue-=operand2;
}
public void multiply(double operand2){
currentValue*=operand2;
}
public void divide(double operand2){
currentValue/=operand2;
}
public void clear(){
currentValue=0;
}
public static void main(String[] args) {
double value=getCurrentValue();
}
}
当我尝试设置双倍时value=getCurrentValue();
最后,我收到一条错误消息“无法对非静态方法进行静态引用。”它说解决办法是使getCurrentValue()
方法也是静态的,但我的教授告诉我不要将该字段设为静态。我只是缺少一个简单的解决方案吗?
静态方法属于类,非静态方法属于类instance班级的。
你打电话时getCurrentValue()
from main
,你会得到一个错误,因为main
不与任何实例关联。
您需要创建该类的实例:
HallLanceMemoryCalculator me = new HallLanceMemoryCalculator();
然后你可以调用实例的getCurrentValue()
:
double value = me.getCurrentValue();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)