2 Replies - 380 Views - Last Post: 19 December 2013 - 03:09 PM Rate Topic: -----

#1 Sagaisabella  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-December 13

Javaproblem with code

Posted 19 December 2013 - 01:48 PM

Hi!

I have done a program that randomize a lottery number.

And then the user can enter a own lottery.

Everything works just fine so far, but when i want to correct the lottery it always

return 0 rätt (0 correct numbers)

Row 29 - 35.

would love some help :)

English isn´t my first language, but i hope you can understand my question ;)

public class Lottery {
    private static int[] lotteryNumberss;

      
        
    public static void main(String[] args) {
       
       Scanner t = new Scanner (System.in);
       Random r = new Random();
       int rattnummer = 6;
        
     //deklaration (referens till en array).  
    int [] lotteryNumbers;
    int [] playersNumbers;
        
    //Skapar upp arrayerna lotteryNumbers[6],playersNumbers[6]
    lotteryNumbers = new int[6];
    playersNumbers = new int[6];
    int rattrad = 0;

    
    System.out.println("Skriv in din lottorad (6 siffror mellan 0-9)");//"spelaren fyller på sin lotto rad.
         for (int i = 0; i <playersNumbers.length; i++)
         playersNumbers[i] = t.nextInt();
         
         for (int j = 0; j <lotteryNumbers.length; j++) //Den rätta raden slumpas fram.
             lotteryNumbers[j] = r.nextInt(9);
         
  //Kontroll av hur många rätt spelaren fick

         [b]for( int i = 0; i < lotteryNumbers.length; i++)
            if(playersNumbers == lotteryNumbers)
            {
            rattrad++;
            }





         
       
         
        System.out.println("Spelarens rad:   " + Arrays.toString(lotteryNumbers)); //Den första arrayen "lotteryNumbers" innehåller den korrekta raden
        System.out.println("Den rätta raden: " + Arrays.toString(playersNumbers)); //Den andra arrayen "playersNumbers" innehåller spelarens valda siffror
        System.out.println("Antal rätt: " + rattrad);
          
        
    
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Javaproblem with code

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,500
  • Joined: 12-December 12

Re: Javaproblem with code

Posted 19 December 2013 - 02:19 PM

if(playersNumbers == lotteryNumbers)

These are both arrays so you shouldn't be comparing them in this way. Assuming you want to compare each element:

if (playersNumbers[i] == lotteryNumbers[i])

However, this compares elements at the same index/position. This is unlikely to achieve the result you are after. You'll need to investigate alternatives, to compare each element of the first array with each element of the second. There are a number of ways to approach these. I'll leave you to pursue this.
Was This Post Helpful? 0
  • +
  • -

#3 Sagaisabella  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-December 13

Re: Javaproblem with code

Posted 19 December 2013 - 03:09 PM

Thanks!

working now

for (int i = 0; i <lotteryNumbers.length; i++ ) {
            if (lotteryNumbers[i] == playersNumbers[i]) {
                ++resultat; 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1