gpt4 book ai didi

java - 全局变量重置为 0

转载 作者:搜寻专家 更新时间:2023-11-01 09:48:55 27 4
gpt4 key购买 nike

在调用 setWordsQuestion() 之前,我将全局变量 inext 正确设置为随机值。一旦我输入 setWordsQuestion(),该值就是 0。你知道我如何调试它吗?

enter image description here

并重置为0:

enter image description here

这是java文件:

public class ExercisesWords_fragment extends Fragment {
static Context context;
String login;
String fileMenuCard = null;

InputStreamReader isr;
InputStream fIn;
BufferedReader input_card = null;

int level, lang;
int ihelp;

String fileLogin;

RadioGroup rg;
RadioButton rb1, rb2, rb3, rb4;
EditText edt;
TextView txt, question;
Button validation, next, help;

int MAXWORDS = 42;
String[] words_questions = new String[MAXWORDS];
String[] words_cb1 = new String[MAXWORDS];
String[] words_cb2 = new String[MAXWORDS];
String[] words_cb3 = new String[MAXWORDS];
String[] words_cb4 = new String[MAXWORDS];
String[] words_response = new String[MAXWORDS];
int[] words_level = new int[MAXWORDS];

int MAXTEMP = 5;
int[] tmp;

int inext_question, inext;

public ExercisesWords_fragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.exercises_words_fragment, container, false);
}

@Override
public void onAttach(Activity activity){
super.onAttach(activity);
}

@Override
public void onPause() {
closeFileWords();
super.onPause();
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();

fileMenuCard = "words";
login = getArguments().getString("login");
lang = getArguments().getInt("lang");
level = getArguments().getInt("level");

fileLogin = login + "_words.txt";

initViewFind();
initExerciseWords();
nextWordsQuestion();

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

edt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
validateResponse();
}
return false;
}
});

validation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validateResponse();
}
});

next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextWordsQuestion();
}
});

help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ihelp = 1;
hideKeyboard();
helpWords();
ihelp = 0;
}
});


}

private void drawBTN() {
if(lang == 0) {
validation.setText(R.string.pro_validation);
validation.setVisibility(View.VISIBLE);
next.setText(R.string.next);
next.setVisibility(View.VISIBLE);
help.setText(R.string.help);
help.setVisibility(View.VISIBLE);
}
else {
validation.setText(R.string.pro_validation_fr);
validation.setVisibility(View.VISIBLE);
next.setText(R.string.next_fr);
next.setVisibility(View.VISIBLE);
help.setText(R.string.help_fr);
help.setVisibility(View.VISIBLE);
}
}



private void nextWordsQuestion() {
int inext;
Random r;
r = new Random();
inext = r.nextInt(MAXWORDS);

if (inext_question < MAXTEMP) {
tmp[inext_question] = inext;
inext_question++;

setWordsQuestion();
return;
}

for (int i = 0; i < MAXTEMP; i++) {
if (inext == tmp[i]) {
r = new Random();
inext = r.nextInt(MAXWORDS);
i--;
}
}

System.arraycopy(tmp, 1, tmp, 0, MAXTEMP - 1);

tmp[MAXTEMP - 1] = inext;

setWordsQuestion();
}

private void setWordsQuestion(){
question.setText(words_questions[inext]);
question.setVisibility(View.VISIBLE);
edt.setText("");

rb1.setText(words_cb1[inext]);
rb2.setText(words_cb2[inext]);
rb3.setText(words_cb3[inext]);
rb4.setText(words_cb4[inext]);

rb1.setVisibility(View.INVISIBLE);
rb2.setVisibility(View.INVISIBLE);
rb3.setVisibility(View.INVISIBLE);
rb4.setVisibility(View.INVISIBLE);
rg.clearCheck();

hideKeyboard();

}

private void validateResponse(){
String response;

if(ihelp == 1){
int sel = rg.getCheckedRadioButtonId();
if(sel < 0) return;
RadioButton rb = (RadioButton) getActivity().findViewById(sel);
response = rb.getText().toString();
}
else {
response = edt.getText().toString();
}
TextView resp = (TextView) getActivity().findViewById(R.id.resp_words);
if(response.equals(words_response[inext])) {

next.setTextColor(Color.GREEN);
next.setVisibility(View.VISIBLE);

resp.setText(R.string.resp_card);
resp.setTextColor(Color.GREEN);
resp.setVisibility(View.VISIBLE);

}
else {
resp.setText(R.string.resp_card_no);
resp.setTextColor(Color.RED);
resp.setVisibility(View.VISIBLE);
}

}

private void helpWords(){
hideKeyboard();

rb1.setText(words_cb1[inext]);
rb1.setVisibility(View.VISIBLE);

rb2.setText(words_cb2[inext]);
rb2.setVisibility(View.VISIBLE);

rb3.setText(words_cb3[inext]);
rb3.setVisibility(View.VISIBLE);

rb4.setText(words_cb4[inext]);
rb4.setVisibility(View.VISIBLE);

}

private void hideKeyboard(){
EditText e = (EditText) getActivity().findViewById(R.id.words_answer);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
}

private void openWords(){
String l = null;
try {
if (lang == 0)
fIn = context.getResources().getAssets()
.open(fileMenuCard+".txt", Context.MODE_PRIVATE);
else
fIn = context.getResources().getAssets()
.open(fileMenuCard+"_fr.txt", Context.MODE_PRIVATE);
isr = new InputStreamReader(fIn);
input_card = new BufferedReader(isr);
} catch (Exception e) {
e.getMessage();
}

for(int i=0;i<MAXWORDS;i++){
try {
l = input_card.readLine();
} catch (IOException e) {
e.printStackTrace();
}

if (l != null) {
StringTokenizer tokens = new StringTokenizer(l, ";");
words_questions[i] = tokens.nextToken();
words_level[i] = Integer.valueOf(tokens.nextToken());
words_cb1[i] = tokens.nextToken();
words_cb2[i] = tokens.nextToken();
words_cb3[i] = tokens.nextToken();
words_cb4[i] = tokens.nextToken();
words_response[i] = tokens.nextToken();
}
}
}

private void closeFileWords(){
try {
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
input_card.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private void initExerciseWords(){
tmp = new int[MAXTEMP];

for(int i=0;i<MAXTEMP;i++)
tmp[i] = -1;

inext_question = 0;

openWords();

}

private void initViewFind(){
edt = (EditText) getActivity().findViewById(R.id.words_answer);
txt = (TextView) getActivity().findViewById(R.id.title_words);
validation = (Button) getActivity().findViewById(R.id.validation_words);
next = (Button) getActivity().findViewById(R.id.next_words);
help = (Button) getActivity().findViewById(R.id.help_words);
question = (TextView) getActivity().findViewById(R.id.words_question);
rg = (RadioGroup) getActivity().findViewById(R.id.rg_words);
rb1 = (RadioButton) getActivity().findViewById(R.id.radio1_words);
rb2 = (RadioButton) getActivity().findViewById(R.id.radio2_words);
rb3 = (RadioButton) getActivity().findViewById(R.id.radio3_words);
rb4 = (RadioButton) getActivity().findViewById(R.id.radio4_words);

if(lang == 0)
txt.setText(R.string.words_fr);
else
txt.setText(R.string.words_title_fr);

}

最佳答案

您正在设置一个隐藏实例成员的局部变量:

private void nextWordsQuestion() {
int inext; // remove this line
...
inext = r.nextInt(MAXWORDS);
...
setWordsQuestion();
...
}

因此,使用实例成员的 setWordsQuestion 看不到您在 nextWordsQuestion 中设置的值。

关于java - 全局变量重置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36673783/

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