Can't find my variable for accessor/mutator method
I am trying to make a program for a homework assignment where on this first class I am making my variables and adding accessor/mutator methods for this but for some reason it can't find my variable of CashBallPick on line 11. I checked many times and it looks spelled the same way at all places with caps in the correct places.
import java.util.Scanner;
public class CashBall
{
Scanner keyboard = new Scanner(System.in);
int[] lotteryNumbers = new int[4];
int yourNumbers;
int CashBallPick;
public CashBall(int yourNumbers, int CashBallPick)
{
this.yourNumbers=yourNumbers;
this.yourCashBallPick=CashBallPick;
}
public int getYourNumbers()
{
return yourNumbers;
}
public int getCashBallPick()
{
return CashBallPick;
}
public void setYourNumbers(int yourNumbers)
{
this.yourNumbers=yourNumbers;
}
public void setCashBallPick(int CashBallPi开发者_如何学JAVAck)
{
this.yourCashBallPick=CashBallPick;
}
}
There is also another class that I am making that is the driver class. I know for normal variables that are written in a different class, lets say a variable called ExampleVariable if I had a constructor for that and wanted to use that variable in a different class I would go ExampleVariable E1 = new ExampleVariable(info here) but how would I do that for an array?
If this is the whole class, try changing line 11 to:
this.CashBallPick=CashBallPick
That should do it. The reason it didn't work before was because your class did not have a variable named "yourCashBallPick".
Edit: line 27 needs the same fix.
It looks like you probably want your constructor like this:
public CashBall(int yourNumbers, int yourCashBallPick)
{
this.yourNumbers=yourNumbers;
this.yourCashBallPick=yourCashBallPick;
}
You're missing the your
prefix for yourCashBallPick
in two places.
You seem to have a mix of CashBallPick
and this.yourCashBallPick
in the code. You didn't declare yourCashBallPick
anywhere so maybe:
int CashBallPick;
Should be
int yourCashBallPick;
--Dan
It is spelled yourCashBallPick
in some places (such as setCashBallPick()
and in your constructor) and CashBallPick
some others (such as getCashBallPick()
). make it the same in all methods.
And regarding arrays, lets say you want an array of 14 ExampleVariable
s:
ExampleVariable example[] = new ExampleVariable[14];
for (int i=0; i< example.length; i++)
example[i] = new ExampleVariable(somevalue);
Is this line correct?
this.yourCashBallPick=CashBallPick;
Is your global variable really declared like this?
精彩评论