扫描仪出错:java输入不匹配异常
希望你能给我一些好的建议:我有一个游戏,我需要玩家输入名字,但它只允许一个单词。如果用户输入超过1个单词,程序将给出javamismatchexception错误并关闭程序是否有办法允许用户输入多个单词作为自己的名字?提前感谢!我为底部的文字混乱道歉。我是这个网站的新用户,它说我已经将代码缩进了4个空格,但并没有正确显示扫描仪出错:java输入不匹配异常,java,Java,希望你能给我一些好的建议:我有一个游戏,我需要玩家输入名字,但它只允许一个单词。如果用户输入超过1个单词,程序将给出javamismatchexception错误并关闭程序是否有办法允许用户输入多个单词作为自己的名字?提前感谢!我为底部的文字混乱道歉。我是这个网站的新用户,它说我已经将代码缩进了4个空格,但并没有正确显示 在.nextLine()中使用而不是next()作为名称。太棒了!我不知道你能做到。谢谢@Codebender@AArisso有时,阅读Javadoc会带来回报: Scann
在.nextLine()中使用
而不是next()
作为名称。太棒了!我不知道你能做到。谢谢@Codebender@AArisso有时,阅读Javadoc会带来回报:
Scanner in = new Scanner(System.in);
String name;
int playerClass=0;
int wincount=0;
int userhealth=100+(wincount)*3;
int comphealth=100;
int userChoice;
int regularAttackD;
int powerAttackD;
int quickAttackD;
int playerchoice;
int advance=1;
Random b = new Random ();
Random d = new Random ();
Random f = new Random ();
powerAttackD=b.nextInt(15)+25;
regularAttackD=d.nextInt(8)+15;
quickAttackD=f.nextInt (4)+3;
System.out.println ("Welcome to the zombie apocalypse...\nIn this text-based game you will need to make decisions based on prompts you see.\nYour decisions will determine your own fate.");
System.out.println ("What would you like to be called?");
name = in.next();
System.out.println (name + ", in this game you may need to fight enemies in a turn-based combat style.");
do
{
System.out.println ("You will now enter a number for the class you want.\n(1)Scout: Increased hit chance for all attacks.\n(2)Brute: Increased damage for all attacks\n(3)Medic:Increased health during combat.");
playerClass = in.nextInt();
if (playerClass == 1 || playerClass == 2 || playerClass == 3)
{
advance=0;
} else {
System.out.println ("The number you entered is not a class! Try again and enter 1,2, or 3.");
}
} while (advance==1);
advance=1;