第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Java 程序獲取 ArrayIndexOutOfBounds 錯(cuò)誤

使用 Java 程序獲取 ArrayIndexOutOfBounds 錯(cuò)誤

UYOU 2021-06-17 15:15:43
我正在制作一個(gè)程序,試圖根據(jù)之前的決定猜測(cè)您是要選擇正面(h)還是反面(t)。程序在前 3 次猜測(cè)“h”,然后查找玩家的最后兩個(gè)選擇,遍歷之前的選擇以找到相同的決策組合并計(jì)算哪個(gè)決策(h 或 t)遵循相同組合最多次數(shù),然后根據(jù)猜測(cè)選擇組合。如果它猜對(duì)了你的輸入,它就贏了,否則玩家就贏了。第一個(gè)獲得25勝,贏得整場(chǎng)比賽。該程序?qū)η?3 次猜測(cè)工作正常,但當(dāng)它進(jìn)入第二個(gè) while 循環(huán)時(shí),它給出“線程“main”中的異常 java.lang.ArrayIndexOutOfBoundsException: -2”import java.util.ArrayList;import java.util.Scanner;public class Guesser {    public static void main(String[] args) {        Scanner reader = new Scanner(System.in);        ArrayList<String> list = new ArrayList<>();        int last = (list.size() - 1);        int secondToLast = (list.size() - 2);        int index = 2;        int wins = 0;        int h = 0;        int t = 0;        while (wins < 25 && list.size() - wins < 25) {            System.out.println("Type h or t: ");            String letter = reader.nextLine();            list.add(letter);            String guess = "h";當(dāng)程序進(jìn)入此文本下方的 while 循環(huán)時(shí),它停止工作(程序應(yīng)該在此處嘗試將最后兩個(gè)決策與之前的所有決策進(jìn)行比較,以嘗試找到相同的決策組合并找出哪個(gè)決策(h 或 t)大部分時(shí)間遵循組合):            while (list.size() > 3 && index < list.size()) {                if (list.get(index - 2).equals(list.get(secondToLast))                     && list.get(index - 1).equals(list.get(last))) {                    String nextGuess = list.get(index);                     if (nextGuess.equals("t")) {                        t++;                    } else {                        h++;                    }                }                index++;            }下面的一切都有效:            if (t > h) {                    guess = "t";                }             System.out.println("You typed " + letter + ", I guessed " + guess +".");            if (guess.equals(letter)) {                wins++;            }             System.out.println("Computer wins: " + wins);            System.out.println("Player wins: " + (list.size() - wins));            System.out.println("");        }     }}
查看完整描述

2 回答

?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

這些行有錯(cuò)誤:


ArrayList<String> list = new ArrayList<>();// here list is empty and its size is 0


int last = (list.size() - 1);// here last is 0 - 1 = -1

int secondToLast = (list.size() - 2);// here secondToLast is 0 - 2 = -2

將這些行更改為:


int last = list.size() - 1 >= 0 ? list.size() - 1 : 0;

int secondToLast = list.size() - 2 >= 0 ? list.size() - 2 : 0;

改變這一行: while (list.size() > 3 && index < list.size())


對(duì)此:


while (list.size() > 3 && index >= 2 && index < list.size())

因此index - 2總是大于或等于零


并更改此行: int secondToLast = (list.size() - 2);


對(duì)此:


int secondToLast = list.size() - 2 >= 0 ? list.size() - 2 : 0;


查看完整回答
反對(duì) 回復(fù) 2021-06-17
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

int last = (list.size() - 1);

 int secondToLast = (list.size() - 2);

將這些放在 while 循環(huán)中。它會(huì)起作用。:)


查看完整回答
反對(duì) 回復(fù) 2021-06-17
  • 2 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)