简体   繁体   English

找不到访问器/突变器方法的变量

[英]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.我正在尝试为家庭作业制作一个程序,在第一个 class 上,我正在制作我的变量并为此添加访问器/突变器方法,但由于某种原因,它在第 11 行找不到我的 CashBallPick 变量。我检查了很多次它在所有地方的拼写方式都相同,并且在正确的地方都有大写字母。

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 CashBallPick)
    {
        this.yourCashBallPick=CashBallPick;
    }

}

There is also another class that I am making that is the driver class.还有另一个我正在制作的 class 是驱动程序 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?我知道对于写在不同 class 中的普通变量,如果我有一个构造函数并想在不同的 class 中使用该变量,那么可以说一个名为 ExampleVariable 的变量我会为数组这样做吗?

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.您在两个地方缺少yourCashBallPick your前缀。

If this is the whole class, try changing line 11 to:如果这是整个 class,请尝试将第 11 行更改为:

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".它之前不起作用的原因是因为您的 class 没有名为“yourCashBallPick”的变量。

Edit: line 27 needs the same fix.编辑:第 27 行需要相同的修复。

You seem to have a mix of CashBallPick and this.yourCashBallPick in the code.您似乎在代码中混合了CashBallPickthis.yourCashBallPick You didn't declare yourCashBallPick anywhere so maybe:您没有在任何地方声明您yourCashBallPick ,所以也许:

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() ).它在某些地方拼写为yourCashBallPick (例如setCashBallPick()和您的构造函数),而在其他一些地方拼写为CashBallPick (例如getCashBallPick() )。 make it the same in all methods.使其在所有方法中都相同。

And regarding arrays, lets say you want an array of 14 ExampleVariable s:关于 arrays,假设您想要一个包含 14 个ExampleVariable的数组:

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?你的全局变量真的是这样声明的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM