尝试打印姓名

dojqjjoe  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(329)

我试图生成一个代码,询问用户的名称,然后打印他稍后输入的名称,但打印时变量为null。如何在不更改询问用户名的方法的情况下修复此问题,直到用户喜欢为止(我在我想要名字印上“?”的地方做了标记

public class Main {

  static class NamingClass {

    String PlayerName;

    static void ChooseName() {
      System.out.println("Enter your name:");
      Scanner ScanPlayerName = new Scanner(System.in);//Object in scanner for name
      NamingClass ObjForName = new NamingClass();//Object to use "PlayerName" String
      ObjForName.PlayerName = ScanPlayerName.nextLine(); //ask user to enter name
      System.out.println("Your  name is " + ObjForName.PlayerName + ", is this correct?");
      System.out.println("True=Yes");
      System.out.println("False=No");
      Scanner ObjToCheck = new Scanner(System.in); //Object in scanner for name check
      Boolean CheckName = ObjToCheck.nextBoolean(); //boolean is asked to user
      if(CheckName == false) {
        ChooseName(); //repeat 
      }
      else{
      }
    }
  }

  static void Credits() { //method that should print name entered in ChooseName()
    System.out.println("The player name is" /* + ?????????????*/);
  }

  public static void main(String[] args) {
    NamingClass.ChooseName();
    Credits();
  }
}
l7wslrjt

l7wslrjt1#

您的错误是在中创建对象 chooseName . 对象在方法调用后被销毁并丢失。。。这就是你需要的。。

public class Main {

  static class NamingClass {

/*make it static and accessible..or consider getters if its private*/    
public static String PlayerName;

    static void ChooseName() {
      System.out.println("Enter your name:");
      Scanner ScanPlayerName = new Scanner(System.in); //Object in scanner for name
/* change this...      
NamingClass ObjForName = new NamingClass();//Object to use "PlayerName" String
      ObjForName.PlayerName = ScanPlayerName.nextLine(); //ask user to enter name

* /

      PlayerName = ScanPlayerName.nextLine(); //now no object is created because its static
     // System.out.println("Your  name is " + ObjForName.PlayerName + ", is this correct?");
System.out.println("Your  name is " + PlayerName + ", is this correct?");
      System.out.println("True=Yes");
      System.out.println("False=No");
      Scanner ObjToCheck = new Scanner(System.in); //Object in scanner for name check
      Boolean CheckName = ObjToCheck.nextBoolean(); //boolean is asked to user
      if(CheckName == false) {
      }
      else{
      }
    }
  }

  static void Credits() { //method that should print name entered in ChooseName()
    //System.out.println("The player name is" /* + ?????????????*/);
System.out.println("The player name is" +PlayerName);
  }

  public static void main(String[] args) {
    NamingClass.ChooseName();
    Credits();
  }
}
00jrzges

00jrzges2#

我认为在调用函数时应该传递name变量。

static void Credits(string name) 
{
    System.out.println("The player name is" + name);
}

要获取名称,namingclass.choosename()将返回名称

String name = NamingClass.ChooseName();

然后打电话:

Credits(name);

代码如下:

public class Main {

    static class NamingClass {

        static String ChooseName() {

            String PlayerName = "";
            Boolean CheckName = false;

            while (!CheckName) {
                System.out.println("Enter your name:");
                Scanner ScanPlayerName = new Scanner(System.in);//Object in scanner for name
                PlayerName = ScanPlayerName.nextLine(); //ask user to enter name
                System.out.println("Your  name is " + PlayerName + ", is this correct?");
                System.out.println("True=Yes");
                System.out.println("False=No");
                Scanner ObjToCheck = new Scanner(System.in); //Object in scanner for name check
                CheckName = ObjToCheck.nextBoolean(); //boolean is asked to user
            }

            return PlayerName;
        }
    }

    static void Credits(String name) {
        System.out.println("The player name is" + name);
    }

    public static void main(String[] args) {
        String name = NamingClass.ChooseName();
        Credits(name);
    }
}

相关问题