当程序循环时,它打印行#34;输入你的名字(第一个和最后一个):"两次?

时间:2014-04-08 18:41:56

标签: java

所以在程序中我问用户是否要重新运行程序,但是当它执行时它会打印行"输入你的名字,"然后是一个空格,两次。有人可以帮我找到原因吗?当你第一次运行它时,它不会发生。

import java.util.Scanner;  
public class PirateName                                         
{                                                                
    static Scanner input = new Scanner(System.in); 
    static String[]firstNames = {"Captain", "Dirty", "Squidlips", "Bowman", "Buccaneer",       
        "Two toes", "Sharkbait", "Old", "Peg Leg", "Fluffbucket",       
         "Scallywag", "Bucko", "Deadman", "Matey", "Jolly",       
         "Stinky", "Bloody", "Miss", "Mad", "Red", "Lady",       
          "Shipwreck", "Rapscallion", "Dagger", "Landlubber", "Freebooter"};       


    static String[]secondNames =        
    {"Creeper","Jim","Storm","John","George","Money","Rat","Jack","Legs",      
    "Head","Cackle","Patch","Bones","Plank","Greedy","Mama","Spike","Squiffy",      
    "Gold","Yellow","Felony","Eddie","Bay","Thomas","Spot","Sea"};      

    static String[]thirdNames =        
        {"From-the-West","Byrd","Jackson","Sparrow","Of-the-Coast","Jones","Ned-Head",      
        "Bart","O'Fish","Kidd","O'Malley","Barnacle","Holystone","Hornswaggle",      
        "McStinky","Swashbuckler","Sea-Wolf","Beard","Chumbucket","Rivers","Morgan",      
        "Tuna-Breath","Three Gates","Bailey","Of-Atlantis","Of-Dark-Water"};      

    static String[] letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",      
    "p","q","r","s","t","u","v","w","x","y","z"};      

    public static void main(String[] args) {       

        System.out.println("Welcome to the pirate name generator");       
        System.out.println("");  
        boolean running = true; 
        while(running){ 
            nameGenerator(); 
        } 
    } 


    public static boolean nameGenerator()  
    {
        boolean rerun = false; 
        int x = 0;       


        System.out.println("Enter your name (first and last): ");     
        String userName = input.nextLine();      
        System.out.println("");      
        try{ 
            String first = userName.substring(0,1);   
            for (int i=0;i <= 25 ; i++)      
            {      
                if(first.equalsIgnoreCase(letters[i]))      
                {      
                    first = firstNames[i];      

                }      

            }      
            String last1 = userName.substring(userName.indexOf(' ')+1);      

            for (int i=0;i <= 25 ; i++)      
            {      
                if(last1.substring(0,1).equalsIgnoreCase(letters[i]))      
                {      
                    last1 = secondNames[i];      

                }      

            }      
            String last2 = userName.substring(userName.length() - 1);     
            for (int i=0;i <= 25 ; i++)      
            {      
                if(last2.equalsIgnoreCase(letters[i]))      
                {      
                    last2 = thirdNames[i];      

                }      

            }      
            System.out.println("Your pirate name is: ");     
            System.out.println("");     
            System.out.println(first+" "+last1+" "+last2);     
            System.out.println("");
            System.out.println("Would you like to try again? (Type 1 for yes, 2- no)");     
            int a = input.nextInt(); 

            if (a==2)     
            {     
                rerun = false;   
                System.out.println("Good Bye!"); 
                return rerun; 
            }     
            else 
            {   
                rerun = true; 
            }   

            return rerun;  
        }    
    catch (Exception e){ 
        System.out.println(" "); 
    } 
        return rerun; 
    }
}

2 个答案:

答案 0 :(得分:1)

我至少看到三个问题。

  • 在方法结束时,当您阅读a的值时,您从Scanner中提取了一个整数,但是您并未撤出整数后面的换行符。这意味着下次拨打nextLine()时,您获得的所有内容都是空行。解决此问题的方法是在input.nextLine()之后立即添加额外input.nextInt()

  • 您正在捕捉异常并将其丢弃,甚至不打印堆栈跟踪。这意味着如果您的程序遇到问题,您将永远不会找到有关该问题的任何信息。

  • 您未在rerun方法之外使用值nameGenerator。当您拨打电话时,您需要检查running是否属实,但running永远不会改变,因此您只需继续拨打电话即可。所以你可能想要改变调用它的代码。

    boolean shouldRun = true; 
    while (shouldRun) {
        shouldRun = nameGenerator(); 
    } 
    

答案 1 :(得分:0)

看起来您正在使用输入扫描程序输入整数和字符串。您应该使用两个单独的扫描仪,或者更改它以便输入带有.nextLine()然后更改为整数。

问题是您在决定再试时输入两个字符。第一个是int,第二个是字符。第二个字符不是整数,因此它保留在缓冲区中。然后,当您第二次输入时,您正在使用已在缓冲区中包含字符的扫描仪。因此它处理缓冲区并将字符串左侧作为空行读取。

http://www.java-forums.org/new-java/24042-input-nextline.html