0

我对 Java 还很陌生,并且正在为这个概念而苦苦挣扎。正如我所说,我试图在两组整数值之间进行比较,其中一组是我使用 HTML 解析从网站检索并存储在数组 ( Integer [] numbers = new Integer[split.length]) 中的。
我从用户输入中检索并存储在数组userNumbers( int userNumbers = new int [SIZE]) 中的另一组值。我尝试使用 if 条件进行比较,即if (userNumber[count] == number [0]).

但是我遇到了错误,IDE 不允许我输入比较的数字数组部分。谁能帮助我理解为什么会这样,或者告诉我我可能做错了什么?这是完整的代码。提前非常感谢您的帮助。

public class lotteryNumbers
{
   static Scanner keyboard = new Scanner(System.in);

   public static void main(String[] args)
   {
      //link to the intended web site
      try {
         Document doc = Jsoup.connect("http://www.national-lottery.co.uk/player/p/drawHistory.do").get();

         Elements elements = doc.getElementsByClass("drawhistory");
         Element table = elements.first();
         Element tbody = table.getElementsByTag("tbody").first();

         Element firstLottoRow = tbody.getElementsByClass("lottorow").first();

         Element dateElement = firstLottoRow.child(0);
         System.out.println(dateElement.text());

         Element gameElement = firstLottoRow.child(1);
         System.out.println(gameElement.text());

         Element noElement = firstLottoRow.child(2);
         System.out.println(noElement.text());

         String [] split = noElement.text().split(" - ");
         // set up an array to store numbers from the latest draw on the lottery web page
         Integer [] numbers = new Integer [split.length];

         int i = 0;
         for (String strNo : split) {
            numbers [i] = Integer.valueOf(strNo);
            i++;
         }

         for (Integer no : numbers) {
            System.out.println(no);
         }

         Element bonusElement = firstLottoRow.child(3);
         Integer bonusBall = Integer.valueOf(bonusElement.text());
         System.out.println("Bonus ball: " + bonusBall);
         //Elements elementsHtml = doc.getElementsByTag("main-article-content");

      } catch (IOException e) {
         e.printStackTrace();

      }
      final int SIZE = 7;
      //array to store user numbers
      int [] userNumbers = new int[SIZE];
      //array to check if user number is present with web numbers
      boolean [] present = new boolean[7];
      int counter = 0;
      while (counter<SIZE)
      {
         System.out.println("enter your numbers");
         userNumbers[counter]=keyboard.nextInt();
         counter++;
      }
      for (int count: userNumbers)
              System.out.println(count);

      if (userNumbers[0] ==      )
4

1 回答 1

1

numbers局部变量在块中声明try{...}。因此它在它之外是不可访问的。

如果您在该try{行之前声明它,它将起作用:

Integer[] numbers;
try {
  ...
  // set numbers here
  ...
} catch (IOException e) {
  ...
}
// can use numbers here

如果它是您从 HTML 解析代码中需要的唯一值,您甚至可以将try/catch结构重构为返回numbers.

顺便说一句,我建议你不要尝试int == Integer,更喜欢int == int。它通常更清晰,您不必猜测int是装箱还是未Integer装箱。

于 2013-08-19T11:38:14.777 回答