您好我是Java的新手,我正在尝试为我的一个作业编写一个简单的基于文本的小问答游戏,这里是代码:
import java.util.* ;
class Quiz
{
public static void main( String[] not_in_use )
{
Scanner keyboard = new Scanner( System.in ) ;
int money = 0;
char answer;
boolean quit = false;
int id = 0;
int id1 = 0;
System.out.print("\nWelcome to Haluatko Miljonääriksi?."
+ "\nAn interactive quiz game about Finland by Khoa \"Kazkyu\" Nguyen.\n"
+ "\nYou are given 15 multiple-choice questions."
+ "\nEarn money and move on by choosing the right answer, final price is 1 million Euros."
+ "\nAnswer a question by typing A, B, C, D on your keyboard."
+ "\nType in \"save\" to use Lifesaver."
+ "\nQuit the game and keep your earned money by typing \"exit\"."
+ "\nYou still win with half the money you earned if you answer wrong."
+ "\n\nPress Enter to begin: ");
keyboard.nextLine();
if ( (id <= 15) && (quit == false))
{
System.out.print("\nWhat is the name of Finland in Finnish?:\n"
+"\nA. Suomi"
+"\nB. Soumi"
+"\nC. Suomea"
+"\nD. Soumea"
+"\n\n");
answer = keyboard.nextLine().charAt(0);
while (!( ( answer == 'A' ) ||
( answer == 'a' ) || ( answer == 'B' ) ||
( answer == 'b' ) || ( answer == 'C' ) ||
( answer == 'c' ) || ( answer == 'D' ) ||
( answer == 'd' ) || ( answer == 'E' ) ||
( answer == 'e' ) || ( answer == 'S' ) || (answer == 's')))
{
System.out.print( "\nWrong input A,B,C,D or Exit or Save only.\n");
answer = keyboard.nextLine().charAt( 0 ) ;
}
if (( answer == 'A') || (answer == 'a'))
{
money = money + 200;
id = id + 1;
System.out.print ( "\nCorrected, you earned " + money + " Euros\n" );
}
else if (( answer == 'B') || (answer == 'b') || (answer == 'C') || (answer == 'c')
|| ( answer == 'D') || (answer == 'd'))
{
System.out.print ( "\nWrong, you lose, no money for you\n"
+"Game over, good luck next time\n");
quit = true;
}
else if ((answer == 'E') || (answer == 'e'))
{
System.out.print ("\nYou choose to quit, your money is: " + money + " Euros"
+"\nGood luck next time\n");
quit = true;
}
else if ((answer == 'S') || (answer == 's'))
{
id1 = id1 + 1;
System.out.print ("\nPlease choose your life saver:"
+"\nType 1 for \"Skip and get point\""
+"\nType 2 for \"Surf Wikipedia for answer\", Warning: Very Random"
+"\nType 3 for \"Asked Jere, Michal, Jaska, Marios for help\", Warning: Very Random"
+"\n\n");
int choice = keyboard.nextInt();
if (choice == 1)
{
money = money + 200;
id = id + 1;
answer
System.out.print ("\nQuestion Skipped, earn " + money + " Euros.\n");
}
if ( choice == 2 )
{
Random rand = new Random();
int randomNum = rand.nextInt((4 - 1) + 1) + 1;
if (randomNum == 1)
{
money = money + 200;
System.out.print ("\nWiki said answer is A, right answer, ");
}
else if (randomNum == 2)
{
System.out.print ("\nWiki said answer is B ,wrong answer, Game over.\n"
+"You earned " + money + " Euros.\n");
quit = true;
}
else if (randomNum == 3)
{
System.out.print ("\nWiki said answer is C ,wrong answer, Game over.\n"
+"You earned " + money + " Euros.\n");
quit = true;
}
else if (randomNum == 4)
{
System.out.print ("\nWiki said answer is D ,wrong answer, Game over.\n"
+"You earned " + money + " Euros.\n");
quit = true;
}
}
if (choice == 3)
{
Random rand1 = new Random();
int randomNum1 = rand1.nextInt((4 - 1) + 1) + 1;
if (randomNum1 == 1)
{
money = money + 200;
System.out.print ("\nYou asked Jere and got the answer: A.\n"
+"Right answer, " + money + " Euros.\n");
}
if (randomNum1 == 2)
{
money = money + 200;
System.out.print ("\nYou asked Michal and got the answer: A.\n"
+"Right answer, " + money + " Euros.\n");
}
if ( randomNum1 == 3)
{
System.out.print ("\nYou called Jaska and recieved a \"Whatever, I like milk, why not\" for an answer."
+ "\nYou answered \"Whatever, I like milk, why not\", Wrong answer."
+ "\nGame over, You earn " + money + " Euro.\n");
quit = true;
}
if ( randomNum1 == 4)
{
System.out.print ("You called Marios and he gave you this answer:\n"
+ "\nAyy Lmao, get rekt m8, inb4 4chan raid."
+ "\nYou don't know what the fuck it mean."
+ "\nSo you quit with " + money + " Euros in your pocket.\n");
quit = true;
}
}
}
}
if ( (id <= 15) && (quit == false))
{
System.out.print("\nWhen is the Finnish's Independence Day?:\n"
+"\nA. 12th of December"
+"\nB. 25th of December"
+"\nC. 6th of December"
+"\nD. 31th of December"
+"\n\n");
****\\line 150 THIS IS LINE 150 WHERE IT HAPPEN\\
answer = keyboard.nextLine().charAt(0);****
while (!( ( answer == 'A' ) ||
( answer == 'a' ) || ( answer == 'B' ) ||
( answer == 'b' ) || ( answer == 'C' ) ||
( answer == 'c' ) || ( answer == 'D' ) ||
( answer == 'd' ) || ( answer == 'E' ) ||
( answer == 'e' ) || ( answer == 'S' ) || (answer == 's')))
{
System.out.print( "\nWrong input A,B,C,D or Exit or Save only.\n");
answer = keyboard.nextLine().charAt( 0 ) ;
}
if (( answer == 'C') || (answer == 'c'))
{
money = money + 250;
id = id + 1;
System.out.print ( "\nCorrected, you earned " + money + " Euros\n" );
}
else if (( answer == 'B') || (answer == 'b') || (answer == 'A') || (answer == 'a')
|| ( answer == 'D') || (answer == 'd'))
{
money = money / 2;
System.out.print ( "\nWrong, you lose, your money is " + money + " Euros\n"
+"Game over, good luck next time\n");
quit = true;
}
else if ((answer == 'E') || (answer == 'e'))
{
System.out.print ("\nYou choose to quit, your money is: " + money + " Euros"
+"\nGood luck next time\n");
quit = true;
}
else if ((answer == 'S') || (answer == 's'))
{
id1 = id1 + 1;
System.out.print ("\nPlease choose your life saver:"
+"\nType 1 for \"Skip and get point\""
+"\nType 2 for \"Surf Wikipedia for answer\", Warning: Very Random"
+"\nType 3 for \"Asked Jere, Michal, Jaska, Marios for help\", Warning: Very Random"
+"\n\n");
int choice = keyboard.nextInt();
if (choice == 1)
{
money = money + 250;
id = id + 1;
System.out.print ("\nQuestion Skipped, earn " + money + " Euros.\n");
}
if ( choice == 2 )
{
Random rand = new Random();
int randomNum = rand.nextInt((4 - 1) + 1) + 1;
if (randomNum == 1)
{
money = money + 250;
System.out.print ("\nWiki said answer is C, right answer, ");
}
else if (randomNum == 2)
{
System.out.print ("\nWiki said answer is B ,wrong answer, Game over.\n"
+"You earned " + money + " Euros.\n");
quit = true;
}
else if (randomNum == 3)
{
System.out.print ("\nWiki said answer is A ,wrong answer, Game over.\n"
+"You earned " + money + " Euros.\n");
quit = true;
}
else if (randomNum == 4)
{
System.out.print ("\nWiki said answer is D ,wrong answer, Game over.\n"
+"You earned " + money + "Euros.\n");
quit = true;
}
}
if (choice == 3)
{
Random rand1 = new Random();
int randomNum1 = rand1.nextInt((4 - 1) + 1) + 1;
if (randomNum1 == 1)
{
money = money + 200;
System.out.print ("\nYou asked Jere and got the answer: C.\n"
+"Right answer, " + money + " Euros.\n");
}
if (randomNum1 == 2)
{
money = money + 200;
System.out.print ("\nYou asked Michal and got the answer: C.\n"
+"Right answer, " + money + " Euros.\n");
}
if ( randomNum1 == 3)
{
System.out.print ("\nYou called Jaska and recieved a \"Whatever, I like milk, why not\" for an answer."
+ "\nYou answered \"Whatever, I like milk, why not\", Wrong answer."
+ "\nGame over, You earn " + money + " Euro.\n");
quit = true;
}
if ( randomNum1 == 4)
{
System.out.print ("You called Marios and he gave you this answer:\n"
+ "\nAyy Lmao, get rekt m8, inb4 4chan raid."
+ "\nYou don't know what the fuck it mean."
+ "\nSo you quit with " + money + " Euros in your pocket.\n");
quit = true;
}
}
}
}
} }
一切似乎工作得很好,除非我输入s使用救生员,选择1之后,它会跳过我想要的问题,但它也会给我一个索引出界错误,它说出来& #150;在150行,我对此很困惑,其他所有输入工作正常。
请帮帮我,谢谢
答案 0 :(得分:1)
如果answer = keyboard.nextLine().charAt(0)
抛出StringIndexOutOfBoundsException
,唯一的可能是keyboard.nextLine()
返回一个空字符串。
答案 1 :(得分:0)
keyboard.nextLine()
返回一个空字符串(一个包含0个字符的字符串 - ""
)。
您可以通过System.out.println(keyboard.nextLine().length());
答案 2 :(得分:0)
使用lifeaver时,没有键盘输入,因此keyboard.nextLine()
只是一个空字符串。由于它是空的,它在索引0处没有char。当你使用生理器时,你必须实现一些跳过keyborad输入部分扫描的逻辑。
答案 3 :(得分:0)
问题是keyboard.nextLine()
是一个空字符串,这就是它抛出异常的原因。
它为空的原因是因为在从用户读取整数后,缓冲区中仍然存在换行符,这会导致问题。
修复它(测试)的最简单方法是从用户那里获取整数:
System.out.print("\nPlease choose your life saver:"
+"\nType 1 for \"Skip and get point\""
+"\nType 2 for \"Surf Wikipedia for answer\", Warning: Very Random"
+"\nType 3 for \"Asked Jere, Michal, Jaska, Marios for help\", Warning: Very Random"
+"\n\n");
//Modify this line right here like so.
int choice = Integer.parseInt(keyboard.nextLine());
这将阻止换行符进入缓冲区,从而解决所有问题。
答案 4 :(得分:0)
在选项1中
answer
没有分号或任何它应该在你的编译器中告诉你这个我不知道它为什么没有,它在自然之后就会出错。