简体   繁体   English

如何解决此“找不到符号”错误?

[英]How do I resolve this 'Cannot find Symbol' error?

I'm trying to create a program that runs an effective lottery.我正在尝试创建一个运行有效彩票的程序。 However, I'm running into problems in my main method.但是,我在主要方法中遇到了问题。 I'm receiving the following error messages, and I'm not sure why.我收到以下错误消息,我不知道为什么。 Any help would be appreciated.任何帮助,将不胜感激。 Lottery:java:171: error: cannot find symbol彩票:java:171:错误:找不到符号

            for (j = 0; j <= numTix; j++) {
                 ^
  symbol:   variable j
  location: class Lottery
Lottery.java:171: error: cannot find symbol
            for (j = 0; j <= numTix; j++) {
                        ^
  symbol:   variable j
  location: class Lottery
Lottery.java:171: error: cannot find symbol
            for (j = 0; j <= numTix; j++) {
                                     ^
  symbol:   variable j
  location: class Lottery
Lottery.java:172: error: cannot find symbol
                currTicket = userTix[j];
                             ^
  symbol:   variable userTix
  location: class Lottery
Lottery.java:172: error: cannot find symbol
                currTicket = userTix[j];
                                     ^
  symbol:   variable j
  location: class Lottery
Lottery.java:176: error: cannot find symbol
                    bestRow = j;
                              ^
  symbol:   variable j
  location: class Lottery
Lottery.java:180: error: cannot find symbol
            for(int x = 0; x < userTix.length; x++) {
                               ^
  symbol:   variable userTix
  location: class Lottery
Lottery.java:181: error: cannot find symbol
                System.out.print(userTix[bestRow][x]);
                                 ^
  symbol:   variable userTix
  location: class Lottery

Here is the method I believe the error is coming from:这是我认为错误来自的方法:

public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int k;
        int n;
        int m;
        System.out.println("First, let's set up the game!");
        System.out.print("How many distinct numbers should the player pick? ");
        k = scnr.nextInt();
        System.out.println(k);
        if (k < 1) {
            System.out.print("Error - number must be greater than or equal to 1! Please try again. ");
            k = scnr.nextInt();
            System.out.print(k);
        }
        System.out.print("Ok. Each of those " + k + " numbers should range from 1 to what? ");
        n = scnr.nextInt();
        System.out.print(n);
        if (n <= k) {
            System.out.print("Error - number must be greater than or equal to " + k + ". Try again. ");
            n = scnr.nextInt();
            System.out.print(n);
        }
        System.out.print("Ok. And finally, the bonus number should range from 1 to what? ");
        m = scnr.nextInt();
        System.out.println(m);
        if (m < 1) {
            System.out.print("Error - number must be greater than or equal to 1. Try again. ");
            m = scnr.nextInt();
            System.out.println(m);
        }
        System.out.println("There are " + numPossibleTickets(k, n, m) + " possible tickets in this game.");
        double chanceWin = 1.0 / numPossibleTickets(k, n, m);
        System.out.println("Each ticket has a " + chanceWin + " chance of winning the Jackpot. Let's play, good luck!");
        System.out.print("How many tickets would you like to buy? ");
        int numTix = scnr.nextInt();
        System.out.println(numTix);
        if (numTix < 1) {
            System.out.print("Error - must buy at least 1 ticket. Try again. ");
            numTix = scnr.nextInt();
            System.out.print(numTix);
        }
        for (int i = 1; i <= numTix; i++) {
            System.out.println("Ticket #" + i + "of " + numTix);
            getPlayerNumbers(k, n);
            System.out.println("Your tickets so far: ");
            int userTix[][] = new int[numTix][k];
            userTix[i - 1] = getPlayerNumbers(k, n);
            System.out.println(userTix + " Bonus: " + getBonusNum(m));
        }
        System.out.println("The moment of truth has arrived! Here are the drawn numbers: ");
        int[] drawnNums = getDrawnNumbers(k, n);
        System.out.println(drawnNums + " Bonus: " + randBonusNum(m));
        int randBonus = randBonusNum(m);
        boolean bonusCheck = bonusMatch(m, randBonus);
        if (numTix > 1) {
            int tempMatches = 0;
            int max = -1;
            int bestRow = 0;
            int[]currTicket = new int[k];
            for (j = 0; j <= numTix; j++) {
                currTicket = userTix[j];
                tempMatches = countMatches(currTicket, drawnNums);
                if (max < tempMatches) {
                    max = tempMatches;
                    bestRow = j;
                }
            }
            System.out.println("Here's your best ticket: ");
            for(int x = 0; x < userTix.length; x++) {
                System.out.print(userTix[bestRow][x]);
            }
            System.out.println(" Bonus: " + m);
            System.out.println("It matched " + max + "/" + k + "drawn numbers.");
            if (bonusCheck = true) {
                System.out.println("It did match the bonus number.");
            }
            else {
                System.out.println("It did not match the bonus number.");
            }
            if (max == k * 1 && bonusCheck == true) {
                System.out.println("WOOHOO! JACKPOT!!");
            }
            else {
                System.out.println("Sorry, no jackpot this time. Did you really expect anything else?");
            }
        }
    }

Again, I'm fairly sure I declared the variables so I'm not sure exactly where the issue lies.同样,我很确定我声明了变量,所以我不确定问题出在哪里。 If any of my methods that I call appear to be part of the problem, let me know and I'll post those as well.如果我调用的任何方法似乎是问题的一部分,请告诉我,我也会发布这些方法。 If you see any other errors in my code don't hesitate to let me know!如果您在我的代码中看到任何其他错误,请随时告诉我!

Well, you don't have a j variable (and userTix goes out of scope too soon).好吧,您没有j变量(并且userTix很快就会退出 scope )。 Simple as that.就那么简单。 Try尝试

int k;
int n;
int m;
int j;
int[][] userTix; // int userTix[][] is legal, but harder to read (IMO)

And then change然后改变

int userTix[][] = new int[numTix][k];

to

userTix = new int[numTix][k];

Also, for the loop variable, you could use (for j ) like此外,对于循环变量,您可以使用 (for j ) 之类的

for (int j = 0; j < numTix; j++) {

Note that starting at 0 means you probably wanted < (not <= ).请注意,从0开始意味着您可能想要< (而不是<= )。

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

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