从另一个方法调用时,私有字符串变为空

Private string becomes null when called from another method

我正在制作 'Who Wants To Be A Millionaire' 游戏。我处于后期阶段,我正在尝试存储用户名和分数,但是当我从另一个方法调用 getUser() 时它显示为 null,我打印了 getUser() 的值以确保它实际存储价值。这是我的代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment1;

import java.util.HashMap;

/**
 *
 * @author ac918
 */
public class User {
    
   // Earnings earning;
    String namee;
    private String user;
    private Integer scores;
    boolean repeat = true;
    GameMethods getGameMethods = new GameMethods();
    HashMap <String, Integer> storeDetails = new HashMap<>();
    
    
    public String storeUser()
    {
        System.out.println("Before we begin what is your name?");
        setUser(getGameMethods.getUserInput());
        //user = this.getUser();
        namee = this.getUser();
        System.out.println("This is getUser in storeUser method" + namee);
        
        if (this.getUser().matches(".*[a-zA-Z]+.*"))
        {
            System.out.println("Goodluck to you " + this.getUser() + " on your quest to win a million dollars");  
        }
        else 
        {
            while(repeat)
            {
                System.out.println("Invalid Input!");
                System.out.println("Name must be letters");
                System.out.println("Please input name to begin");
                setUser(getGameMethods.getUserInput());
                
                if (this.getUser().matches(".*[a-zA-Z]+.*"))
                {
                    System.out.println("Goodluck to you " + this.getUser() + " on your quest to win a million dollars");  
                    repeat = false;
                }
            }
        }
        
        return this.getUser();
    }

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * @return the score
     */
    public Integer getScore() {
        return scores;
    }
    
    public int setUserEarnings (int count)
    {
        setScore(count);
        
        switch (count) {
            case 0:
                System.out.println("Unfortunately, you have won nothing !!!");
                break;
            case 1:
                System.out.println("Congratulations!!! you have won " + Earnings.ONEHUNDRED.getValue());
                break;
            case 2:
                System.out.println("Congratulations!!! you have won " + Earnings.TWOHUNDRED.getValue());
                break;
            case 3:
                System.out.println("Congratulations!!! you have won " + Earnings.THREEHUNDRED.getValue());
                break;
            case 4:
                System.out.println("Congratulations!!! you have won " + Earnings.FIVEHUNDRED.getValue());
                break;
            case 5:
                System.out.println("Congratulations!!! you have won " + Earnings.ONETHOUSAND.getValue());
                break;
            case 6:
                System.out.println("Congratulations!!! you have won " + Earnings.TWOTHOUSAND.getValue());
                break;
            case 7:
                System.out.println("Congratulations!!! you have won " + Earnings.FOURTHOUSAND.getValue());
                break;
            case 8:
                System.out.println("Congratulations!!! you have won " + Earnings.EIGHTTHOUSAND.getValue());
                break;
            case 9:
                System.out.println("Congratulations!!! you have won " + Earnings.SIXTEENTHOUSAND.getValue());
                break;
            case 10:
                System.out.println("Congratulations!!! you have won " + Earnings.THIRTYTWOTHOUSAND.getValue());
                break;
            case 11:
                System.out.println("Congratulations!!! you have won " + Earnings.SIXTYFOURTHOUSAND.getValue());
                break;
            case 12:
                System.out.println("Congratulations!!! you have won " + Earnings.ONETWENTYFIVETHOUSAND.getValue());
                break;
            case 13:
                System.out.println("Congratulations!!! you have won " + Earnings.TWOFIFTYTHOUSAND.getValue());
                break;
            case 14:
                System.out.println("Congratulations!!! you have won " + Earnings.FIVEHUNDREDTHOUSAND.getValue());
                break;
            case 15:
                System.out.println("Congratulations!!! you have won " + Earnings.ONEMILLION.getValue());
                break;
            default:
                System.out.println("Unfortunately, you have won nothing !!!");
                break;
        }
          
        return this.getScore();
    }
    
    public void storeUserHighscore(String name, int score)
    {
        System.out.println("This is getUser in storeUserHighscore method" + namee);
        name = this.getUser();
        score = this.getScore();
        
        storeDetails.put(name, score);
        System.out.println(storeDetails);
    }

    /**
     * @param score the score to set
     */
    public void setScore(Integer score) {
        this.scores = score;
    }
}

这是输出

Before we begin what is your name?
John
This is getUser in storeUser method John
Goodluck to you John on your quest to win a million dollars
What does 'NFL' stand for
a) National Food League
b) National Federation League
c) National Football League
d) National Fighting League
a
Unfortunately this answer is wrong
Unfortunately, you have won nothing !!!
Thank you for playing
This is getUser in storeUserHighscore method null
{null=-5}

这里是调用storeUserHighscore的地方

public class GameMethods 
{
    int count = 0;
    String name = "";
    int score = 0;
    
    public void reviewUserInput(String input, String answer, HashMap <Integer, LifeLines> lifelines)
    {
        User getUser = new User();
       // System.out.println(input);
        GameMethods getGameMethods = new GameMethods();
        boolean loop = true;
        
        if (input.equalsIgnoreCase("x"))
            {
                System.exit(0);
            }
            else if (input.equalsIgnoreCase("l"))
            {
                input = getGameMethods.hasLifelines(lifelines, answer);
            }
            else if (!input.equalsIgnoreCase("a") && !input.equalsIgnoreCase("b") && !input.equalsIgnoreCase("c") && !input.equalsIgnoreCase("d"))
            {
                while(loop)
                {
                 System.out.println("Invalid Input try again!!");
                 input = getGameMethods.getUserInput();
                 
                 if (input.equalsIgnoreCase("a") || input.equalsIgnoreCase("b") || input.equalsIgnoreCase("c") || input.equalsIgnoreCase("d") || input.equalsIgnoreCase("l") || input.equalsIgnoreCase("x"))
                 {
                     loop = false;
                     
                     if (input.equalsIgnoreCase("x"))
                     {
                          System.exit(0);
                     }
                     else if (input.equalsIgnoreCase("l"))
                     {
                        // input = " ";
                         input = getGameMethods.hasLifelines(lifelines, answer);
                     }
                 }
                }       
            }
       // System.out.println("this is ans " +answer);
       // System.out.println("This is user "  +input);
       
            if (input.equalsIgnoreCase(answer))
            {
                count++;
                System.out.println("Correct answer!!!");
                getUser.setUserEarnings(count);
            }
            else if (!input.equalsIgnoreCase(answer))
            {
                count-=5; 
                System.out.println("Unfortunately this answer is wrong");
                getUser.setUserEarnings(count);
                System.out.println("Thank you for playing");
                getUser.storeUserHighscore(name, score);
                System.exit(0);
            }
    }
}

这里也是主要的方法

public class Game 
{
     public static LifeLines lifeLine ;
    
    public static void main(String[] args)
    {
        String answer;
        String userInput;
        
        HashMap <Integer, LifeLines> lifelines = new HashMap <> ();
        lifelines.put(0, lifeLine);
        lifelines.put(1, lifeLine);
        lifelines.put(2, lifeLine);
       
        GameMethods getGameMethods = new GameMethods();
        InputOutput getInputMethods = new InputOutput();
        User getUserMethods = new User();
        getInputMethods.readIntroduction();
        getInputMethods.clearBuffer();
        getUserMethods.storeUser();
        
       for(int counter = 0; counter < 16; counter++)
       {
           
            getInputMethods.readQuestion();
            getInputMethods.readA();
            getInputMethods.readB();
            getInputMethods.readC();
            getInputMethods.readD();
            answer = getInputMethods.readAnswer();
            
            userInput = getGameMethods.getUserInput();

            getGameMethods.reviewUserInput(userInput, answer, lifelines);             
       }
       
       
    }
}

你在哪里调用 storeUserHighscore 方法?

此外,我认为第一个 if 语句是不必要的。

重构成这样

while(repeat){
 
if (this.getUser().matches(".*[a-zA-Z]+.*")){
                
System.out.println("Goodluck to you " + this.getUser() + " on your quest to win a million dollars");  
                   break;
               
 }

    System.out.println("Invalid Input!");
    System.out.println("Name must be letters");
    System.out.println("Please input name to begin");
    setUser(getGameMethods.getUserInput());
}

我也相信你有两个做同样事情的变量(namee 和 user)。

您正在 GameMethodsreviewUserInput(String input, String answer..) 中创建一个新的用户 User getUser = new User(); 对象,并且您没有为新创建的 User 对象设置任何名称,这就是为什么您将获得名称的 null 值。

要解决此问题,您可以向此方法 reviewUserInput() 添加另一个参数 (User)。像这样,

public void reviewUserInput(User user, String input, String answer, HashMap <Integer, LifeLines> lifelines);

Game class 中的此 getGameMethods.reviewUserInput(userInput, ...); 方法调用此方法。像这样,

getGameMethods.reviewUserInput(getUserMethods, userInput, answer, lifelines);

因此,您可以在此方法中使用 user 对象 reviewUserInput() 因此,您不会在此处获得任何 null,您将在此处获得实际名称 (John) .