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”。this.name不输出所需的字符串
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);
}
但是,可以说,我输入“史蒂夫”,在它应该参数传递给方法GetName,这是确实的代码
String myName = scan.nextLine();
的这一部分。然而,当我到
System.out.println(this.name);
它打印出“史蒂夫”,而应该打印出“巴勃罗的一部分。因此,控制台看起来像
Description of yourself
Please type your name
Steve
Your name is
Steve
However, the creator of this crappy program is
Steve
难道我做错了什么?
你觉得'name = newName;'是吗? – Titus
我想你对'name'变量的范围感到困惑。 – ochi