2016-04-30 55 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

谢谢,我可以在同一个班级中完成任务,但几乎没有任何问题,但我试图学习如何在单独的班级中完成,以获得方法调用的要点,但我似乎并不了解如何调用方法 –

相关问题