扫描程序出错:java输入不匹配异常

时间:2016-01-22 03:28:29

标签: java

希望你能在这里给我一些好的建议:我有一个游戏,我需要玩家输入名称,但它只允许它是一个单词。如果用户输入的单词超过1个,则程序会发出javamismatchexception错误,并且该程序将关闭。 有没有办法允许用户输入多个单词作为他们的名字?提前致谢!我为底部的一堆话语道歉......我对这个网站很新,并且说我已经用4个空格缩进代码,但它并没有正确显示。

Screenshot here

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;

0 个答案:

没有答案