Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 15th, 2007, 02:27 PM
nijinohashi nijinohashi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 1 nijinohashi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 51 sec
Reputation Power: 0
Lottery problem...

I'm suppose to create a program where the user inputs 6 numbers from 1-49,
the program then gets 6 random numbers
matching the random numbers and the user input numbers to find how many number matches...
but I can't get it to work...
Code:
import java.util.Scanner;

public class PlayLottery {
    public static void main(String[] args) {
    	Scanner scanner = new Scanner(System.in);
    	
    	int[] userNum = getUserNumbers();
    	int[] randomNum = getRandomNumbers();
    	
    	int matchedNum = getTotalMatchedNumbers(userNum, randomNum);
    	
    	for (int i = 0; i < 6; i++)	{
    		System.out.print("There are " + matchedNum + " matched number(s)");
    	}
    }
    
    public static int[] getUserNumbers() {
    	Scanner scanner = new Scanner(System.in);
    	
    	int[] userNum = new int[6];
    	
    	for (int i = 0; i < userNum.length; i++){
    		System.out.print("Enter 6 different numbers: ");
    		userNum[i] = scanner.nextInt();
    	}
    	return userNum;
    }
    
    public static int[] getRandomNumbers() {
    	
    	int[] randomNum = new int[6];
    	
    	for (int i = 0; i < randomNum.length; i++){
    		randomNum[i] = (int)(Math.random()*2);
    		for (int j = 0; j < i; ++j){
    		if (randomNum[i] == randomNum[j]){
    			--i;
    		}
    		}
    		
    	}
    	return randomNum;
    }
    
    public static int getTotalMatchedNumbers(int[] userNum, int[] randomNum) {
    	int matchedNum = 0;
    	for (int i = 0; i < 6; i++){
    		if (userNum[i] == randomNum[i]){
    			matchedNum++;
    		} else {
    		}
    	}
    	return matchedNum;
    }
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Lottery problem...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT