java


Building a lotto machine using java. Program Doesn't function if same number is chosen more then once


I am trying to build a lotto machine using java. This is the code:
import java.util.Scanner;
import java.util.Random;
import java.util.Random;
public class wissam {
public static void main(String[] args) {
int counter = 0;
int[] lottoNumbers = new int[6];
for (int x = 0; x < lottoNumbers.length; x++) {
Random rand = new Random();
lottoNumbers[x] = rand.nextInt(42) + 1;
}
int[] userChoice = new int[6];
System.out.println("Enter a number");
Scanner scan = new Scanner(System.in);
userChoice[0] = scan.nextInt();
for (int y = 1; y < userChoice.length; y++) {
System.out.println("Enter another number");
userChoice[y] = scan.nextInt();
}
for (int x = 0; x < lottoNumbers.length; x++) {
System.out.print(lottoNumbers[x] + " ");
}
System.out.println("");
for (int z = 0; z < userChoice.length; z++) {
for (int a = 0; a < lottoNumbers.length; a++) {
if (lottoNumbers[z] == userChoice[a]) {
counter++;
int b = lottoNumbers[z];
System.out.print("The common numbers were" + b + "");
}
}
}
if (counter == 0) {
System.out.println("You are such a loser");
}
}
}
It asks the user to input 6 numbers and works fine, as long as the user chooses 6 distinct numbers between 1 and 42.
I want to check if the user inputs distinct numbers that are between 1 and 42, and if they aren't I want to ask the user to change the number. How do I do that?
I would not use an array. I would use a Set. It has the function 'contains'. So whenever the users gives another number, you can check if the Set so far already contains the number.
I would do it this way
public static void main(String[] args) {
final ArrayList<Integer> userChoice = new ArrayList<>(6);
// Scan numbers
Scanner scan = new Scanner(System.in);
while (userChoice.size() < 6) {
System.out.println("Enter a number");
int choice = scan.nextInt();
if (choice < 1 || choice > 42) {
System.out.println("Number not between 1 and 42");
continue;
}
if (userChoice.contains(choice)) {
System.out.println("Number already chosen");
continue;
}
userChoice.add(choice);
}
System.out.println("You have chosen: " + userChoice);
}
Considering that you're almost there, the simplest case would be to create nested loops and before you store the number, check that it's not already within the array.
for (int y = 1; y < userChoice.length; y++) {
System.out.println("Enter another number");
boolean flag = false;
int result = scan.nextInt();
if(result < 1 || result > 42){
System.out.println("please enter a number between 1 - 42 inclusive");
y--;
continue;
}
for(int i = 1; i < userChoice.length; i++){
if(result == userChoice[i]){
flag = true;
break;
}
}
if(flag){
System.out.println("Enter a different number");
y--;
}else{
userChoice[y] = result;
}
}

Related Links

Listing children from Google Drive folder
How to use a variable of a class in another class without inheritance [closed]
Recursions With Integers (Very Basic) [closed]
Spring Executable jar not saving in DB, It works with Eclipse
Spring session login
Race condition in spring Controller
Javax Mail Untitled attachment in Outlook
Hibernate: ERROR: incorrect usage. OracleSql (-transition <file> | <process_escapes> <convert_nchars> { <sql> } )
Get user properties from azure AD using Graph API
What is exact difference between Inheritance and Abstract class?
Testing REST endpoints with custom exception handling
How to search a word from multiple documents in java?
How to stop the further execution of a Program in Beanshell Assertion
The presenter the model and multiple data sources
Resources should be closed - Sonar
Use jackson to wrap subclass attributes into a sub-key

Categories

HOME
reflection
oracle11g
stock
adb
programming-languages
qore
portia
adfs
applepay
gitpitch
handsontable
modelica
phaser
windows-server-2012
decomposition
angular-ui
http-status-code-504
predix
xlsxwriter
excel-2007
google-search-api
autosys
p-value
poltergeist
entitlements
buck
msys2
kendo-ui-grid
asset-pipeline
gammu
mapdb
stacked
libvpx
angularjs-factory
elasticsearch-plugin
lumberjack
fedex
vao
import-from-excel
node-sass
executenonquery
auto-update
outlook-2013
multipeer-connectivity
dropbox-php
media-player
namecoin
sqldf
blacklist
parallel-data-warehouse
ableton-live
httplistener
elements
azure-virtual-network
underscore.js-templating
measures
contact-list
holder.js
disque
markojs
ios4
cloudbees
browser-link
cartesian-product
ruby-2.2
sframe
generic-programming
skos
sorl-thumbnail
uitouch
jquery-layout
concurrent-collections
meteor-velocity
inbox
web2py-modules
odata4j
qt-faststart
comaddin
lcs
distutils
tinn-r
jquery-knob
rdoc
coderush
windows-phone-7.1.1
ohm
hirefire
xtype
self-extracting
calling-convention
oncheckedchanged
jmock
datareader
digest-authentication
vc90
meego
nintendo-ds
xetex
grid-system
uiq3

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile