int triesno=5;
final TextView tries= (TextView) findViewById(R.id.tries);
tries.setText("you have "+triesno.toString()+" tries left");
I want to make a game that when the user sends wrong input he gets less tries. The above code prints the initial number, but I want to modify only the variable and not to update like:
triesno--;
tries.setText("you have "+triesno.toString()+" tries left");
How can I do that?
int tries = 5;
final TextView triesTv = (TextView) findViewById(R.id.tries);
tries = tries - 1;
String triesStr = String.format("you have: %d tries left", tries);
triesTv.setText(triesStr);
Are you sure about your question?
final TextView tvTries = (TextView) findViewById(R.id.tries);
int tries = Integer.parseInt(tvTries.getText()); // Get current number of time that you can try to.
tvTries .setText("you have "+ --tries + " tries left"); // -1 and set to textview
Related
I have my first Android app and I am having difficulty on how can I double the point for a word/words whenever a player click an imageview. Basically, I want it to function like this: If the player hasn't click the imageview, the pointing system stays the same. If the player clicks the imageview the a message will pop-out (a toast perhaps telling the player that the points for the next 3 words will be doubled). How can I make this happen? How can I double the points for the 3 consecutive words without exceeding?
Here's my code for searching and calculation
//CALCULATE SCORE
private int optionTxtView = 0 ; //which textview to use for addition purposes
private int addClick = 0 ; //No. of clicks for search for calculation
private void calculate(){
x = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));
y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
z = x + y;
score.setText(Integer.toString(z));
}
//SEARCH WORD, DISPLAYING SCORE
public void viewWord(View view)
{
String s1= search.getText().toString(); //What word to search
String s2= dbHelper.getData(s1); // If in db, display equivalent score
if(optionTxtView == 0){
//display the score on textview1
tv2.setText(s2);
optionTxtView = 1;
}
else{
if(optionTxtView == 1){
//display the score on textview2
tv3.setText(s2);
optionTxtView = 0;
}
}
//Display search word/s
adapter.add(text.getText().toString());
adapter.notifyDataSetChanged();
if(addClick ==0){
calculate();
addClick = 1;
text.setText("");
generatedString="";
}
else{
if(addClick == 1){
calculate();
addClick = 2;
text.setText("");
generatedString="";
}}
boolean sDouble = false
imgView.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
// Show your Toast message
// double = true;
}
});
and where you are running your game code check the value of sDouble, if its true then double the scoring system inside a for loop, or set an integer value to 0 and increment it for next 3 words, after that set the value of sDouble back to false.
You may also need to make sure that the user doesn't click on the image for the next 3 tries after clicking it once.
Update:
Since your score is a String, you need to parse it to an Integer before performing any mathematical operation, use Integer.parseInt(s2).
so what i have is 2 editText one for minimum number and one for maximum number and when the user clicks the button the app generate a random number between the 2 given number (I got it) but if the user entered the maximum number in the minimum number field the app crashes what i want to accomplish is if the user enters the maximum number in the minimum number field set the maximum number field to minimum number + 1 and try again automatically I tried the code below and its working if the user entered the values in the right please but if the user entered the maximum number in the minimum number field the app crash
Button gen = (Button)findViewById(R.id.button);
final EditText mini = (EditText)findViewById(R.id.mini);
final EditText maxi = (EditText)findViewById(R.id.maxi);
final TextView res = (TextView)findViewById(R.id.result);
final Random r = new Random();
final int[] number = {0};
gen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int minn = Integer.parseInt(mini.getText().toString());
int maxx = Integer.parseInt(maxi.getText().toString());
if (minn>=maxx){
maxi.setText(minn+1);
maxx = Integer.parseInt(maxi.getText().toString());
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));
}else{
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));
}
}
});
}
Replace the following...
maxi.setText(minn+1);
With...
maxi.setText(String.valueOf(minn+1));
Passing int value instead of String value into the setText method causing the crash problem.
I think you should do something like this:
if (minn>=maxx){
maxi.setText(String.valueOf(minn));
mini.setText(String.valueOf(maxx));
maxx = Integer.parseInt(maxi.getText().toString());
minn = Integer.parseInt(minn.getText().toString());
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));
}
number[0] = minn + r.nextInt(maxx - minn + 1);
res.setText(String.valueOf(number[0]));
I have two textViews in my app right now. One textView serves as a Counter and the other servers as the Limit. What I want to accomplish is when the Counter equals the Limit, the Counter is set back to ZERO and the Limit is increased by 100.
private int i = 0;
Spinner spinnerMonsters = (Spinner) findViewById(R.id.spinnerMonsters);
TextView textViewBattleResults = (TextView) findViewById(R.id.textViewBattleResults);
TextView textViewXPValue = (TextView) findViewById(R.id.textViewXPValue);
TextView textViewXpNextLevel = (TextView) findViewById(R.id.textViewXpNextLevel);
if (textViewXPValue.getText().toString().equals(textViewXpNextLevel.getText().toString())) {
int newLimit = Integer.parseInt(textViewXpNextLevel.getText().toString()) + 100;
textViewXpNextLevel.setText(newLimit + "");
textViewXPValue.setText("0");
}
else {
textViewXPValue.setText(textViewXPValue.getText().toString());
textViewXpNextLevel.getText().toString();
}
//Monster sequences
if (spinnerMonsters.getSelectedItem().toString().equals("(0) Training Dummy")) {
textViewBattleResults.setText("You have killed Training Dummy for 10 experience points!");
i = i+10;
textViewXPValue.setText(String.valueOf(i));
}
else {
textViewBattleResults.setText(" ");
textViewXPValue.setText(textViewXPValue.getText().toString());
}
if (spinnerMonsters.getSelectedItem().toString().equals("(2) Cockroach")) {
textViewBattleResults.setText("You have killed a Cockroach for 27 experience points!");
i = i+27;
textViewXPValue.setText(String.valueOf(i));
}
else {
textViewBattleResults.setText(" ");
textViewXPValue.setText(textViewXPValue.getText().toString());
}
}
});
The problem here is that the counter will count up by ten when the button is clicked. But when the Counter equals the Limit, the Limit increases by 100 and the Counter remains at the same number and doesn't reset back to ZERO.
Any suggestions..?
EDIT
Also, how would i do the same thing but if the Counter was Equal to or Greater than the Limit?
You don't reset value of i to zero. You first set text of xpvalue to zero, but then you set it's text to current value ofi, which is not zero.
I want to compare two sets of digits. The first I will generate as a random in code. The second will be entered into an EditText.
I wish to compare the two numbers and see if they match.
Random number:
public static int random() {
if (randomNumberGenerator == null)
initRNG();
return randomNumberGenerator.nextInt();
}
private static Random randomNumberGenerator;
private static synchronized void initRNG() {
if (randomNumberGenerator == null)
randomNumberGenerator = new Random();
}
Showing randomly selected number
display = (TextView) findViewById (R.id.textView1);
display.setText ("Random Number:" + random ());
How can I compare the two numbers?
You can use something like the following:
display = (TextView) findViewById (R.id.textView1);
EditText myEditText = ...; // Find your EditText, maybe by ID?
int random = random(); // Get your random value
display.setText("Random Number:" + random()); // Show the random number
int numberEntered = -1; // Default value for numberEntered; should be a value you wouldn't get randomly
try {
// Here, we try to make a number out of the EditText; if it fails, we get a Toast error
numberEntered = Integer.parseInt(myEditText.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(myEditText.getContext(), "That's not a number!", Toast.LENGTH_LONG).show(); // Tell the user they didn't enter a number
}
if (random == numberEntered) {
// HAPPY DAY, THEY MATCH!
} else {
// No luck :(
}
Just make a subtraction between both numbers and if the result is 0, both numbers match.
EDIT: Subtraction was just a way to show that both were numbers. Obviously it makes more sense to compare both numbers directly rather than subtracting and comparing then.
I am getting an unusual result when attempting to place a value in an array.
I have an array table[] of a simple class result{ int score, long time, string ID}
Intention is to have a sort of leader board.
My code happily finds the correct place to insert a new score if it is in the top 10.
int ix = 0;
int jx = 10; //
while ( ix < jx )
{
if (points > sTable[ix].points)
{
// score is higher move records down
for (jx = mNumRecords - 1; jx >ix ; jx--)
{
sTable[jx] = sTable[jx -1];
}
//now add new score
sTable[ix].score = score; // all good until here
sTable[ix].time = time;
}
ix++;
}
Problem is that when I try to insert the score using sTable[ix].score = score;
The value gets written to sTable[ix].score and also sTable[ix +1].score.
It is repeatable, it occurs at any value of ix, I have single stepped through the code and as far as I can tell the command only executes once.
Has anyone seen this before?
That because you copied the object reference to the next element in the array. You should copy the values, or create a new object:
Option A:
// score is higher move records down
for (jx = mNumRecords - 1; jx >ix ; jx--)
{
sTable[jx].time = sTable[jx -1].time;
sTable[jx].score = sTable[jx -1].score;
}
//now add new score
sTable[ix].score = score; // all good until here
sTable[ix].time = time;
Option B:
for (jx = mNumRecords - 1; jx >ix ; jx--)
{
sTable[jx] = sTable[jx -1];
}
sTable[ix] = new Result(score, time, ""); // Or however you construct the object