import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
/**
This class prints the numeric value of a letter grade given by the user.
*/
public class Words
{
int i=0;
String[] wordz;
/**
Constructs words class
*/
public Words()
{
wordz= new String[5];
}
/**
collects 5 words from user and places then into array
@return the gradeValue
*/
public void inputArray(String a, String b, String c, String d, String e)
{
wordz = new String[] { a, b, c, d, e };
}
/**
counts even and odds
@return numeric grade
*/
public void removeShortWords()
{
ArrayList<String> wordzList = new ArrayList<String>(Arrays.asList(wordz));
for(i=0; i < wordz.length; i++)
{
if(wordz[i].length() < 3)
wordzList.remove(i);//out of bounds error here
String[] wordz = wordzList.toArray(new String[wordzList.size()]);
}
}
/**
prints out the array of 10 positive integers
@return numeric grade
*/
public void printArray()
{
System.out.println(Arrays.toString(wordz));
}
}
這是我的測試人員類。字數組程序的出錯錯誤
import java.util.Scanner;
public class WordPrgm {
public static void main(String[] args)
{
Words wordArray = new Words();
System.out.println("PLease enter five words");
Scanner in = new Scanner(System.in);
String w1 = in.nextLine();
String w2 = in.nextLine();
String w3 = in.nextLine();
String w4 = in.nextLine();
String w5 = in.nextLine();
wordArray.inputArray(w1, w2, w3, w4, w5);
wordArray.removeShortWords();
wordArray.printArray();
}
}
這裏的程序應該從數組中刪除少於3個字母的單詞並打印出新單詞。我一直在一遍又一遍地查看代碼,但是我看不到解決方案在哪裏以及我錯過了什麼。我認爲for循環可能會搞砸了。謝謝!
我在程序的這一點上總是收到一個錯誤。
wordzList.remove(i);
少於五個?看起來更像我三個人。 – laune 2014-12-05 07:09:00
是的,對不起,我沒有想到,它應該少於3,而不是5。 – javaProgrammer 2014-12-05 07:12:05
@javaProgrammer如果答案幫助你不要忘記接受它 – 2014-12-19 07:40:23