2016-04-30 53 views
1

我想比較不同類中的兩個不同數組,一個用用戶輸入的數字填充,另一個用隨機數填充。但是我無法從用戶的數字中得到一個進入我的構造函數類來比較它們的數字。 下面是代碼:無法將我的數組數組傳遞到另一個類

import java.util.Scanner; 

class LotteryTester{ 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int[] Picks = new int [5]; 
     System.out.println("Introduce your numbers here: "); 

     for(int i = 0; i < Picks.length; i++) 
     { 
      Picks[i] = input.nextInt(); 
     } 

     for (int i = 0; i < Picks.length; i++) 
     { 
      System.out.println(Picks); 
     } 
    } 
} 

這裏是無法找到的第一個數組類:

import java.util.Random; 
    public class Lottery 
    { 
     Random random = new Random(); 
     int number; 
     int count; 
    public Lottery() 
    { 
     int lotteryNums[] = new int[5]; 
     for (int i=0; i < lotteryNums.length; i++) 
     { 
      number = random.nextInt(10); 
      lotteryNums[i] = number; 
     } 
    } 

    public static int CompareNums(Picks[] numbers) 
    { 
     for (int i=0; i <Picks.length; i++) 
     { 
      if (Picks[i] == lotteryNums[i]) 
      { 
       System.out.println("The number on " + (i+1)+ " matches"); 
       count++; 
      } 
      else 
      { 
        System.out.println("the number on " + (i+1)+ " matches"); 
       } 
      } 
      return count; 
     } 
} 

我只是不明白如何使用方法正確

+0

你在哪裏調用'Lottery.CompareNums()'? –

+0

我其實並不知道我應該調用這個對象,我通過使用它引用它的相同對象名稱來假設它。 –

+0

你必須調用它的運行方法。你的主要方法目前不叫它。 –

回答

2

你從來沒有把lotteryNums賦值給構造函數之外的變量。只要方法結​​束,所有在它們內部聲明的變量都不再可用。

你的類應該是這樣的:

public class Lottery 
{ 
    Random random = new Random(); 
    int number; 
    int count; 
    int lotteryNums[]; 
    public Lottery() 
    { 
     lotteryNums[] = new int[5]; //Set the variable in the class so we can use it later 
     for (int i=0; i < lotteryNums.length; i++) 
     { 
      number = random.nextInt(10); 
      lotteryNums[i] = number; 
     } 
    } 

    public int CompareNums(Picks[] numbers) //Removed static since numbers are made in a constructor. All non-static class variables wouldn't be accessible otherwise. 
    { 
     for (int i=0; i <Picks.length; i++) 
     { 
      if (Picks[i] == lotteryNums[i]) 
      { 
       System.out.println("The number on " + (i+1)+ " matches"); 
       count++; 
      } 
      else 
      { 
       System.out.println("the number on " + (i+1)+ " matches"); 
      } 
     } 
     return count; 
    } 
} 

你的主要方法,然後可以改變遵循以下邏輯。 生成中獎號碼:

Lottery lottery = new Lottery(); 

然後,詢問用戶他們的號碼。 最後,檢查中獎號碼,看看用戶是否贏了。

if (lottery.CompareNums(lotteryNums) == 5) System.out.println("You Won!"); 
0

你實際上可以做這個沒有額外的類,arrayLists會更好。

試試這個,我留下評論來告訴你我做了什麼。

import java.util.ArrayList; 
import java.util.Random; 
import java.util.Scanner; 


public class CompareArrayLists { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     ArrayList<Integer> userNums = new ArrayList(); 
     ArrayList<Integer> randNums = new ArrayList(); 
     Random random = new Random(); 

     Scanner cin = new Scanner(System.in); 
     int num = 0; 
     for (int i = 0; i < 5; i++) //let a user enter 5 numbers and generate 5 at the same time 
     { 
      System.out.print("Please enter your " + (i+1) + " number >"); 
      num = cin.nextInt(); 
      userNums.add(num); 
      num = random.nextInt(10); 
      randNums.add(num); 
     } 
     num = 0; 
     for (int j : userNums)//test each item in userNums to see if matches in randNums 
     { 
      if (!(randNums.contains(j))) 
       num++;//increment nums if something doesn't match 
     } 
     if (num > 0)//not a match if nums increments, you could also print nums to see how many are correct 
      System.out.println("Not a match"); 
     else 
      System.out.println("a match"); 



    } 
+0

謝謝,我可以在同一個班級中完成任務,但幾乎沒有任何問題,但我試圖學習如何在單獨的班級中完成,以獲得方法調用的要點,但我似乎並不瞭解如何調用方法 –

相關問題