Java-参数“无法解析为变量”错误
我的代码有这个问题,我知道它必须处理范围,但我已经检查了很长时间,我似乎找不到解决办法。任何帮助都将不胜感激。似乎所有的错误都与我在一等股票中设置的参数有关 以下是错误:Java-参数“无法解析为变量”错误,java,compiler-errors,Java,Compiler Errors,我的代码有这个问题,我知道它必须处理范围,但我已经检查了很长时间,我似乎找不到解决办法。任何帮助都将不胜感激。似乎所有的错误都与我在一等股票中设置的参数有关 以下是错误: Exception in thread "main" java.lang.Error: Unresolved compilation problems: close cannot be resolved to a variable, price cannot be resolved to a variabl
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
close cannot be resolved to a variable,
price cannot be resolved to a variable,
change cannot be resolved to a variable,
close cannot be resolved to a variable,
这是我的密码:
public class Stock
{
String Symbol;
String Name;
double previousClosingPrice;
double currentPrice;
Stock(String s, String n, double previousClosingPrice, double currentPrice)
{
Symbol = s;
Name = n;
}
public String getName() {
return Name; }
public String getSymbol() {
return Symbol; }
public double setClosingPrice(double close)
{
previousClosingPrice = close;
return close;
}
public double currentPrice(double price)
{
currentPrice = price;
return price;
}
public double getChangePercent(double change)
{
change = (currentPrice - previousClosingPrice) / currentPrice * 100;
return change;
}
public String toString()
{
System.out.println(Name + " stock closing price is $" + setCurrentPrice(price));
}
}
public class Test_Stock
{
public static void main(String[] args)
{
Stock s1 = new Stock("GOG", "Google", 134.67, 131.98);
Stock s2 = new Stock("MSF", "Microsoft", 156.52, 161.22);
System.out.println("Google stock: ");
System.out.println(" Symbol: " + s1.getSymbol());
System.out.println(" Closing price: " + s1.setClosingPrice(close));
System.out.println(" Current price: " + s1.setCurrentPrice(price));
System.out.println(" Change percent: " + s1.getChangePercent(change));
s1.toString();
System.out.println("Google stock: ");
System.out.println(" Symbol: " + s2.getSymbol());
System.out.println(" Closing price: " + s2.setClosingPrice(close));
System.out.println(" Current price: " + s2.setCurrentPrice(price));
System.out.println(" Change percent: " + s2.getChangePercent(change));
s2.toString();
}
}
您的Stock对象将字符串作为第一个和第二个输入:
Stock(String s, String n, double previousClosingPrice, double currentPrice)
就目前的情况而言,您没有给它字符串,我假设输入错误,您忘记了双引号,因此将您的行更改为:
Stock s1 = new Stock("GOG", "Google", 134.67, 131.98);
Stock s2 = new Stock("MSF", "Microsoft", 156.52, 161.22);
这是错误消息的第一部分
下一个问题是,您的currentPrice方法有一个输入错误-我假设它需要设置currentPrice
下一步:您的toString缺少return语句
下一步:您正在使用的变量,如close、price、change,不会在任何地方设置。在将它们用作参数输入之前,您需要将它们设置在某个位置。考虑main中的这两行,例如:
System.out.println(" Closing price: " + s2.setClosingPrice(close));
System.out.println(" Current price: " + s2.setCurrentPrice(price));
你指的是两个变量,收盘价和价格
这些都不是主要声明的
它们都没有声明为类Stock的成员变量
简言之,它们不存在于任何适用范围内。您可能对一些方法使用了类似的名称,但它们不是您现在使用它们的地方可用的名称
我认为您对参数传递的工作原理感到困惑。例如,如果我声明了一个方法:
void myMethod(int fubar) { ... }
然后这样称呼它:
myMethod(42);
然后在myMethod的这个特定调用中,变量fubar的值为42。类似地,如果我这样称呼它:
int xyz = 1 + 2 + 3;
myMethod(xyz);
在调用myMethod时,fubar是6。这里我想说的是fubar是一个变量,它将从调用中接收一个值;该名称在定义它的方法之外的任何地方都不会自动可用,您也不需要使用相同的名称。COG->COG等,即您忘记了字符串周围的引号,这些变量在“main”方法中未定义。这很简单。@clanktree您使用的是IDE吗?您自己查找所有这些错误会有很大帮助:我使用的是VisualStudio代码,但我只得到了编译错误。我今天刚下载了它,所以我不确定是否有正确的调试设置。@clanktree如果没有抱怨所有这些错误,恐怕你没有。在你继续做任何事情之前,你最好先把它整理好。