gpt4 book ai didi

java - 奇怪的错误+如何使这段代码更高效?

转载 作者:行者123 更新时间:2023-12-02 08:06:19 24 4
gpt4 key购买 nike

一旦我使用 Game() 方法,我的应用程序就会崩溃,为什么?另外我想知道是否有可能使我的代码在占用的空间方面更短并且更好。

代码:

package com.aleksei.etb;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

public class ETBetaActivity extends Activity implements View.OnClickListener {

Button answer_1,
answer_2,answer_3,
answer_4,main;

TextView q_textview,
TextView tip;

private String aString[];

private int i1 = 0;
private int correct = 0;

private boolean alive = false;

MediaPlayer button_click;

private String[] questions ={"Question 1" , "Question 2","Question 5"};
private String[] answers_correct ={"Correct answer 1", "Correct answer 2","Correct answer 3","Correct answer 4","Correct answer 5"};

List<String> question_list = new ArrayList<String>();
List<String> answer_list_correct = new ArrayList<String>();


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getData();
}

@Override
public void onClick(View view) {

button_click = MediaPlayer.create(this, R.raw.button_click);
button_click.start();
switch(view.getId()){

case R.id.button5: //main
if(!alive)
alive = true;
break;
case R.id.button1: //answer_1
if(alive == false)
return;
if(correct(1))
correct++;
break;
case R.id.button2: //answer_2
if(alive == false)
return;
if(correct(2))
correct++;
break;
case R.id.button3: //answer_3
if(alive == false)
return;
if(correct(3))
correct++;
break;
case R.id.button4: //answer_3
if(alive == false)
return;
if(correct(4))
correct++;
break;
default:
break;
}
Game();
}

private boolean correct(int button){

try {
for (int i = 0; i < answers_correct.length; i++){
if(button == 1 && aString[0] == answers_correct[i]
|| button == 2 && aString[1] == answers_correct[i]
|| button == 3 && aString[2] == answers_correct[i]
|| button == 4 && aString[3] == answers_correct[i])
return true;
}
}
catch(Exception ex){
System.out.println(ex);
}
return false;
}

private void Game(){

if(i1 > questions.length) //no more questions
return;
main.setText("Next");
try {
String answer_list[][] = {
{answers_correct[i1], "Answer 1-2" , "Answer 1-3" , "Answer 1-4"},
{answers_correct[i1], "Answer 2-2" , "Answer 2-3" , "Answer 2-4"},
{answers_correct[i1], "Answer 3-2" , "Answer 3-3" , "Answer 3-4"},
{answers_correct[i1], "Answer 4-2" , "Answer 4-3" , "Answer 4-4"},
{answers_correct[i1], "Answer 5-2" , "Answer 5-3" , "Answer 5-4"}};

Collections.shuffle(Arrays.asList(answer_list[i1]));
answer_1.setText(answer_list[i1][0]);
answer_2.setText(answer_list[i1][1]);
answer_3.setText(answer_list[i1][2]);
answer_4.setText(answer_list[i1][3]);
aString[0] = answer_list[i1][0];
aString[1] = answer_list[i1][1];
aString[2] = answer_list[i1][2];
aString[3] = answer_list[i1][3];
q_textview.setText(questions[i1]);
}
catch(Exception e){
System.out.println(e);
}
tip.setText(correct);

/*questions = question_list.toArray(new String[question_list.size()]);
answers_correct = answer_list_correct.toArray(new String[answer_list_correct.size()]);

question.setText(questions[i1]);

answer_list_correct.remove(questions[i1]);
question_list.remove(questions[i1]);*/
i1++;
}
private void getData(){
//Getting the data
main = (Button) findViewById(R.id.button5);
answer_1 = (Button) findViewById(R.id.button1);
answer_2 = (Button) findViewById(R.id.button2);
answer_3 = (Button) findViewById(R.id.button3);
answer_4 = (Button) findViewById(R.id.button4);
q_textview = (TextView) findViewById(R.id.question);
tip = (TextView) findViewById(R.id.answ1);

//Making the buttons, actually work
main.setOnClickListener(this);
answer_1.setOnClickListener(this);
answer_2.setOnClickListener(this);
answer_3.setOnClickListener(this);
answer_4.setOnClickListener(this);

//Resets the text
//Note to self: Replace with another ContectView
main.setText("Begin!");
answer_4.setText("");
answer_3.setText("");
answer_2.setText("");
answer_1.setText("");

/* for(String x : questions) {
for(String y : answers_correct){

answer_list_correct.add(y);
question_list.add(x);

Collections.shuffle(answer_list_correct);
Collections.shuffle(question_list);

}
} */
}
}

致以诚挚的问候。

最佳答案

Game() ,你检查i1 > questions.length在顶部,但尝试 q_textview.setText(questions[i1]);靠近底部。如果i1 == questions.length ,这会抛出一个 ArrayIndexOutOfBoundsException (希望我正确地记住了这个名字),使它成为 i1 >= questions.length 。另外,您使用 i1作为 answers_correct 的索引和answer_list ,这不会超出粘贴的 questions 的范围和answers_correct ,但可能是真实的(不过不太可能,你的问题不会比答案多,不是吗?)。

correct ,你的if声明

if(button == 1 && aString[0] == answers_correct[i]
|| button == 2 && aString[1] == answers_correct[i]
|| button == 3 && aString[2] == answers_correct[i]
|| button == 4 && aString[3] == answers_correct[i])
return true;

可以缩短为

if (aString[button-1] == answers_correct[i]) return true;

(除非按钮的值<1或>4,那么您也需要检查button >= 1 && button <= 4)。但是,使用 ==比较字符串是危险的,而且几乎肯定是错误的。 ==比较引用的相等性,因此 string1 == string2仅当两者都引用同一个 String 实例时才为 true。如果(似乎是这种情况)您的所有字符串都来自源代码中的字符串文字,那么它会起作用,因为每个文字只有一个实例,但如果外部字符串可以进入游戏,那么您必须使用equals比较字符串。使用equals同样在那些情况下 ==会(有点意外)起作用。

关于java - 奇怪的错误+如何使这段代码更高效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116511/

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