This question already has answers here:
Keep pressing a button so that a counter keeps adding by 1 every time
(2 answers)
Closed 5 years ago.
What i need this code to do is that I want it so that once you click the button for the first time, it displays if it's correct and if the button is pressed a second time it displays a new question.
I have updated the code. I have tried implementing the use of counters with the help of some comments however it still does not function correctly.
if(view.getId()==R.id.btnEnter){
counter++;
if(!answerDisplayed.endsWith("?")) {
int useranswer= Integer.parseInt(answerDisplayed.substring(5));
if(useranswer==calculatedAnswer){
//correct
}else{
//incorrect
}
}
if(counter ==2) {
randomQuestion();
}
}
counter = 0;
if(view.getId() == R.id.btnEnter && counter == 0) {
counter++;
// do something
}else{
randomQuestion();
}
at your randomQuestion(), you should decrease the counter.
void randomQuestion(){
.
.
.
--counter;
}
Related
I am doing a questionnaire with questions and written answers.
I need that when adding the answer, press the main button, tell me if it is correct or not and show me the other question of the array, until the array is finished. Here I upload the whole code.
mainbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int i=0;i<question;i++) {
if(answer.getText()==(option[i])) {
message.setText("is correct");
corrects++;
}
else {
message.setText("incorrect");
}
};
You have the right idea, but you need to process the questions one at a time, not all at once inside the ActionListener.
One way to approach this is to keep track of the question outside of the action listener, in this example we use int questionCounter = 0; to keep track of the current question, then we can remove the for loop and process the questions one at a time. Here is a simple example of how it could work using your code, note how we reset the fields and add the next question every time the previous question is answered.
//Use a variable outside of the action listener to keep track of the current question:
int questionCounter = 0;
//Show first question in the text area:
area.setText(question[questionCounter]);
mainbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(answer.getText().equals(option[questionCounter])) {
message.setText("The previous answer was correct");
corrects++;
//Update the total each time you get one correct
correct_answers.setText("correct answers: "+corrects);
}
else {
message.setText("The previous answer was incorrect");
}
//Increment for the next question
questionCounter++;
//Check if there is another question, then show it
if (questionCounter < question.length){
answer.setText("");
area.setText(question[questionCounter]);
}
//Show the overall results if there are no more answers to check
else{
area.setText("Quiz complete, you got " + corrects + " out of " + question.length + " correct.");
}
};
This question already has answers here:
How to Delete Item Without Deleting Position in Recycler View?
(2 answers)
Closed 4 years ago.
I am trying to count clicks based on getAdapterPosition(). And it works properly. Below the code
#Override
public void onClick(View v) {
// Do button click handling here
if ( posisi2==getAdapterPosition() ) {
clickcount--;
tombolbaca.setText("Baca " + clickcount + "x");
if (clickcount <= 0)
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
} // adapter
} // onClick
But when I am trying to count clicks using the comparison of two string, I got the problem. The result is, the computer can only count that once. Can you help me to fix the problem? The problem lays here:
public Button tombolbaca;
private int klik10 = 10;
#Override
public void onClick(View v) {
tombolbaca = (Button) v.findViewById(R.id.buttonbaca);
// Problem here
if( tombolbaca.getText().toString().equals("Baca 10x") ) {
klik10--;
tombolbaca.setText("Baca " + klik10 + "x");
if (klik10 <= 0)
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
}
} // onclick
Ok, got it now.
The problem is in logics: this is your code, written by text instead of code
if( tombolbaca.getText().toString().equals("Baca 10x"){ // this line says "if the text is exactly Baca 10x, go on"
klik10--; // this line says: "make the value of klik10 = klik10 -1
tombolbaca.setText("Baca " + klik10 + "x"); //this line says "set text of tombolbaca as the composition of the strings and the value of klik10
if (klik10 <= 0) //if klik10 is equal or less than 0, do this
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
So, the problem lays here:
First iteration:
klik10 = 10
it enter the first if
klik10 will now be 9
text will be Baca 9x
not less than 1, so skip the if
Second iteration
klik10 = 9 (because you set it before)
not going into if
so, the problem is that you are going in only if text is Baca 10x, but after the first iteration it won't be that anymore.
A solution could be this:
#Override
public void onClick(View v) {
klik10--;
tombolbaca.setText("Baca " + klik10 + "x");
if (klik10 <= 0)
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
}
The if, as it is written, is not necessary. you can just remove it and the code will work.
If not, tell me why there is the if clause and I will fix above code :)
PS:
If you want to check if the text is the correct counter, do the following if:
if(tombolbaca.getText().String().equals("Baca " + klik10.toString() + "x"){
...
In second attempt the comparation will be between "Baca 9x".equals("Baca 10x") and your if statement not will work
Try as follow
if( !tombolbaca.getText().toString().equals("Baca 0x") ) { //using "Baca 0x"
...
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Good day everyone, so i am writing a quiz app and score is based on what the user selects. I used radio buttons, check box and edit text tags for answering questions. When the user clicks a radio button, or a check box, the score updates, but when the user enters the correct text in the EditText field, the score does not update. I used an if else statement to check if the string entered matches an answer.
public void startFinalScreen(View v) {
EditText text = (EditText) findViewById(R.id.edit_text_view_answer);
int questionNineScore = getIntent().getIntExtra("GNS", 0);
int finalScore = questionNineScore;
String answer = "William Shakespeare";
if (text.getText().toString() == answer) {
qNineScore = qNineScore + 1;
}
finalScore = qNineScore;
}
I used .getIntent() to get score from a previous activity. After i do this, the score does not add +1 even after typing the correct answer.
Instead of
text.getText().toString() == answer
use
if (text.getText().toString().equals(answer)) {
qNineScore = qNineScore + 1;
}
Disclaimer: I'm really new at this and I apologize in advance if:
1) my question has already been asked (I've tried searching and had a lot of trouble finding what I needed)
or 2) if I'm not asking the question correctly.
Basically, I'm trying to make a game where pressing the spacebar triggers a sort of "super-power" that will perform a set of actions just once. Afterwards, if they try to press it again, it'll run up some sort of dialogue box that says their one-time super-power has already been used.
What I have:
try {
Key move = canvas.getLastKey();
int space = 0;
if(move == Key.SPACE) {
if (space == 0) {
space = 1;
}
if (space == 2){
JOptionPane.showMessageDialog(null, "superpower already used");
}
}
if( space == 1 ) {
//action here
canvas.resetKey();
space = 2;
}
}
Right now, the super-hero action is on an endless loop but if I reset the key here:
if(move == Key.SPACE) {
if (space == 0) {
space = 1;
canvas.resetKey();
}
the user can just use the super-power over and over again. Any help would be appreciated!
In the third line, you have written int space=0 so your variable is constantly reset to 0...
You have to initialize it elsewhere (the beginning of your program is a good place for any global variable).
You should consider moving int space = 0, outside of the try block. I suppose your try block gets invoked repeatedly, so you should declare this variable under a global scope.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax Question IF ELSE (Java)
I am trying to make a calculator which shows a message if no value is entered in editbox. But it FC's!! I am making apps after long time so I am quite confused.
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
double a=0;
double b=0;
double c=0;
EditText edit;
EditText edit2;
TextView edit3;
String lname="";
edit=(EditText)findViewById(R.id.edit);
edit2=(EditText)findViewById(R.id.edit2);
edit3=(TextView)findViewById(R.id.edit3); // everything defined above
String editstr= edit.getText().toString(); // real work starts
if(editstr.contentEquals(lname))
edit3.setText("Enter value");
else
a=Double.parseDouble(edit.getText().toString()); // else add the stuff
b=Double.parseDouble(edit2.getText().toString());
c=a+b;
edit3.setText(Double.toString(c));
} };
put brackets around your if-else, currently in case of else it only executes the first line, other lines are executed no matter if your if passes or fails.
if(editstr.contentEquals(lname)) {
edit3.setText("Enter value");
} else {
a=Double.parseDouble(edit.getText().toString()); // else add the stuff
b=Double.parseDouble(edit2.getText().toString());
c=a+b;
edit3.setText(Double.toString(c));
}
As written, only the a=Double.parseDouble(edit.getText().toString()); is affected by the else. If you want the rest of it there, surround the block in {}
It does work, it just never fires.
You should do
if(editStr.isEmpty())
editStr = "Enter value";
else
{
//editStr.equals("someValue"); //test against some value
//rest
}