Java arraylist. I need to pass random numbers to an array list and pass them to another class to check those numbers against another set of numbers -
i confused passing arraylist values 1 class another. used array in these classes before. changed arraylists.
i have 2 classes. class has arraylist called "locationcells". programs 3 random digits class , uses inputs , check if inputs match 3 digits. it's more guessing game.
import java.util.arraylist; class simpledotcom { private arraylist<string> locationcells; public void setlocationcells(arraylist<string> locs) { locationcells = locs; } public string checkyourself(string stringguess) { string result = " miss"; int index = locationcells.indexof(stringguess); if (index >= 0) { locationcells.remove(index); if (locationcells.isempty()) { result = "kill"; } else { result = "hit"; } } return result; } }
this looks right. class main method:
import java.util.arraylist; class simpledotcomgame { public static void main(string[] args) { int numofguesses = 0; gamehelper helper = new gamehelper(); simpledotcom thedotcom = new simpledotcom(); /* part don't understand. used have int array , generated random numbers , worked well. int randomnum = (int) (math.random() * 5); arraylist<string> locations = new arraylist<string>(); */ thedotcom.setlocationcells(locations); boolean isalive = true; while (isalive == true) { string guess = helper.getuserinput("enter number"); string result = thedotcom.checkyourself(guess); numofguesses++; if (result.equals("kill")) { isalive = false; system.out.println("you took " + numofguesses + "guesses"); } } } }
if see comments section above. that's part getting confused. used have array there. int array. able pass int random numbers "simpledotcom" class. arraylist string type, not sure how move forward.
thank in advance,
int numericguess = integer.parseint(helper.getuserinput("enter number"));
also can use list of integers too:
arraylist<integer> locations = new arraylist<integer>(); while(//condition){ int randomnum = (int) (math.random() * 5); locations.add(randomnum) }
this way can perform
locations.indexof(numericguess)
or locations.contains(numericguess)
or
conversely can do,
string guess = helper.getuserinput("enter number"); arraylist<string> locations = new arraylist<string>(); while(//condition){ int randomnum = (int) (math.random() * 5); locations.add(string.valueof(randomnum)) }
and check locations.indexof(guess)
or locations.contains(guess)
Comments
Post a Comment