gpt4 book ai didi

java - 如何从事件监听器的语句中调用函数?

转载 作者:行者123 更新时间:2023-12-02 01:31:36 24 4
gpt4 key购买 nike

我一直在做一个小项目,但在实现我的想法时遇到了问题。我正在尝试构建的应用程序是一个教育应用程序,它给出随机的基本数学(+,-,*,/)问题,如果学生答对了,它应该显示下一个问题,否则,它应该给他/她还有一次再试一次的机会。

我的问题具体落在最后一部分,我编写了一个函数来显示下一个问题,但我不完全知道单击按钮时如何调用它。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

import java.util.Random;

import static java.lang.String.valueOf;


public class MainActivity extends AppCompatActivity {
TextView fN;
TextView sN;
TextView oP;
TextView out;
TextView ht;
EditText ans;
Button btn;
int answer;
int fNum;
int sNum;
int opNum;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ht.setVisibility(View.GONE);

final String[] ops = {"+","-","÷","×"};
final String[] pass = {
"Very good!",
"Excellent!",
"Nice work!",
"Keep up the good work!"

};
final String[] fail = {
"No. Please try again.",
"Wrong. Try once more.",
"Don't give up!",
"No. Keep trying."
};
public static void nextQ() {
//UI variables
fN = findViewById(R.id.fN);
sN = findViewById(R.id.sN);
oP = findViewById(R.id.oP);
out = findViewById(R.id.out);
ans = findViewById(R.id.Ans);
btn = findViewById(R.id.btn);
//functionalities variables
r = new Random();
fNum = r.nextInt(9);
sNum = r.nextInt(9);
opNum = r.nextInt(ops.length);
if (opNum == 0)
answer = fNum + sNum;
if (opNum == 1)
answer = fNum - sNum;
if (opNum == 2)
answer = fNum / sNum;
if (opNum == 3)
answer = fNum * sNum;

ht.setText(answer);
}
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
ht = findViewById(R.id.hText);



fN.setText(fNum);
sN.setText(sNum);
oP.setText(ops[opNum]);

int usrAns = Integer.parseInt(valueOf(ans.getText()));

int rMSG = r.nextInt(pass.length);

if(answer == usrAns)
out.setText(""+pass[rMSG]);
nextQ();
if(answer != usrAns)
out.setText(""+fail[rMSG]);
}
});
}
}

最佳答案

尝试输入此代码并检查它是否有效?

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
import static java.lang.String.valueOf;

public class MainActivity extends AppCompatActivity {
TextView fN;
TextView sN;
TextView oP;
TextView out;
TextView ht;
EditText ans;
Button btn;
int answer;
int fNum;
int sNum;
int opNum;
Random r;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ht.setVisibility(View.GONE);
btn = findViewById(R.id.btn);
fN = findViewById(R.id.fN);
sN = findViewById(R.id.sN);
oP = findViewById(R.id.oP);
out = findViewById(R.id.out);
ans = findViewById(R.id.Ans);

final String[] ops = {"+", "-", "÷", "×"};
final String[] pass = {
"Very good!",
"Excellent!",
"Nice work!",
"Keep up the good work!"

};
final String[] fail = {
"No. Please try again.",
"Wrong. Try once more.",
"Don't give up!",
"No. Keep trying."
};

btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
ht = findViewById(R.id.hText);


fN.setText(fNum);
sN.setText(sNum);
oP.setText(ops[opNum]);

int usrAns = Integer.parseInt(valueOf(ans.getText()));

int rMSG = r.nextInt(pass.length);

if (answer == usrAns)
out.setText("" + pass[rMSG]);
nextQ();
if (answer != usrAns)
out.setText("" + fail[rMSG]);
}
});
}

public void nextQ() {

//functionalities variables
r = new Random();
fNum = r.nextInt(9);
sNum = r.nextInt(9);
opNum = r.nextInt(ops.length);
if (opNum == 0)
answer = fNum + sNum;
if (opNum == 1)
answer = fNum - sNum;
if (opNum == 2)
answer = fNum / sNum;
if (opNum == 3)
answer = fNum * sNum;
ht.setText(answer);
}
}

关于java - 如何从事件监听器的语句中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com