gpt4 book ai didi

java - this.name 没有输出所需的字符串

转载 作者:行者123 更新时间:2023-12-01 22:05:28 25 4
gpt4 key购买 nike

import java.util.Scanner;

class Description
{
String name = "Pablo";
public void getName(String newName) throws InterruptedException
{
System.out.println("Your name is ");
name = newName;
System.out.println(newName);
Thread.sleep(2000);
System.out.println("However, the creator of this crappy program is ");
System.out.println(this.name);
}

}

public class Learning {
public static void main(String[] args) throws InterruptedException
{
System.out.println("Description of yourself");
Scanner scan = new Scanner(System.in);
Thread.sleep(1000);
System.out.println("Please type your name");
Description person = new Description();
String myName = scan.nextLine();
person.getName(myName);
}
}

我对 Java 还很陌生。我正在阅读有关二传手和这个的内容。我预计它会在这部分代码中输出“Pablo”。

    public void getName(String newName) throws InterruptedException
{
System.out.println("Your name is ");
name = newName;
System.out.println(newName);
Thread.sleep(2000);
System.out.println("However, the creator of this crappy program is ");
System.out.println(this.name);
}

但是,假设我在这部分代码中输入“Steve”

String myName = scan.nextLine();

它应该向方法 getName 传递一个参数,确实如此。然而,当我到达这个部分时

        System.out.println(this.name);

它打印出“Steve”,而它应该打印出“Pablo”。所以控制台看起来像

Description of yourself
Please type your name
Steve
Your name is
Steve
However, the creator of this crappy program is
Steve

我做错了什么吗?

最佳答案

It prints out "Steve," while it should print out "Pablo. So the console looks like

没有。在 getName 方法中,您将自己的名字更改为给定的名字。

 name = newName; 

因此,从那时起,您的名称不再带有“Pablo”,并且具有值“Steve”

关于java - this.name 没有输出所需的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com