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