所以我对编程还很陌生,我正在做一个简单的练习程序,可以在字母表中找到字母顺序。这应该很容易......但由于某种原因,当我添加一个 while 循环时,我得到了一个 StringIndexOutOfBoundsException 。所以程序确实第一次做它应该做的......但不允许我在不重新运行程序的情况下再次测试。我测试了 while 循环,里面只有一个简单的 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;
}
}