I'm clueless at the moment how would I Iterate through one letter in a word each click
for example the word is "DOG"
When someone clicks a button for example named "Click me"
It would give them a option to change the color of the first letter to the last If the player clicks RED the first Letter D would be red if the player clicks on GREEN the second letter O would be green and if someone clicks on purple the last letter G would be purple.
How would I get the next letter in a word?
Here is a simple solution. Every call of nextClick() give you the next letter (as a String):
String word = "DOG";
int position = 0;
public String nextClick() {
if (word != null && position < word.length()) {
position++;
return word.substring(position-1, position);
}
return "";
}
int pos = 0;
String word = ....
public char nextLetter() {
if (pos < word.length()) {
return word.charAt(pos++);
}
return ''; // returns empty char
}
If you want to cycle when reaching the end of the String then do:
public char nextLetter() {
char c = word.charAt(pos);
pos = (pos + 1) ℅ word.length();
return c;
}
Replying to your Question: How would I get the next letter in a word
Assign your word to String
and convert String
to char
array and then just iterate the array.Like in your example on each click you need to pull next element from array.
String str = "DOG";
char[] charArray = str.toCharArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.