如何解决这个’Cannot find Symbol’错误?

huangapple go评论173阅读模式
英文:

How do I resolve this 'Cannot find Symbol' error?

问题

以下是要翻译的代码部分:

我试图创建一个运行有效彩票的程序但是我在我的主方法中遇到了问题我收到以下错误消息但不确定原因任何帮助都将不胜感激
Lottery.java:171:错误找不到符号

    for (j = 0; j <= numTix; j++) {
         ^
  符号变量j
  位置类Lottery
Lottery.java:171:错误找不到符号
    for (j = 0; j <= numTix; j++) {
                ^
  符号变量j
  位置类Lottery
Lottery.java:171:错误找不到符号
    for (j = 0; j <= numTix; j++) {
                             ^
  符号变量j
  位置类Lottery
Lottery.java:172:错误找不到符号
                currTicket = userTix[j];
                             ^
  符号变量userTix
  位置类Lottery
Lottery.java:172:错误找不到符号
                currTicket = userTix[j];
                                     ^
  符号变量j
  位置类Lottery
Lottery.java:176:错误找不到符号
                    bestRow = j;
                          ^
  符号变量j
  位置类Lottery
Lottery.java:180:错误找不到符号
    for (int x = 0; x < userTix.length; x++) {
                       ^
  符号变量userTix
  位置类Lottery
Lottery.java:181:错误找不到符号
                System.out.print(userTix[bestRow][x]);
                                 ^
  符号变量userTix
  位置类Lottery

这是我认为出错的方法

public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int k;
        int n;
        int m;
        System.out.println("首先,让我们设置游戏!");
        System.out.print("玩家应选择多少个不同的数字?");
        k = scnr.nextInt();
        System.out.println(k);
        if (k < 1) {
            System.out.print("错误 - 数字必须大于或等于1!请重试。");
            k = scnr.nextInt();
            System.out.print(k);
        }
        System.out.print("好的。这些" + k + "个数字应该在1到多少之间?");
        n = scnr.nextInt();
        System.out.print(n);
        if (n <= k) {
            System.out.print("错误 - 数字必须大于或等于" + k + "。请重试。");
            n = scnr.nextInt();
            System.out.print(n);
        }
        System.out.print("好的。最后,奖励号码应在1到多少之间?");
        m = scnr.nextInt();
        System.out.println(m);
        if (m < 1) {
            System.out.print("错误 - 数字必须大于或等于1。请重试。");
            m = scnr.nextInt();
            System.out.println(m);
        }
        System.out.println("在这个游戏中有" + numPossibleTickets(k, n, m) + "个可能的票。");
        double chanceWin = 1.0 / numPossibleTickets(k, n, m);
        System.out.println("每张票中奖的概率为" + chanceWin + "。让我们玩,祝你好运!");
        System.out.print("您想购买多少张彩票?");
        int numTix = scnr.nextInt();
        System.out.println(numTix);
        if (numTix < 1) {
            System.out.print("错误 - 必须购买至少1张彩票。请重试。");
            numTix = scnr.nextInt();
            System.out.print(numTix);
        }
        for (int i = 1; i <= numTix; i++) {
            System.out.println("票号#" + i + "的" + numTix);
            getPlayerNumbers(k, n);
            System.out.println("到目前为止您的票:");
            int userTix[][] = new int[numTix][k];
            userTix[i - 1] = getPlayerNumbers(k, n);
            System.out.println(userTix + " 奖励号码:" + getBonusNum(m));
        }
        System.out.println("时刻到了!这是抽奖号码:");
        int[] drawnNums = getDrawnNumbers(k, n);
        System.out.println(drawnNums + " 奖励号码:" + 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("这是您最好的票:");
            for (int x = 0; x < userTix.length; x++) {
                System.out.print(userTix[bestRow][x]);
            }
            System.out.println(" 奖励号码:" + m);
            System.out.println("它匹配了" + max + "/" + k + "个号码。");
            if (bonusCheck = true) {
                System.out.println("它匹配了奖励号码。");
            } else {
                System.out.println("它没有匹配奖励号码。");
            }
            if (max == k * 1 && bonusCheck == true) {
                System.out.println("喔呼!中了头奖!!");
            } else {
                System.out.println("对不起,这一次没有中头奖。你真的期望有别的吗?");
            }
        }
    }

请注意,这段代码中有一些HTML实体,如&lt;&quot;,这些需要替换为正确的Java代码符号,以使代码能够编译和运行。

英文:

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

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

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!

答案1

得分: 3

不要有别的内容。以下是翻译好的部分:

"嗯,你没有一个 j 变量(而且 userTix 的作用范围太小)。就是这样简单。尝试:

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

然后将

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

改为

userTix = new int[numTix][k];

此外,对于循环变量,你可以使用(对于 j)像这样:

for (int j = 0; j &lt; numTix; j++) {

请注意,从 0 开始意味着你可能想要使用 &lt;(而不是 &lt;=)。”

英文:

Well, you don't have a j variable (and userTix goes out of scope too soon). 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 (int j = 0; j &lt; numTix; j++) {

Note that starting at 0 means you probably wanted &lt; (not &lt;=).

huangapple
  • 本文由 发表于 2020年8月6日 09:40:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63275690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定