IndexOutofBounds和list.remove Java的问题

时间:2017-09-08 16:33:35

标签: java arraylist indexoutofboundsexception

固定它是一个与问题没关系的错误

我正在编写代码来玩游戏 我创建了一个名为card的数组列表,其中包含81个数字 我在循环中运行此代码(以及其他一些东西),在开始时初始化9次并且它工作正常,但是当我在第一轮播放后再次运行它时,它给了我一个超出范围的索引异常 我调试了它,在最后一个例子中,cards.size是68,选择是58,所以逻辑上它应该工作

choice=(int) (Math.random()*cards.size());
card= (int)cards.get(choice);
cards.remove(choice)

哦,如果你正在玩它,你获得一套并提出错误的方式是有三个项目,其中每个数字对于所有三个相同或不同于所有三个 例如:
0:3212
5:1231
7:2223
在第一列中有1 2和3,在第二列中它们都是2,依此类推

完整的长码

import java.util.*;
import java.lang.Math.*; 
// This tries to play a version of the game of set with no GUI(because I havent learned it yet)
//It is supposed to crash eventually if you use all the cards or go to a stalemate if there are no cards on the board
// but it is crashing sooner than it should
public class Setdeath {
   public static void main(String[] args) {
      Scanner keyboard = new Scanner(System.in);
      // Initializing all of the stuff so i don't have to later
      int fir, sec, thi, dog, k, klat, qa, qb, firsti, secondi, narc, choice, card, carda, x, tricar, ex, first, second, third;
      int trid;
      double v, bees;
      // Where I create the list it's just numbers 0-80
      List cards= new ArrayList();
      k=0;
      while (k<81){
         cards.add(k);
         k++;
      }
   // put 12 cards in a list
      List table= new ArrayList();
      k=1;
      while (k<13){
         choice=(int) (Math.random()*cards.size());
         card= (int)cards.get(choice);
         cards.remove(choice);
         carda=card;
   // this converts it into a trinary ish thing for gameplay
         ex=(carda/27);
         carda= carda- ex*27;
         tricar= 1000*(ex+1);
         ex=(carda/9);
         carda= carda- ex*9;
         tricar= 100*(ex+1)+tricar;
      
         ex=(carda/3);
         carda= carda- ex*3;
         tricar= 10*(ex+1)+tricar;
      
         ex=(carda);
         tricar=ex+1+tricar;
      
         table.add(tricar);
         k++;
      }
   
   // for repeat
      for(;;){
         k=0;
         while (k<12){
            dog=(int)table.get(k);
            System.out.println(k+": "+dog);
            k++;}
      
      // get three cards spots
         System.out.print("What's Piece A? ");
         fir = keyboard.nextInt();
         System.out.print("What's Piece B? ");
         sec = keyboard.nextInt();
         System.out.print("What's Piece C? ");
         thi = keyboard.nextInt();
      // initialize cards
         first= (int)table.get(fir);
         second= (int)table.get(sec);
         trid= (int)table.get(thi);
      
      
         v=3.0;
         third=0;
         firsti=first;
         secondi=second;
         // this takes the first two cards and finds the third card to make a match
         while (v>-1){
            bees = Math.pow(10.0,v);
            narc =(int)bees;
            qa= firsti/(narc);
            qb= secondi/(narc);
            firsti=firsti-qa*narc;
            secondi=secondi-qb*narc;
            if (qa==qb){
               third= third+ qa*narc;}
            else {
               third= third+ (6-qa-qb)*narc;}
            v--;
         }
         // If a match is made than we remove the 3 cards from the table and add three more
      
         if (trid==third){
            System.out.println("Set");
            table.remove(thi);
            table.remove(sec);
            table.remove(fir);
            klat=0;
            //where it crashes
            while (klat<3){
               choice=(int) (Math.random()*cards.size());
               card= (int)cards.get(choice);
               cards.remove(choice);
            
            //Converts to trinary to put back on the table
               carda=card;
               ex=(carda/27);
               carda= carda- ex*27;
               tricar= 1000*(ex+1);
               ex=(carda/9);
               carda= carda- ex*9;
               tricar= 100*(ex+1)+tricar;
            
               ex=(carda/3);
               carda= carda- ex*3;
               tricar= 10*(ex+1)+tricar;
            
               ex=(carda);
               tricar=ex+1+tricar;
               // adds it to the table
            
               table.add(tricar);
               k++;
            }}
         else{
            System.out.print("Not a set");}
      }}}

0 个答案:

没有答案