我在要求我自定义功能的行上找到IndexOutOfBoundsException
:
motCars = remplaceTirets(motAlea, motCars, lettreDon);
。
如果给定的字母等于单词中的字母,则该函数应该将一个或多个短划线转换为字母,并且实际函数中的一行表示:
tempo += tirets.charAt(j);
结果是:_ _ _ _ _ _ _(这些破折号的数量取决于程序选择的单词,哪个有效,然后它要求给出一封信,但当我给出一封信时,我得到:
线程'main'中的异常java.lang.String IndexOutOfBoundsException。字符串索引超出范围:1。
部分是法语,因为我住在魁北克省。但是我希望这没关系,因为法语单词只涉及字符串和单词,而不是java的逻辑。我是一个初学者,并且对所有Java论坛上的所有建议都不知所措。欢迎提出任何具体建议。
提前感谢您花时间看看!
阿妮塔
import java.util.Scanner;
class Tp {
public static void main( String[] args ) throws Exception {
Scanner clavier = new Scanner(System.in);
String motAlea = "";
String motCars = "";
char lettreDon = Character.UNASSIGNED;
String tempo = "";
String invite = "";
String tirets = "";
int l = 0;
int m = 0;
final String ANNONCE = "J'ai choisi un mot a deviner\n";
final String INSTRUCT = "Entrez une lettre a la fois. L'indice d'essais: 15.\n";
final String INVITE = "\tEntrez une lettre: ";
final String BRAVO = "BRAVO ! Le mot a deviner etait: ";
final String DESOLE = "DESOLE...Vous avez perdu...";
String[] vingtMots = { "WATTHEUREMETRE", "HELIOGRAPH", "GRENOUILLERE", "CONTRAROTATIF",
"CUISSARDE", "BRIGANTINE", "AVITAILLEUR", "ENTREDOUBLURE",
"GALLETAGE", "OEUILLERE", "CREMAILLERE", "HALTEROPHILIE",
"MARTINGALE", "EMPENNAGE", "ENCOCHAGE", "DECLENCHEUR",
"BIMETALLIQUE", "PIVOTEMENT", "DECLINAISON", "CROISILLON"
}; // tableau string
int indexAlea = 0;
indexAlea = (int)(Math.random() * 20) + 1;
motAlea = vingtMots[indexAlea];
for (l = 0; l < motAlea.length(); l++) {
tempo += "_";
motCars = tempo;
} // for
System.out.print(ANNONCE);
System.out.print(INSTRUCT);
l = 0;
do {
if (motCars.equals(motAlea)) {
System.out.print(BRAVO + motAlea + ", " + "devine en " + m +
" tentatives");
System.exit(0);
} // if
if (l == 15) {
System.out.print("\n" + DESOLE + "Le mot a devine etait: " +
motAlea + ". " + "Au revoir... ");
System.exit(0);
} // if
for (int i = 0; i < motAlea.length(); i++) {
System.out.print(motCars.charAt(i) + " ");
} // for
m = l + 1;
invite = "\t" + INVITE + m + "> :";
lettreDon = lecture(invite);
motCars = remplaceTirets(motAlea, motCars, lettreDon);
l++;
} // do
while (l < 16); {
System.out.print("\n" + DESOLE + "Le mot a devine etait: " + motAlea + ". "
+ "Au revoir... ");
} // while
} //main(...)
public static char lecture(String invite1){
Scanner clavier = new Scanner(System.in);
final String ERREUR = "La valeur entree est erronnee !\nReprenez-vous...";
final String VIDE = " ";
String retour = "";
do {
try {
System.out.print(invite1);
retour = clavier.nextLine().trim(); // Mise en forme;
for (int k = 0; k < retour.length(); k++) {
if(Character.isLetter(retour.charAt(k))) {
return retour.toUpperCase().charAt(0);
} // if
} // for
} // try
catch (Exception e) {
System.out.print(ERREUR);
}
}// do
while (!retour.equals(VIDE)); {
retour = "X";
return retour.charAt(0);
} // while
} // lecture(...)
public static String remplaceTirets(String motAlea1, String tirets,
char lettre) {
String retour;
String tempo = "";
for (int j = 0; j < motAlea1.length(); j++) {
String lettre1 = Character.toString(lettre);
if (motAlea1.charAt(j) != lettre1.charAt(0)) {
tempo += tirets.charAt(j);
} // if
else {
tempo += lettre1.charAt(0);
} // else
tirets = tempo;
} // for
return retour = tirets;
} //remplaceTirets(...)
}//Tp
答案 0 :(得分:1)
该行
tirets = tempo;
应该不在for循环中。
将您的代码更改为
for (int j = 0; j < motAlea1.length(); j++) {
String lettre1 = Character.toString(lettre);
if (motAlea1.charAt(j) != lettre1.charAt(0)) {
tempo += tirets.charAt(j);
} // if
else {
tempo += lettre1.charAt(0);
} // else
//tirets = tempo; //REMOVE THIS LINE
} // for
tirets = tempo; //ADD THIS LINE
答案 1 :(得分:0)
您正在tirets
根据motAlea1
的长度访问某个职位。我希望motAlea1.length() > tirets.length()
。
for (int j = 0; j < motAlea1.length(); j++) {
String lettre1 = Character.toString(lettre);
if (motAlea1.charAt(j) != lettre1.charAt(0)) {
tempo += tirets.charAt(j); //THIS COULD FAIL!!!
}else{
tempo += lettre1.charAt(0);
}
tirets = tempo;
}
答案 2 :(得分:0)
在这个循环中:
for (int j = 0; j < motAlea1.length(); j++) {
String lettre1 = Character.toString(lettre);
if (motAlea1.charAt(j) != lettre1.charAt(0)) {
tempo += tirets.charAt(j);
} // if
else {
tempo += lettre1.charAt(0);
} // else
tirets = tempo;
} // for
字符串tirets
比motAlea1
短,因此您尝试检索字符超出其结尾。