This question already has answers here:
is there any way to find resource Id of drawable
(7 answers)
Closed 7 years ago.
I want to complete a task, which assigns the variable randomButton a value. At the end, I want to check the value of randomButton If it is something I don't like, I want to re do the task where I assign randomButton a value.
I want to do this task:
int i = rng.nextInt(4); //Find random button from between 1-4
randomButton = buttons.get(i);
buttonBackground = randomButton.getBackground();
Log.v(TAG, "In the do");
and if
buttonBackground.equals(R.drawable.redcircle)
I want to try and do the task again. How do I achieve this...?
I have tried:
do {
int i = rng.nextInt(4); //Find random button from between 1-4
randomButton = buttons.get(i);
buttonBackground = randomButton.getBackground();
Log.v(TAG, "In the do");
}while(!buttonBackground.equals(R.drawable.redcircle));
But that is an infinite loop. So how do I repeat a task if the value is something I don't like?
By the way, buttons is an arraylist with different image button objects. I want to see if the background of that imagebutton (randomButton) is redcircle. If it is, I want to go through the code again. If it isn't, thats great, and I can continue.
A do-while loop would be the correct way to to this. I do not know what R.drawable.redcircle means, is that a color?
You did something wrong in the while clause:
}while(!buttonBackground.equals(R.drawable.redcircle));
I should have posted this in the comment but i couldn't since i dont have 50 reputation yet.
Related
This question already has answers here:
How to generate random numbers without repetition
(5 answers)
Closed 1 year ago.
I wanna make image guessing android game, there are 150 images, I want the image to be generated randomly, and the user should type the name of character if it is correct the next button will display another image till the 150 are finished
This the code for avoiding image repition
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.myImage);
int position = new Random().nextInt(list.size());
imageViewObject.setImageResource(Integer.intValue(list.get(position)));
list.remove(position);
but how should I compare the user input with character name, should I use image path, or make array for answers and compare it to user input?
There are many ways to do it, depending on your project. An effective one would be storing them in an object (let's call it Card), and card will contain as properties (imageURL as String / characterName as String/ ifCorrect as Boolean).
Store these objects inside an array and if the user guesses the right answer, change the object's ifCorrect field to true, so you know the image has been played.
(I am a beginner)
I am making a decision maker app that gives responses randomly using a switch and changes the text of a TextView as output. What I want to do:
For example, if user asks "Should I post a question on stackoverflow?taps OKthe app displays an answer randomly(say, Yes! you should do it.). And next time when the user asks the same question I want the same response (First time response should be random next time it should be same as given before).I am using EditText for input.
I tried doing this with arrays but it does not work. If it is to be done by arrays please explain or give any other method for doing this...Thanks
If I was you I'd use a HashMap.
HashMap<String, String> mymap = new HashMap<String , String>();
Then you can save the question and answer like:
String question = "Should I post a question on stackoverflow?"
String answer = "Yes! you should do it"
mymap.put(question, answer);
And then when you want to retrieve the answer for this question you do it like:
mymap.get(question);
Edit:
The question in your HashMap is a key
To retrieve your answer you have to find it by the key(which is your question ).So instead of saying:
mymap.get(answer); you say mymap.get(question);
If you want to retrieve your question you probably have to write a method like:
public String getQuestion(String userQuetsion){
if(mymap.keySet().contains(userQuestion))
return userQuestion;
}
I'm a new programmer doing a date of birth selector for part of my project. I have got everything set up apart from a few things and I am unsure of how to do these things.
ArrayList<String> years_tmp = new ArrayList<String>();
years_tmp.add("Year");
for(int years = Calendar.getInstance().get(Calendar.YEAR) ; years>=Calendar.getInstance().get(Calendar.YEAR)-90;years--)
{
years_tmp.add(years+"");
}
Y = new JComboBox(years_tmp.toArray());
Above is my part of my code for a JComboBox which lists the previous 90 years and has the word "Years" as the first object.
For my code above how would I list the years like it currently does, but to only display years which divide by four exactly (leap years)?
Also how do I make it so once the JComboBox list has been opened the selection years can not be selected so when the value is saved in my save file it does not allow the save of the word "Years"?
To get a value which can be devided be 4 without a rest you can use the modulo operator '%':
if(year % 4 == 0) {
...
}
To disallow the Selection of "year" itself, you have several ways. One could be to append an ItemListener to your ComboBox and check whether the user selected the "year" value. If the user selected this value you can print an error message or just select another value - maybe the next possible one. You also can do more fancy stuff like disabling the save Button if the user selected an invalid value..
I think this should help you to get to the right direction.
I want to verify the user input (number) in a textfield that if it's bigger than 9 or not. Note that the number from 1 to 9.
if it's bigger than 9 I want to show a jOptionPane
some code i have traied:
else
if(jTextField1.contains()){ // want to compare it if it's bigger than 9 or not
jOptionPane1.showMessageDialog(this,"Please enter the number of the tab"); // wich means from 1 to 9
}
So how to do that with java?
Thanks in advance :)
You can directly do it with help of js using jQuery just give id to our text field and add a script tag with the following code :
num = $("#yourId").val();
if(num>9){
alert("your message");
}
or any thing else you want to achieve
Say the user answers a mathematical question, then he moves on to answer the next one. What statement do you use to empty the EditText?
i.e. 1 + 1 = 2, 2 is still in the EditText when the equation is now 3 + 3.
If I haven't made myself clear please let me know.
Set the text to "":
editText.setText("");