首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从另一个方法调用时,私有字符串变为空。

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

Stack Overflow用户
提问于 2021-08-22 12:38:14
回答 2查看 296关注 0票数 0

我在做一个“谁想成为百万富翁”的游戏。我处于后期阶段,我试图存储用户名和分数,但是当我从另一种方法调用getUser()时,它显示为null,我打印了getUser() out的值,以确保它实际上存储了这些值。这是我的密码

代码语言:javascript
复制
/*
 * 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;
    }
}

这是输出

代码语言:javascript
复制
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的地方

代码语言:javascript
复制
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);
            }
    }
}

这里也是主要的方法

代码语言:javascript
复制
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);             
       }
       
       
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-23 02:05:46

您正在reviewUserInput(String input, String answer..) of GameMethods中创建一个新的用户GameMethods对象,并且没有为新创建的User对象设置任何名称,这就是为什么要为名称获取null值的原因。

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

代码语言:javascript
复制
public void reviewUserInput(User user, String input, String answer, HashMap <Integer, LifeLines> lifelines);

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

代码语言:javascript
复制
getGameMethods.reviewUserInput(getUserMethods, userInput, answer, lifelines);

所以,您可以在这个方法中使用user对象,所以,这里不会得到任何null,这里会得到尖头名称(John)。

票数 0
EN

Stack Overflow用户

发布于 2021-08-22 15:36:16

您在哪里调用storeUserHighscore方法?

另外,我认为第一个if声明是不必要的.

重构成这样。

代码语言:javascript
复制
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)。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68881397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档