我一直在完成一个教程,但我的代码中有7个错误需要解决,我完全不知道如何解决手中的错误。
package com.example.youdothemath;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class PlayGame extends Activity implements OnClickListener
{
private SharedPreferences gamePrefs;
public static final String GAME_PREFS = "ArithmeticFile";
//class content
private int level = 0, answer = 0, operator = 0, operand1 = 0, operand2 = 0;
private final int ADD_OPERATOR = 0, SUBTRACT_OPERATOR = 1, MULTIPLY_OPERATOR = 2, DIVIDE_OPERATOR = 3;
private String[] operators = {"+", "-", "x", "/"};
private int[][] levelMin = {
{1, 11, 21},
{1, 5, 10},
{2, 5, 10},
{2, 3, 5}};
private int[][] levelMax = {
{10, 25, 50},
{10, 20, 30},
{5, 10, 15},
{10, 50, 100}};
private Random random;
private TextView question, answerTxt, scoreTxt;
private ImageView response;
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, enterBtn, clearBtn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gamePrefs = getSharedPreferences(GAME_PREFS, 0);
question = (TextView)findViewById(R.id.question);
answerTxt = (TextView)findViewById(R.id.answer);
response = (ImageView)findViewById(R.id.response);
scoreTxt = (TextView)findViewById(R.id.score);
response.setVisibility(View.INVISIBLE);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
btn3 = (Button)findViewById(R.id.btn3);
btn4 = (Button)findViewById(R.id.btn4);
btn5 = (Button)findViewById(R.id.btn5);
btn6 = (Button)findViewById(R.id.btn6);
btn7 = (Button)findViewById(R.id.btn7);
btn8 = (Button)findViewById(R.id.btn8);
btn9 = (Button)findViewById(R.id.btn9);
btn0 = (Button)findViewById(R.id.btn0);
enterBtn = (Button)findViewById(R.id.enter);
clearBtn = (Button)findViewById(R.id.clear);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn0.setOnClickListener(this);
enterBtn.setOnClickListener(this);
clearBtn.setOnClickListener(this);
if(savedInstanceState!=null)
{
//restore state
}
else
{
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
int passedLevel = extras.getInt("level", -1);
if(passedLevel>=0) level = passedLevel;
level=savedInstanceState.getInt("level");
int exScore = savedInstanceState.getInt("score");
scoreTxt.setText("Score: "+exScore);
}
}
random = new Random();
chooseQuestion();
}
private void chooseQuestion()
{
//get a question
answerTxt.setText("= ?");
operator = random.nextInt(operators.length);
operand1 = getOperand();
operand2 = getOperand();
if(operator == SUBTRACT_OPERATOR)
{
while(operand2>operand1)
{
operand1 = getOperand();
operand2 = getOperand();
}
}
else if(operator==DIVIDE_OPERATOR)
{
while((((double)operand1/(double)operand2)%1 > 0) || (operand1==operand2))
{
operand1 = getOperand();
operand2 = getOperand();
}
}
switch(operator)
{
case ADD_OPERATOR:
answer = operand1+operand2;
break;
case SUBTRACT_OPERATOR:
answer = operand1-operand2;
break;
case MULTIPLY_OPERATOR:
answer = operand1*operand2;
break;
case DIVIDE_OPERATOR:
answer = operand1/operand2;
break;
default:
break;
}
question.setText(operand1+" "+operators[operator]+" "+operand2);
}
private int getOperand()
{
//return operand number
return random.nextInt(levelMax[operator][level] - levelMin[operator][level] + 1)
+ levelMin[operator][level];
}
@Override
public void onClick(View view)
{
//button clicked
if(view.getId()==R.id.enter)
{
//enter button
String answerContent = answerTxt.getText().toString();
if(!answerContent.endsWith("?"))
{
//we have an answer
int enteredAnswer = Integer.parseInt(answerContent.substring(2));
int exScore = getScore();
}
if(enteredAnswer==answer) // error: enteredAnswer cannot be resolved to a variable
{
//correct
scoreTxt.setText("Score: "+(exScore+1)); //error: exScore cannot be resolved to a variable
response.setImageResource(R.drawable.tick);
response.setVisibility(View.VISIBLE);
}
else
{
//incorrect
scoreTxt.setText("Score: 0");
response.setImageResource(R.drawable.cross);
response.setVisibility(View.VISIBLE);
setHighScore();
}
chooseQuestion();
}
else if(view.getId()==R.id.clear)
{
//clear button
answerTxt.setText("= ?");
response.setVisibility(View.INVISIBLE);
int enteredNum = Integer.parseInt(view.getTag().toString());
if(answerTxt.getText().toString().endsWith("?"))
answerTxt.setText("= "+enteredNum);
else
answerTxt.append(""+enteredNum);
}
else
{
//number button
}
}
private int getScore()
{
String scoreStr = scoreTxt.getText().toString();
return Integer.parseInt(scoreStr.substring(scoreStr.lastIndexOf(" ")+1));
}
private void setHighScore()
{
//set high score
int exScore = getScore();
if(exScore>0)
{
//we have a valid score
SharedPreferences.Editor scoreEdit = gamePrefs.edit();
DateFormat dateForm = new SimpleDateFormat("dd MMMM yyyy");
String dateOutput = dateForm.format(new Date());
String scores = gamePrefs.getString("highScores", "");
if(scores.length()>0)
{
//we have existing scores
}
else
{
//no existing scores
scoreEdit.putString("highScores", ""+dateOutput+" - "+exScore);
scoreEdit.commit();
}
}
List<Score> scoreStrings = new ArrayList<Score>();
String[] exScores = scores.split("\\|"); // error: scores cannot be resolved
for(String eSc : exScores)
{
String[] parts = eSc.split(" - ");
scoreStrings.add(new Score(parts[0], Integer.parseInt(parts[1])));
}
Score newScore = new Score(dateOutput, exScore); // error: dateOutput cannot be resolved to a variable
scoreStrings.add(newScore);
Collections.sort(scoreStrings);
StringBuilder scoreBuild = new StringBuilder("");
for(int s=0; s<scoreStrings.size(); s++)
{
if(s>=10) break;//only want ten
if(s>0) scoreBuild.append("|");//pipe separate the score strings
scoreBuild.append(scoreStrings.get(s).getScoreText());
}
//write to prefs
scoreEdit.putString("highScores", scoreBuild.toString()); //error: scoreEdit cannot be resolved
scoreEdit.commit(); //error: scoreEdit cannot be resolved
}
protected void onDestroy()
{
setHighScore();
super.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
//save state
int exScore = getScore();
savedInstanceState.putInt("score", exScore);
savedInstanceState.putInt("level", level);
super.onSaveInstanceState(savedInstanceState);
}
}
我评论了问题所在的代码以及问题所在。代码的特色来自PlayGame.java代码。任何帮助将不胜感激。
答案 0 :(得分:0)
试试这个..
第一部分
if(!answerContent.endsWith("?"))
{
//we have an answer
int enteredAnswer = Integer.parseInt(answerContent.substring(2));
int exScore = getScore();
if(enteredAnswer==answer) // error: enteredAnswer cannot be resolved to a variable
{
//correct
scoreTxt.setText("Score: "+(exScore+1)); //error: exScore cannot be resolved to a variable
response.setImageResource(R.drawable.tick);
response.setVisibility(View.VISIBLE);
}
} else
{
//incorrect
scoreTxt.setText("Score: 0");
response.setImageResource(R.drawable.cross);
response.setVisibility(View.VISIBLE);
setHighScore();
}
第二部分
private void setHighScore()
{
//set high score
int exScore = getScore();
if(exScore>0)
{
//we have a valid score
SharedPreferences.Editor scoreEdit = gamePrefs.edit();
DateFormat dateForm = new SimpleDateFormat("dd MMMM yyyy");
String dateOutput = dateForm.format(new Date());
String scores = gamePrefs.getString("highScores", "");
if(scores.length()>0)
{
//we have existing scores
}
else
{
//no existing scores
scoreEdit.putString("highScores", ""+dateOutput+" - "+exScore);
scoreEdit.commit();
List<Score> scoreStrings = new ArrayList<Score>();
String[] exScores = scores.split("\\|"); // error: scores cannot be resolved
for(String eSc : exScores)
{
String[] parts = eSc.split(" - ");
scoreStrings.add(new Score(parts[0], Integer.parseInt(parts[1])));
}
Score newScore = new Score(dateOutput, exScore); // error: dateOutput cannot be resolved to a variable
scoreStrings.add(newScore);
Collections.sort(scoreStrings);
StringBuilder scoreBuild = new StringBuilder("");
for(int s=0; s<scoreStrings.size(); s++)
{
if(s>=10) break;//only want ten
if(s>0) scoreBuild.append("|");//pipe separate the score strings
scoreBuild.append(scoreStrings.get(s).getScoreText());
}
//write to prefs
scoreEdit.putString("highScores", scoreBuild.toString()); //error: scoreEdit cannot be resolved
scoreEdit.commit(); //error: scoreEdit cannot be resolved
}
}
}
答案 1 :(得分:0)
大多数错误都与变量的范围有关。例如,在if块之外声明的变量未在该块之外定义:
if (thisOrThat == true) {
int value = 10;
Log.d("YourApp", "Value is: " + value);
}
Log.d("YourApp", "Value is: " + value); //value is cannot be resolved to a variable
将标记if块之外的日志记录行,因为未在if块之外定义值。
答案 2 :(得分:0)
您的所有问题都与范围相关:
以此为例:
if(!answerContent.endsWith("?"))
{
//we have an answer
int enteredAnswer = Integer.parseInt(answerContent.substring(2));
int exScore = getScore();
}
enteredAnswer
仅在if子句的语句中可见,并且不能在其外部访问。如果要在if语句之外访问它,请在范围之外声明它。
像这样:
int enteredAnswer = 0;
int exScore = 0;
if(!answerContent.endsWith("?"))
{
//we have an answer
enteredAnswer = Integer.parseInt(answerContent.substring(2));
exScore = getScore();
}