Java 功能在android上无法正常工作
我在android应用程序上使用的一个函数有点问题。 这个函数不太难,它只是一个简单的计算,有两个随机整数。 问题是我有一个标签,我想把它的文本改成“问题”,这样用户就可以解决它了。我知道函数有问题,但我不太明白是什么。我最接近的猜测是返回部分是错误的(因为它是空的),但到目前为止我无法修复它 这是我的代码:Java 功能在android上无法正常工作,java,android,function,android-intent,return,Java,Android,Function,Android Intent,Return,我在android应用程序上使用的一个函数有点问题。 这个函数不太难,它只是一个简单的计算,有两个随机整数。 问题是我有一个标签,我想把它的文本改成“问题”,这样用户就可以解决它了。我知道函数有问题,但我不太明白是什么。我最接近的猜测是返回部分是错误的(因为它是空的),但到目前为止我无法修复它 这是我的代码: import java.util.Random; import android.app.Activity; import android.os.Bundle; import android
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Game extends Activity implements OnClickListener {
private static final String TAG = "Game";
int game[];
public static final String KEY_DIFFICULTY = "com.example.coursework1.dificulty";
public static final int DIFFICULTY_NOVICE = 0;
public static final int DIFFICULTY_EASY = 1;
public static final int DIFFICULTY_MEDIUM = 2;
public static final int DIFFICULTY_GURU = 3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE);
game = getGame(diff);
}
public int[] getGame(int diff) {
// TODO Auto-generated method stub
String operators [] = {"+","-","*","/"};
Random op1, op2, op3, op4, op5, op6;
if(diff == getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE)){
op1 = new Random();
op2 = new Random();
int operator = new Random().nextInt(operators.length);
String random = (operators[operator]);
int generate1 = op1.nextInt(10);
int generate2 = op2.nextInt(10);
int operation;
if(random == "+"){
operation = generate1 + generate2;
TextView setProblemToView = (TextView) findViewById(R.id.problem);
setProblemToView.setText("generate1" + random + "generate2");
}
}
return null;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
为什么要使用int-ruturn而必须始终返回null?这样使用:
public void getGame(int diff) {
String operators [] = {"+","-","*","/"};
Random op1, op2, op3, op4, op5, op6;
if(diff == getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE)){
op1 = new Random();
int operator = new Random().nextInt(operators.length);
String random = (operators[operator]);
int generate1 = op1.nextInt(10);
int generate2 = op1.nextInt(10);
int operation;
if( "+".equalsIgnoreCase(random) ){
operation = generate1 + generate2;
TextView setProblemToView = (TextView) findViewById(R.id.problem);
setProblemToView.setText("generate1" + random + "generate2");
}
}
}
getGame(diff);
就这样称呼它:
public void getGame(int diff) {
String operators [] = {"+","-","*","/"};
Random op1, op2, op3, op4, op5, op6;
if(diff == getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE)){
op1 = new Random();
int operator = new Random().nextInt(operators.length);
String random = (operators[operator]);
int generate1 = op1.nextInt(10);
int generate2 = op1.nextInt(10);
int operation;
if( "+".equalsIgnoreCase(random) ){
operation = generate1 + generate2;
TextView setProblemToView = (TextView) findViewById(R.id.problem);
setProblemToView.setText("generate1" + random + "generate2");
}
}
}
getGame(diff);
在我的编辑中解决了相同的随机问题,请查看更多信息。注意:不要使用
==
比较字符串值。使用equals()
。这里到底出了什么问题?这里的问题是,当我在avd上测试应用程序时,问题字符串的值仍然是相同的(这就是问题)。可能重复@ZouZou感谢提示。我现在就改!当然,不要返回相同的值。它就在我面前,我没有看到它。谢谢:)