没有机会输入

时间:2017-01-25 17:44:34

标签: java java.util.scanner

我正在制作一个刽子手游戏。一切正常,但我正在努力实现一个高分榜。每个人的得分都有效但当我要求用户在游戏结束时输入他们的名字时,它只是跳过它并返回到原始行。 这是我尝试获取用户名的地方。

if(highScore(score))
{
    System.out.print("Enter your name: ");
    name[position]=scan.nextLine();
}

这是高分法。

//checks if you made it to highscore list
    static boolean highScore(int score)
    {
        for (int i=0;i<high.length;i++)
        {
            if(score>high[i])
            {
                if(i==0)
                {
                    //moving scores down
                    high[2]=high[1];
                    high[1]=high[0];
                    high[0]=score;
                    //moves names down
                    name[2]=name[1];
                    name[1]=name[0];
                    System.out.println("Congratulations you are in first place");
                }
                else if(i==1)
                {
                    //moves scores down
                    high[2]=high[1];
                    high[1]=score;
                    //moves names down
                    name[2]=name[1];
                    System.out.println("Congratulations you are in second place");
                }
                else
                {
                    //replaces score
                    high[2]=score;
                    System.out.println("Congratulations you are in third place");
                }

                position=i;
                return true;
            }
        }
        //did not make it 
        return false;
}

这是整个代码。有点长

import java.util.Random;
import java.util.Scanner;

public class hangman
{
    static int lives=6;//lives at start
    static String secret="";//secret word(filled with asterix'*')
    static String pastGuesses="";//previous incorect attempts
    static int score=0;//starting score
    static int high[]=new int[3];//top 3~~high score for this sitting
    static String name[]=new String[3];//top3~~names of high score holders
    static int position=0;//position for scanning in name
    static boolean playAgain=true;//if true~~have option to continue with game

    //print board
    static void show()
    {
        System.out.println("~~~~~~~~~~~~~~");
        System.out.println(secret);
        if(!(pastGuesses.equals(""))^checkwin())
            System.out.println("Previous incorrect answer: "+pastGuesses);
    }

    //making secret word(Filling with correct numer of asterix'*')
    static void makeSecret(String word)
    {
        for(int i=0;i<word.length();i++)
        {
            secret+="*";
        }
    }

    //checks for win
    static boolean checkwin()
    {
        int count=0;

        for (int i=0;i<secret.length();i++)
        {
            if(secret.charAt(i)=='*')
                count++;
        }
        if(count==0)
        {
            score+=10;
            return true;
        }
        else
            return false;
    }

    //checks for letter in word(lives=0 also in here)
    static void checkLetter(char guess,String word)
    {
        int count=0;
        char[] secretChars = secret.toCharArray();//convert secret to array of chars

        for(int i=0;i<word.length();i++)
        {
            if (word.charAt(i) == guess) 
            {
                secretChars[i] = guess;
                count++;
            }
        }
        secret = new String(secretChars);//back to string

        if(count==0)
        {
            pastGuesses+=guess+" ";
            lives--;
            if(lives==0)
            {
                System.out.println("~~Out of lives, you lose.~~");
                System.out.println("The word was: "+word);
                System.out.println("Your final score was "+score);
                System.out.println("You guessed "+score/20+" words correctly.");
                playAgain=false;
            }
            else
                System.out.println("Incorrect.\nYou have "+lives+" lives left");
        }
    }

    //checks if you made it to highscore list
    static boolean highScore(int score)
    {
        for (int i=0;i<high.length;i++)
        {
            if(score>high[i])
            {
                if(i==0)
                {
                    //moving scores down
                    high[2]=high[1];
                    high[1]=high[0];
                    high[0]=score;
                    //moves names down
                    name[2]=name[1];
                    name[1]=name[0];
                    System.out.println("Congratulations you are in first place");
                }
                else if(i==1)
                {
                    //moves scores down
                    high[2]=high[1];
                    high[1]=score;
                    //moves names down
                    name[2]=name[1];
                    System.out.println("Congratulations you are in second place");
                }
                else
                {
                    //replaces score
                    high[2]=score;
                    System.out.println("Congratulations you are in third place");
                }

                position=i;
                return true;
            }
        }
        //did not make it 
        return false;
    }

    public static void main(String [] args)
    {
        //intialise random gen and scanner
        Scanner scan=new Scanner(System.in);
        Random rand=new Random();
        String answer="";//in game optons
        //word to guess
        String word="";
        int num;

        //list of categories
        String cartoon[]={"homer","bart","lisa","bob","tina","marge","louise","maggie",};
        String english[]={"goneril","hamlet","lear","oethello","shylock","antonio"};
        String clothes[]={"hat","gloves","trousers","jumper","shoes"};
        int play=0;//play again option

        while(true)
        {
            System.out.println("Type start to start game or type high to see highscore\nor type end to quit.");
            answer=scan.next();
            //game code
            if(answer.equalsIgnoreCase("start"))
            {
                //resets everything to original level to start game
                playAgain=true;
                play=0;
                secret="";
                pastGuesses="";
                score=0;

                while(playAgain)
                {
                    //option to play another word
                    while(playAgain)
                    {
                        //play again choice
                        if(play==1)
                        {
                            System.out.println("Do you want to play again?");
                            answer=scan.next();
                            if(answer.equalsIgnoreCase("yes"))
                            {
                                secret="";
                                pastGuesses="";
                                break;
                            }
                            else if(answer.equalsIgnoreCase("no"))
                            {
                                System.out.println("Your final score was "+score);
                                System.out.println("You guessed "+score/20+" word correct.");
                                if(highScore(score))
                                {
                                    System.out.print("Enter your name: ");
                                    name[position]=scan.nextLine();
                                }
                                playAgain=false;
                            }
                            else
                                System.out.println("Invalid choice");
                        }
                        else
                            break;
                    }
                    play=1;
                    //choosing a word at random from chosen category
                    while (playAgain)
                    {
                        System.out.println("Choose a category(type the corresponding number):\n1.Cartoon characters.\n2.English play characters.");
                        System.out.println("3.Items of clothing.");
                        int category=scan.nextInt();

                        if(category==1)
                        {
                            num=rand.nextInt(cartoon.length);
                            word=cartoon[num];
                            break;
                        }
                        else if(category==2)
                        {
                            num=rand.nextInt(english.length);
                            word=english[num];
                            break;
                        }
                        else if(category==3)
                        {
                            num=rand.nextInt(clothes.length);
                            word=clothes[num];
                            break;
                        }
                        else
                            System.out.println("invalid input.");
                    }
                    makeSecret(word);

                    //main game
                    while(lives>0&playAgain)
                    {
                        //printin board
                        show();

                        //making guesses
                        while(true)
                        {
                            //making choice of guessing letter or word
                            System.out.println("Do you want to(Enter corresponding number):\n1.Guess a letter.\n2.Guess the word.");
                            int guessChoice=scan.nextInt();
                            //letter guess
                            if(guessChoice==1)
                            {
                                System.out.print("Guess a letter: ");
                                String s1=scan.next();
                                char guess=s1.charAt(0);
                                //checking letter in word
                                checkLetter(guess,word);
                                //calls highscore
                                if(lives==0)
                                {
                                    if(highScore(score))
                                    {
                                        System.out.print("Enter your name: ");
                                        name[position]=scan.nextLine();
                                    }

                                }
                                break;
                            }
                            //word guess
                            else if(guessChoice==2)
                            {
                                System.out.print("Guess the word: ");
                                String s1=scan.next();
                                if(s1.equalsIgnoreCase(word))
                                    secret=word;
                                else
                                {
                                    checkLetter(' ',word);
                                    //calls high score
                                    if(lives==0)
                                    {
                                        if(highScore(score))
                                        {
                                            System.out.print("Enter your name: ");
                                            name[position]=scan.nextLine();
                                        }
                                    }
                                }
                                break;
                            }
                            else
                                System.out.println("Invalid choice.");
                        }

                        //winning statements
                        if(checkwin())
                        {
                            show();
                            System.out.println("Your current score is "+score);
                            System.out.println("~~Congratulations you won~~");
                            break;
                        }
                    }
                }
            }
            else if(answer.equalsIgnoreCase("high"))
            {
                for(int i=0;i<high.length;i++)
                {
                    System.out.println(name[i]+": "+high[i]);
                }
            }
            else if(answer.equalsIgnoreCase("end"))
                break;
        }
    } 
}

扫描仪在代码的其他部分工作正常,是扫描仪的名称是扫描。任何帮助是值得赞赏的,如果你知道是什么原因导致你可以解释它,所以我再也不做。谢谢。

0 个答案:

没有答案