import java.util.Scanner;
import java.util.Random;
public class ResponseTimeProject
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random rand = new Random();
System.out.print("Please enter your full name: ");
String name = in.nextLine();
System.out.println ("Hello " + name
+ ". Please answer as fast as you can."
+ "\n\nHit <ENTER> when ready for the question.");
in.nextLine();
String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int character=(int)(Math.random()*26);
String s=alphabet.substring(character, character+1);
Random r = new Random();
for (int i = 0; i < 1; i++)
{
System.out.println (alphabet.charAt(r.nextInt(alphabet.length())));
}
long startTime = System.currentTimeMillis();
System.out.print("What is the next letter in the alphabet?" + " ");
String response = in.nextLine();
int letter = Integer.parseInt(response);
long endTime = System.currentTimeMillis();
String outcome;
if (letter == character+1)
outcome = "Correct!";
else
outcome = "Incorrect.";
long reactionTime = endTime - startTime;
System.out.println("That took " + reactionTime + " milliseconds");
System.out.println("Thank you " + name + ", goodbye.");
}
}
這是我的代碼。我正在詢問用戶字母表中的下一個字母是什麼。我無法弄清楚正確的字符串結果。我想讓程序知道答案是否正確。線程「main」中的異常java.lang.NumberFormatException:用於輸入字符串:「G」