私はプログラミングにかなり慣れていないので、アルファベットの文字の順序を見つける簡単な練習プログラムに取り組んでいます。これは非常に簡単なはずですが、while ループを追加すると、何らかの理由で StringIndexOutOfBoundsException が発生します。そのため、プログラムは最初にすべきことを行います...しかし、プログラムを再実行しないと再度テストすることはできません。内部に単純な print ステートメントだけを使用して while ループをテストしたところ、うまくいきました。
どんな助けでも大歓迎ですありがとう!
import java.io.*;
public class test {
public static void main(String[] args) throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
boolean again=true;
String response;
while (again)
{
System.out.println("Enter a letter to find it's order in the alphabet");
char theLetter = (char) in.read();
System.out.println(theLetter + " is the " + convertLetter(theLetter) + "th letter of the alphabet");
System.out.println("want to play again?");
response = in.readLine();
if (response.charAt(0)=='n')
{
again=false;
}
}
System.out.println("end program");
}
public static int convertLetter(char TheLetter)
{
//number value 'a'=97
//number value 'b'=98
//number value 'c'=99
//subtracting 'a' from any other number will reveal how many places away that number is from the start
//thus finding it's chronological place in the alphabet
int NumberValue= (int)TheLetter;
int a = 'a';
int CalulateOrder = (NumberValue - a) + 1;
return CalulateOrder;
}
}