Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-第一次正确尝试时无法增加计数器_Android_If Statement_For Loop - Fatal编程技术网

Android-第一次正确尝试时无法增加计数器

Android-第一次正确尝试时无法增加计数器,android,if-statement,for-loop,Android,If Statement,For Loop,我试图添加mod this,这样每当玩家在第一次尝试时猜到标志时,就会增加一个计数器。完成10个标志后,该值将显示在末尾。到目前为止,我只让它显示猜测的数量和正确答案的百分比 import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List;

我试图添加mod this,这样每当玩家在第一次尝试时猜到标志时,就会增加一个计数器。完成10个标志后,该值将显示在末尾。到目前为止,我只让它显示猜测的数量和正确答案的百分比

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FlagQuizGame extends Activity 
{
// String used when logging error messages
private static final String TAG = "FlagQuizGame Activity";

private List<String> fileNameList; // flag file names
private List<String> quizCountriesList; // names of countries in quiz
private Map<String, Boolean> regionsMap; // which regions are enabled
private String correctAnswer; // correct country for the current flag
private int totalGuesses; // number of guesses made
private int correctAnswers; // number of correct guesses
private int guessRows; // number of rows displaying choices
private Random random; // random number generator
private Handler handler; // used to delay loading next flag
private Animation shakeAnimation; // animation for incorrect guess

private TextView answerTextView; // displays Correct! or Incorrect!
private TextView questionNumberTextView; // shows current question #
private ImageView flagImageView; // displays a flag
private TableLayout buttonTableLayout; // table of answer Buttons
private int counter;

// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState); // call the superclass's method
  setContentView(R.layout.main); // inflate the GUI

  fileNameList = new ArrayList<String>(); // list of image file names
  quizCountriesList = new ArrayList<String>(); // flags in this quiz
  regionsMap = new HashMap<String, Boolean>(); // HashMap of regions
  guessRows = 1; // default to one row of choices
  random = new Random(); // initialize the random number generator
  handler = new Handler(); // used to perform delayed operations
  counter = 0;
  // load the shake animation that's used for incorrect answers
  shakeAnimation = 
     AnimationUtils.loadAnimation(this, R.anim.incorrect_shake); 
  shakeAnimation.setRepeatCount(3); // animation repeats 3 times 

  // get array of world regions from strings.xml
  String[] regionNames = 
     getResources().getStringArray(R.array.regionsList);

  // by default, countries are chosen from all regions
  for (String region : regionNames )
     regionsMap.put(region, true);

  // get references to GUI components
  questionNumberTextView = 
     (TextView) findViewById(R.id.questionNumberTextView);
  flagImageView = (ImageView) findViewById(R.id.flagImageView);
  buttonTableLayout = 
     (TableLayout) findViewById(R.id.buttonTableLayout);
  answerTextView = (TextView) findViewById(R.id.answerTextView);

  // set questionNumberTextView's text
  questionNumberTextView.setText(
     getResources().getString(R.string.question) + " 1 " + 
     getResources().getString(R.string.of) + " 10");

  resetQuiz(); // start a new quiz
} // end method onCreate

// set up and start the next quiz 
private void resetQuiz() 
{      
  // use the AssetManager to get the image flag 
  // file names for only the enabled regions
  AssetManager assets = getAssets(); // get the app's AssetManager
  fileNameList.clear(); // empty the list

  try 
  {
     Set<String> regions = regionsMap.keySet(); // get Set of regions

     // loop through each region
     for (String region : regions) 
     {
        if (regionsMap.get(region)) // if region is enabled
        {
           // get a list of all flag image files in this region
           String[] paths = assets.list(region);

           for (String path : paths) 
              fileNameList.add(path.replace(".png", ""));
        } // end if
     } // end for
  } // end try
  catch (IOException e) 
  {
     Log.e(TAG, "Error loading image file names", e);
  } // end catch

  correctAnswers = 0; // reset the number of correct answers made
  totalGuesses = 0; // reset the total number of guesses the user made
  quizCountriesList.clear(); // clear prior list of quiz countries

  // add 10 random file names to the quizCountriesList
  int flagCounter = 1; 
  int numberOfFlags = fileNameList.size(); // get number of flags

  while (flagCounter <= 10) 
  {
     int randomIndex = random.nextInt(numberOfFlags); // random index

     // get the random file name
     String fileName = fileNameList.get(randomIndex);

     // if the region is enabled and it hasn't already been chosen
     if (!quizCountriesList.contains(fileName)) 
     {
        quizCountriesList.add(fileName); // add the file to the list
        ++flagCounter;
     } // end if
  } // end while

  loadNextFlag(); // start the quiz by loading the first flag
} // end method resetQuiz

// after the user guesses a correct flag, load the next flag
private void loadNextFlag() 
{
  // get file name of the next flag and remove it from the list
  String nextImageName = quizCountriesList.remove(0);
  correctAnswer = nextImageName; // update the correct answer

  answerTextView.setText(""); // clear answerTextView 

  // display the number of the current question in the quiz
  questionNumberTextView.setText(
     getResources().getString(R.string.question) + " " + 
     (correctAnswers + 1) + " " + 
     getResources().getString(R.string.of) + " 10");

  // extract the region from the next image's name
  String region = 
     nextImageName.substring(0, nextImageName.indexOf('-'));

  // use AssetManager to load next image from assets folder
  AssetManager assets = getAssets(); // get app's AssetManager
  InputStream stream; // used to read in flag images

  try
  {
     // get an InputStream to the asset representing the next flag
     stream = assets.open(region + "/" + nextImageName + ".png");

     // load the asset as a Drawable and display on the flagImageView
     Drawable flag = Drawable.createFromStream(stream, nextImageName);
     flagImageView.setImageDrawable(flag);                       
  } // end try
  catch (IOException e)  
  {
     Log.e(TAG, "Error loading " + nextImageName, e);
  } // end catch

  // clear prior answer Buttons from TableRows
  for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
     ((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();

  Collections.shuffle(fileNameList); // shuffle file names

  // put the correct answer at the end of fileNameList
  int correct = fileNameList.indexOf(correctAnswer);
  fileNameList.add(fileNameList.remove(correct));

  // get a reference to the LayoutInflater service
  LayoutInflater inflater = (LayoutInflater) getSystemService(
     Context.LAYOUT_INFLATER_SERVICE);

  // add 3, 6, or 9 answer Buttons based on the value of guessRows
  for (int row = 0; row < guessRows; row++) 
  {
     TableRow currentTableRow = getTableRow(row);

     // place Buttons in currentTableRow
     for (int column = 0; column < 3; column++) 
     {
        // inflate guess_button.xml to create new Button
        Button newGuessButton = 
           (Button) inflater.inflate(R.layout.guess_button, null);

        // get country name and set it as newGuessButton's text
        String fileName = fileNameList.get((row * 3) + column);
        newGuessButton.setText(getCountryName(fileName));

        // register answerButtonListener to respond to button clicks
        newGuessButton.setOnClickListener(guessButtonListener);
        currentTableRow.addView(newGuessButton);
     } // end for
  } // end for

  // randomly replace one Button with the correct answer
  int row = random.nextInt(guessRows); // pick random row
  int column = random.nextInt(3); // pick random column
  TableRow randomTableRow = getTableRow(row); // get the TableRow
  String countryName = getCountryName(correctAnswer);
  ((Button)randomTableRow.getChildAt(column)).setText(countryName);    
} // end method loadNextFlag

// returns the specified TableRow
private TableRow getTableRow(int row)
{
  return (TableRow) buttonTableLayout.getChildAt(row);
} // end method getTableRow

// parses the country flag file name and returns the country name
private String getCountryName(String name)
{
  return name.substring(name.indexOf('-') + 1).replace('_', ' ');
} // end method getCountryName

// called when the user selects an answer
private void submitGuess(Button guessButton) 
{
  String guess = guessButton.getText().toString();
  String answer = getCountryName(correctAnswer);
  ++totalGuesses; // increment the number of guesses the user has made

  // if the guess is correct
  if (guess.equals(answer)) 
  {
     ++correctAnswers; // increment the number of correct answers

     // display "Correct!" in green text
     answerTextView.setText(answer + "!");
     answerTextView.setTextColor(
        getResources().getColor(R.color.correct_answer));

     disableButtons(); // disable all answer Buttons

     // if the user has correctly identified 10 flags
     if (correctAnswers == 10) 
     {
        // create a new AlertDialog Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle(R.string.reset_quiz); // title bar string

        // set the AlertDialog's message to display game results
        builder.setMessage(String.format("%d %s, %.02f%% %s", 
           totalGuesses, getResources().getString(R.string.guesses), 
           (1000 / (double) totalGuesses), 
           getResources().getString(R.string.correct)));

        builder.setCancelable(false); 

        // add "Reset Quiz" Button                              
        builder.setPositiveButton(R.string.reset_quiz,
           new DialogInterface.OnClickListener()                
           {                                                       
              public void onClick(DialogInterface dialog, int id) 
              {
                 resetQuiz();                                      
              } // end method onClick                              
           } // end anonymous inner class
        ); // end call to setPositiveButton

        // create AlertDialog from the Builder
        AlertDialog resetDialog = builder.create();
        resetDialog.show(); // display the Dialog
     } // end if 
     else // answer is correct but quiz is not over 
     {
        // load the next flag after a 1-second delay
        handler.postDelayed(
           new Runnable()
           { 
              @Override
              public void run()
              {
                 loadNextFlag();
              }
           }, 1000); // 1000 milliseconds for 1-second delay
     } // end else
  } // end if
  else // guess was incorrect  
  {
     // play the animation
     flagImageView.startAnimation(shakeAnimation);

     // display "Incorrect!" in red 
     answerTextView.setText(R.string.incorrect_answer);
     answerTextView.setTextColor(
        getResources().getColor(R.color.incorrect_answer));
     guessButton.setEnabled(false); // disable the incorrect answer
  } // end else
} // end method submitGuess

// utility method that disables all answer Buttons 
private void disableButtons()
{
  for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
  {
     TableRow tableRow = (TableRow) buttonTableLayout.getChildAt(row);
     for (int i = 0; i < tableRow.getChildCount(); ++i)
        tableRow.getChildAt(i).setEnabled(false);
  } // end outer for
} // end method disableButtons

// create constants for each menu id
private final int CHOICES_MENU_ID = Menu.FIRST;
private final int REGIONS_MENU_ID = Menu.FIRST + 1;

// called when the user accesses the options menu
@Override
public boolean onCreateOptionsMenu(Menu menu)             
{            
  super.onCreateOptionsMenu(menu);                        

  // add two options to the menu - "Choices" and "Regions"
  menu.add(Menu.NONE, CHOICES_MENU_ID, Menu.NONE, R.string.choices);             
  menu.add(Menu.NONE, REGIONS_MENU_ID, Menu.NONE, R.string.regions);             

  return true; // display the menu                        
}  // end method onCreateOptionsMenu                       

// called when the user selects an option from the menu
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
  // switch the menu id of the user-selected option
  switch (item.getItemId()) 
  {
     case CHOICES_MENU_ID:
        // create a list of the possible numbers of answer choices
        final String[] possibleChoices = 
           getResources().getStringArray(R.array.guessesList);

        // create a new AlertDialog Builder and set its title
        AlertDialog.Builder choicesBuilder = 
           new AlertDialog.Builder(this);         
        choicesBuilder.setTitle(R.string.choices);

        // add possibleChoices's items to the Dialog and set the 
        // behavior when one of the items is clicked
        choicesBuilder.setItems(R.array.guessesList,                    
           new DialogInterface.OnClickListener()                    
           {                                                        
              public void onClick(DialogInterface dialog, int item) 
              {                                                     
                 // update guessRows to match the user's choice     
                 guessRows = Integer.parseInt(                      
                    possibleChoices[item].toString()) / 3;          
                 resetQuiz(); // reset the quiz                     
              } // end method onClick                               
           } // end anonymous inner class
        );  // end call to setItems                             

        // create an AlertDialog from the Builder
        AlertDialog choicesDialog = choicesBuilder.create();
        choicesDialog.show(); // show the Dialog            
        return true; 

     case REGIONS_MENU_ID:
        // get array of world regions
        final String[] regionNames = 
           regionsMap.keySet().toArray(new String[regionsMap.size()]);

        // boolean array representing whether each region is enabled
        boolean[] regionsEnabled = new boolean[regionsMap.size()];
        for (int i = 0; i < regionsEnabled.length; ++i)
           regionsEnabled[i] = regionsMap.get(regionNames[i]);

        // create an AlertDialog Builder and set the dialog's title
        AlertDialog.Builder regionsBuilder =
           new AlertDialog.Builder(this);
        regionsBuilder.setTitle(R.string.regions);

        // replace _ with space in region names for display purposes
        String[] displayNames = new String[regionNames.length];
        for (int i = 0; i < regionNames.length; ++i)
           displayNames[i] = regionNames[i].replace('_', ' ');

        // add displayNames to the Dialog and set the behavior
        // when one of the items is clicked
        regionsBuilder.setMultiChoiceItems( 
           displayNames, regionsEnabled,
           new DialogInterface.OnMultiChoiceClickListener() 
           {
              @Override
              public void onClick(DialogInterface dialog, int which,
                 boolean isChecked) 
              {
                 // include or exclude the clicked region 
                 // depending on whether or not it's checked
                 regionsMap.put(
                    regionNames[which].toString(), isChecked);
              } // end method onClick
           } // end anonymous inner class
        ); // end call to setMultiChoiceItems

        // resets quiz when user presses the "Reset Quiz" Button
        regionsBuilder.setPositiveButton(R.string.reset_quiz,
           new DialogInterface.OnClickListener()
           {
              @Override
              public void onClick(DialogInterface dialog, int button)
              {
                 resetQuiz(); // reset the quiz
              } // end method onClick
           } // end anonymous inner class
        ); // end call to method setPositiveButton

        // create a dialog from the Builder 
        AlertDialog regionsDialog = regionsBuilder.create();
        regionsDialog.show(); // display the Dialog
        return true;
  } // end switch

  return super.onOptionsItemSelected(item);
} // end method onOptionsItemSelected

// called when a guess Button is touched
private OnClickListener guessButtonListener = new OnClickListener() 
{
  @Override
  public void onClick(View v) 
  {
     submitGuess((Button) v); // pass selected Button to submitGuess
  } // end method onClick
}; // end answerButtonListener
} // end FlagQuizGame    
import java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Random;
导入java.util.Set;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.res.AssetManager;
导入android.graphics.drawable.drawable;
导入android.os.Bundle;
导入android.os.Handler;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TableLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
公共类FlagQuizGame扩展活动
{
//记录错误消息时使用的字符串
私有静态最终字符串TAG=“FlagQuizGame活动”;
私有列表fileNameList;//标记文件名
私人列表quizCountriesList;//测验中的国家名称
私有映射区域映射;//启用了哪些区域
私有字符串correctAnswer;//为当前标志更正国家/地区
private int totalGuesses;//猜测次数
private int correctAnswers;//正确猜测的数量
private int guessRows;//显示选项的行数
私有随机;//随机数生成器
私有处理程序;//用于延迟加载下一个标志
私有动画shakeAnimation;//不正确猜测的动画
private TextView answerTextView;//显示正确!或不正确!
private TextView questionNumberTextView;//显示当前问题#
private ImageView flagImageView;//显示一个标志
私有表布局按钮TableLayout;//应答按钮表
专用int计数器;
//首次创建活动时调用
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);//调用超类的方法
setContentView(R.layout.main);//膨胀GUI
fileNameList=new ArrayList();//图像文件名列表
quizCountriesList=new ArrayList();//此测验中的标志
regionsMap=新的HashMap();//区域的HashMap
guessRows=1;//默认为一行选项
random=new random();//初始化随机数生成器
handler=new handler();//用于执行延迟操作
计数器=0;
//加载用于错误答案的抖动动画
震动动画=
AnimationUtils.loadAnimation(这是R.anim.error\u shake);
shakeAnimation.setRepeatCount(3);//动画重复3次
//从strings.xml获取世界区域数组
字符串[]区域名称=
getResources().getStringArray(R.array.RegionList);
//默认情况下,从所有区域中选择国家/地区
for(字符串区域:regionNames)
regionsMap.put(region,true);
//获取对GUI组件的引用
questionNumberTextView=
(文本视图)findViewById(R.id.questionNumberTextView);
flagImageView=(ImageView)findViewById(R.id.flagImageView);
按钮选项卡布局=
(TableLayout)findViewById(R.id.buttonTableLayout);
answerTextView=(TextView)findViewById(R.id.answerTextView);
//设置questionNumberTextView的文本
questionNumberTextView.setText(
getResources().getString(R.string.question)+“1”+
getResources().getString(R.string.of)+“10”);
resetquick();//开始新的测验
}//结束方法onCreate
//设置并开始下一个测验
私有无效重置测验()
{      
//使用AssetManager获取映像标志
//仅启用区域的文件名
AssetManager assets=getAssets();//获取应用程序的AssetManager
fileNameList.clear();//清空列表
尝试
{
Set regions=regionsMap.keySet();//获取区域集
//循环通过每个区域
用于(字符串区域:区域)
{
if(regionsMap.get(region))//如果启用了region
{
//获取此区域中所有标志图像文件的列表
字符串[]路径=资产。列表(区域);
用于(字符串路径:路径)
添加(path.replace(“.png”,”);
}//如果结束,则结束
}//结束
}//结束尝试
捕获(IOE异常)
{
Log.e(标记“加载图像文件名时出错”,e);
}//端盖
correctAnswers=0;//重置正确答案的数量
totalGuesses=0;//重置用户的猜测总数
quizCountriesList.clear();//清除先前的测验国家列表
//向quizCountriesList添加10个随机文件名
int flagCounter=1;
int numberOfFlags=fileNameList.size();//获取标志的数量

虽然(flagCounter创建另一个变量,用于跟踪当前问题的尝试次数。每次猜测正确答案时,如果猜测次数=1,则增加另一个计数器,表示在一次尝试中正确猜测了多少答案。此外,在每次回答正确后,将当前猜测计数器重置为0。

您没有询问sp具体的问题,并且你已经发布了一堆文本。如果你缩小代码范围,并且清楚你想要达到的目标,你会得到一个有用的回答。我的问题是,当在第一次尝试每个问题时按下正确的回答按钮时,我如何设置一个计数器,将其相加并在屏幕上显示结果d、 例如,如果一个标志出现,我打错了答案,它不会添加到计数器中,因为我第一次尝试时没有得到它。