【发布时间】:2016-10-21 09:40:58
【问题描述】:
我目前正在做一个学校项目(一个小型 android 游戏),到目前为止,我已经编写了一个代码,它会在活动 InGame 启动后 2 秒生成一个 random equation 并在 @987654323 中显示它@。再过 5 秒后,第二个等式生成并显示在不同的文本视图中。现在用户必须通过按下按钮bigger 或smaller 来决定第二个等式的结果是否比第一个更大。如果它是正确的,应该显示下一个方程,它会像这样继续下去,直到用户判断错误。
到目前为止,这是我的代码:
(第一个方程的代码):
// Generate random equation and display it in textview
String[] operationSet = new String[]{"+", "-", "/", "*"};
String equation;
static double doubleAnswer1;
public void start1() {
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;
numbers.add(number);
}
String equation = "";
for (int i = 0; i < numOfOperations; i++) {
equation += numbers.get(i);
equation += operations.get(i);
}
equation += numbers.get(numbers.size() -1);
TextView TextEquation = (TextView)findViewById(R.id.textView_first_equation);
TextEquation.setText(equation);
// Evaluate the result of the equation
double doubleAnswer1 = eval(equation);
String stringAnswer = Double.toString(doubleAnswer1);
TextView textAnswer = (TextView)findViewById(R.id.textView4);
textAnswer.setText(stringAnswer);
}
(第二个等式的代码(与第一个等式基本相同,只是字符串和双打的名称不同)):
String equation2;
static double doubleAnswer2;
public void start2() {
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;
numbers.add(number);
}
String equation2 = "";
for (int i = 0; i < numOfOperations; i++) {
equation2 += numbers.get(i);
equation2 += operations.get(i);
}
equation2 += numbers.get(numbers.size() -1);
TextView TextEquation = (TextView)findViewById(R.id.textView3);
TextEquation.setText(equation2);
// Evaluate the result of the equation
double doubleAnswer2 = eval(equation2);
String stringAnswer = Double.toString(doubleAnswer2);
TextView textAnswer = (TextView)findViewById(R.id.textView_result2);
textAnswer.setText(stringAnswer);
}
这是我的 onCreate 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ingame);
// Display first equation 2 seconds after the activity is launched
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
start1();
}
}, 2000);
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
start2();
}
}, 7000);
// Check if user was right or wrong
final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
Log.v("TAG", "you are right");
} else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
Log.v("TAG", "you are right");
} else {
Log.v("TAG", "you are wrong");
}
}
};
buttonBigger.setOnClickListener(listener);
buttonSmaller.setOnClickListener(listener);
}
应用程序启动正确,它还显示first and second 等式,但是当我按下其中一个按钮时,它会在logcat you are wrong 中告诉我,但我决定100% 正确。但是,如果我调试应用程序,它会告诉我 doubleAnswer1 和 doubleAnswer2 是 both = 0. 这就是为什么它总是告诉我“你错了”。我不知道如何解决这个问题,也许我需要将 doubleAnswer1 和 doubleAnswer2 存储在某个地方。
我真的不知道该怎么做,所以如果有人知道该怎么做,那真的会帮助我。
如果我的问题有任何不清楚的地方,请随时提出,我会尽力澄清问题。
提前感谢您的帮助!
【问题讨论】:
-
因为您将
doubleAnswer1和doubleAnswer2声明为方法内部的局部变量,并且永远不会触及您之前声明的静态变量。因此,它们都将保持默认值。 -
我该如何改变呢? (我是 android 开发新手)
-
顺便说一句:这就是所谓的 shadowing 只是一种不好的做法:您尝试从不对不同的事物使用相同的名称。
-
您只需不使用局部变量,而是使用类中的静态变量(针对给定问题)。当然,在“真实”设计中使用静态类范围变量也不是一个好主意。
-
@G.M 与android开发无关,这是基本的
java。
标签: java android android-studio if-statement double