随机猜谜游戏java,我不能让游戏再次起作用

时间:2015-07-24 16:09:11

标签: java

这是我下面的代码,我需要再次插入一个播放功能和方法,允许我将用户名,尝试次数,成功尝试次数和玩家最佳得分保存到文本文件中!我必须重复这一课程,以重新回到大学的第二年,所以我们非常感谢任何关于代码的指针。我从来没有那么擅长编程,所以我很难被卡住。这是我第一次在这个网站上发布,因为朋友的推荐,所以提前感谢

import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class GuessingGame3 {

    public static void main(String[] args)
    {

        Random generator = new Random(); //This is were the computer selects the Target

        int guess;
        int count = 0;
        int Target;
        String userName;
        int answer;
        boolean play = true; 
        int attempt = 6;

        Scanner consoleIn = new Scanner(System.in); 
        Scanner name = new Scanner(System.in); 

        System.out.println("Hello! Please enter your name:\n"); //This is were the user enters his/her name
        userName= name.nextLine();

        System.out.println("Hello "+ userName + " :) Welcome to the game!\n");

        while (play == true)
        {
            Target = generator.nextInt(100) + 1;
            System.out.println("Can you guess the number i'm thinking off? You will have "+ attempt +" attempts to guess the correct number"); //This is where the computer asks the user to guess the number and how many guesses they will have

            do {
                guess = consoleIn.nextInt();
                count++;
                attempt -= 1;

                if (guess > Target)
                System.out.println("Sorry! Your guess was too high! You have "+ attempt +" attempts left!"); //This is to help the player get to the answer 
                else 
                if (guess < Target)
                System.out.println("Sorry! Your guess was too low! You have "+ attempt +" attempts left!"); //This is to help the player get to the answer 
               }        
                while(guess != Target && count <6);

                if(guess == Target) {
                System.out.println("Congratulations "+  userName + ", it took you "+ count +" attempts to guess correctly!"); //This tells the player that they got the correct answer and how many attempts it took
                    }

                else 
                {
                System.out.println("Sorry "+ userName + ", You've used up all of your guesses! The correct answer was "+ Target + "!");  //This tells the player that they failed to find the number and then tells them what the correct answer  
                }
                {
                while(guess = Target || count > 6);
                System.out.println("Would you like to play again "+ userName +"? [Y?N]:\n");
                Answer = answer.nextLine();
                if (Answer = "Y")
                    play = true;
                    else
                        if (Answer = "N")
                            System.out.println("Thanks for playing "+ userName +" :)! Please come back soon!");
                            }
                            break;

            }
        }
    }   

1 个答案:

答案 0 :(得分:0)

正如上面提到的用户,你需要这是答案== Y.你不应该使用字符串,而应该使用字符。您还应该使用'ToUpperCase()'来确保它仍然有用,如果用户输入'y'我建议使用.nextChar()而不是nextLine(),以防用户意外输入一个包含多个字符的字符串。例如,如果他们在按下y时碰到t按钮。