随机猜谜游戏java,我无法让play-reach函数正常工作

随机猜谜游戏java,我无法让play-reach函数正常工作,java,Java,这是我下面的代码,我需要插入一个播放再次功能和一个方法,允许我保存用户名,尝试次数,成功尝试次数和球员的最佳分数到一个文本文件!为了回到大学二年级,我必须重复这一部分的课程,所以如果你能给我一些关于代码的建议,我将不胜感激。我从来没有那么擅长编程,所以我陷入了困境。这是我第一次在这个网站上发帖,也是由于一位朋友的推荐,所以提前谢谢 import java.util.Random; import java.util.Scanner; import javax.swing.JOptionPane;

这是我下面的代码,我需要插入一个播放再次功能和一个方法,允许我保存用户名,尝试次数,成功尝试次数和球员的最佳分数到一个文本文件!为了回到大学二年级,我必须重复这一部分的课程,所以如果你能给我一些关于代码的建议,我将不胜感激。我从来没有那么擅长编程,所以我陷入了困境。这是我第一次在这个网站上发帖,也是由于一位朋友的推荐,所以提前谢谢

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;

            }
        }
    }   
import java.util.Random;
导入java.util.Scanner;
导入javax.swing.JOptionPane;
公开课猜谜游戏3{
公共静态void main(字符串[]args)
{
Random generator=new Random();//这是计算机选择的目标
智力猜测;
整数计数=0;
int目标;
字符串用户名;
int答案;
布尔播放=真;
int尝试=6;
扫描仪控制台EIN=新扫描仪(System.in);
扫描仪名称=新扫描仪(System.in);
System.out.println(“您好!请输入您的姓名:\n”);//这是用户输入的姓名
userName=name.nextLine();
System.out.println(“你好”+用户名+:)欢迎来到游戏!\n);
while(play==true)
{
目标=generator.nextInt(100)+1;
System.out.println(“你能猜出我正在想的数字吗?你将有“+trust+”尝试猜出正确的数字”);//这是计算机要求用户猜出数字以及他们将有多少次猜测的地方
做{
guess=consoleIn.nextInt();
计数++;
尝试-=1;
如果(猜测>目标)
System.out.println(“对不起!您的猜测太高了!您还有“+trust+”次尝试!”);//这是为了帮助玩家找到答案
其他的
如果(猜测<目标)
System.out.println(“对不起!您的猜测太低了!您还有“+trust+”次尝试!”);//这是为了帮助玩家找到答案
}        
while(猜测!=目标和计数6);
System.out.println(“您想再次播放“+userName+”?[Y?N]:\N”);
Answer=Answer.nextLine();
如果(回答=“Y”)
玩=真;
其他的
如果(回答=“N”)
System.out.println(“感谢您玩“+userName+”:)!请尽快回来!”;
}
打破
}
}
}   

正如上面的用户所指出的,您需要的是答案==Y。您不应该使用字符串,而应该使用字符。您还应该使用“ToUpperCase()”来确保在用户输入“y”时仍能正常工作。我建议使用.nextChar()而不是nextLine(),以防用户意外输入包含多个字符的字符串。例如,如果他们在按下y键的同时按下t键。

向我们展示您拥有的,直到您展示您拥有的代码,我们无法准确回答。一般来说,如果希望信息在应用程序重新启动后仍然有效,则需要将其写入某个持久位置(如文件),否则,如果只想在应用程序的生命周期内保存信息,则可以将其存储在内存中的某个适当的数据容器中。这就是现在的代码。感谢或
Answer.equalsIgnoreCase(“y”)
,但同样,op从未真正声明过答案。。所以yaknow..当我使用“Answer.equalsIgnoreCase(“y”)时,它指向“.”并表示找不到toUpperCase建议的符号,因为我认为不应在本例中使用字符串。我坚持此建议。